I have this file em.pyx in the same folder as the Jupyter notebook where I try to import it but it is giving me the error
ImportError: No module named em
I've tried adding
import sys
sys.path.insert(0, 'name_of_directory_where_pyxfile_is')
or
sys.path.append('my/path/to/module/folder')
as suggested here and here, but I keep getting the same error. I've also created an empy __init__.py file, but nothing.
EDIT: Then I added
import pyximport
pyximport.install()
before import em, and now I get lots of errors from python 2.7 (I don't know why python2 since the compilation of pyx was made with python3 and the jupyter kernel is also set to Python3); the first three are:
ImportError Traceback (most recent call last)
<ipython-input-2-778b3d13b1ae> in <module>()
2 pyximport.install()
3
----> 4 import em1d
/home/me/.local/lib/python2.7/site-packages/pyximport/pyximport.pyc in load_module(self, fullname)
460 self.pyxbuild_dir,
461 build_inplace=self.inplace,
--> 462 language_level=self.language_level)
463 return module
464
/home/me/.local/lib/python2.7/site-packages/pyximport/pyximport.pyc in load_module(name, pyxfilename, pyxbuild_dir, is_package, build_inplace, language_level, so_path)
231 raise exc.with_traceback(tb)
232 else:
--> 233 exec("raise exc, None, tb", {'exc': exc, 'tb': tb})
234 return mod
235
EDIT2: Also, after running pyximport.install() I get (None, None)
As far as I know you cannot import Cython-Files directly because they may not be valid Python, C nor C++.
What you can do is to use Cython to compile the files into compiled C/C++ Code. That'll create a *.so-File (shared object). As soon as you have that, you can import it just like a regular python module.
One way is using a setup.py file to do that:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('em.pyx'))
Then you just execute that script like:
python3 setup.py build_ext --inplace
And that will create a importable em.cp3xx-win... .pyd / em.cpython-3x... .so File (depending on your OS) inside the same directory as your setup file. It is importable using import em. (Besides it will create a .c / .cpp File as well)
That should work with a valid pip install Cython installation, but it could be that you need a C/C++ compiler as well.
The other way is using the cythonize command:
cythonize -i -a em.pyx
The -a tells it to create an HTML-File as well, which contains you code with
colorized lines. The color says how much interaction that specific line has with Python. Generally, you want to reduce that colorization to improve the performance gains, though note that there are cases in which Python is a little bit more performant (e.g., with regular expressions as far as I know)
I hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With