'ImportError: No module named ...' when trying to import pyx file to Jupyter
Asked Answered
A

2

17

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)

Angelia answered 21/3, 2019 at 14:34 Comment(2)
Check out my answer here: https://mcmap.net/q/488617/-how-do-i-import-function-from-pyx-file-in-python For me the problem was that I didn't compile the setup and then all related files needed to be in the same directory.Parmer
Seems like a python version issue. Have you tried creating a virtualenv with python3?Salvo
S
0

The import can occur because you are trying to import .pyx directly into python. You need first install Cython which is the container of pyxinstall. This might help.

But you can try out the other way. Try to convert pyx file to py file then import the file. The work of yours will be done but will have sacrifice the speed.

Symbolize answered 17/9, 2022 at 14:53 Comment(0)
A
0

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.

Acciaccatura answered 14/11, 2022 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.