0

I compiled my Python code with Cython and got a .pyd file. I want to import it so I was advised to put it in my PYTHONPATH.

Here it is :

In [1]: import sys
In [2]: sys.path
Out[2]: 
['',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\spyder\\utils\\site',
 'C:\\Users\\loic\\Anaconda2\\python27.zip',
 'C:\\Users\\loic\\Anaconda2\\DLLs',
 'C:\\Users\\loic\\Anaconda2\\lib',
 'C:\\Users\\loic\\Anaconda2\\lib\\plat-win',
 'C:\\Users\\loic\\Anaconda2\\lib\\lib-tk',
 'C:\\Users\\loic\\Anaconda2',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\Sphinx-1.5.1-py2.7.egg',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\win32',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\Pythonwin',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\setuptools-27.2.0-py2.7.egg',
 'C:\\Users\\loic\\Anaconda2\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\loic\\.ipython']

I put my .pyd file in C:\\Users\\loic\\Anaconda2\\lib\\site-packages and when I tried to import it with Python I got that message:

    import maido
ImportError: DLL load failed: %1 is not a valid Win32 application.

Then I put my .pyd file in the same directory as my Python programm and I got the exact same message.

I did some research and I came across that post (Using cython extension once compiled [under Windows], how to use .pyd's?) but it doesn't seem to give me more information.

I am using Python 64 bits with Anaconda 2 because the code I compiled required Python 64 bits (it needed more memory because otherwise the console was quickly saturated in memory).

Can someone tell me what should I do to make it work with Python 64 bits please ?

EDIT: In case you need it, here is my setup.py file

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

from Cython.Distutils import build_ext
from Cython.Build import cythonize

import numpy as np

setup(name = "maido",
      include_dirs = [np.get_include()],
      cmdclass = {'build_ext': build_ext},
      ext_modules = cythonize("C:\Users\python\Documents\maido\maido_cython.pyx"),
      )
Loïc Poncin
  • 511
  • 1
  • 11
  • 30
  • I would try to create a proper python package so that `pip install .` works (maybe with `--user` flag while testing). – J.J. Hakala Jun 24 '17 at 19:08
  • @Lo Are you sure your compiler didn't compile it with a 32bit compiler? I.e. use the Visual Studio 64bit command prompt then try again – Matt Jun 24 '17 at 19:13
  • @Matt I think so, I am using the Anaconda 2 (64 bits) Prompt. You mean that instead of typing `C:\Users\loic\Documents\maido\setup.py build_ext --inplace` I should type `C:\Users\python\Documents\maido\setup.py build_ext --inplace --compiler=something` or something like it ? – Loïc Poncin Jun 24 '17 at 20:48
  • @Lo What I mean is that if you have a 32bit and 64bit compiler on your system before you Cythonize you should open up the Visual Studio tools command prompt for 64bit before you type the `Cythonize ` command. – Matt Jun 25 '17 at 04:04
  • I tried what you said with but it didn't work. I was told that Cython was easy to use but I can't get anything to work. I'll uninstall Anaconda and start again. Thanks for your help. – Loïc Poncin Jun 25 '17 at 15:56
  • @Loic Cython "easy to use" is a fallacy. But once you learn it it is very powerful. – Matt Jul 04 '17 at 16:52
  • @Matt I succeeded, I made a stupid mistake but my program did not really get any faster. I must have forgot something. As you pointed out Cython is more complicated than it looks. – Loïc Poncin Jul 04 '17 at 17:41
  • Good to hear - I was about to post how to do a fresh install but you beat me to it. The trick with Cython is you really have to use memoryviews to get direct C++ access between NumPy and Python. Then you have to learn pointers when passing memoryviews to functions to avoid mem copying. – Matt Jul 04 '17 at 17:50

0 Answers0