I have a simple pyx
file:
$ cat helloworld.pyx
print("hello world in pyx file to be converted to so file by cython.")
I have created an .so
module using Cython. The directory contains following files:
$ ls -ltrh
total 140K
-rw-r--r-- 1 cardio cardio 177 Jul 3 2018 setup.py
-rw-r--r-- 1 cardio cardio 73 Jul 3 2018 helloworld.pyx
-rw-r--r-- 1 cardio cardio 74K Jul 3 2018 helloworld.c
-rwxr-xr-x 1 cardio cardio 52K Jul 3 2018 helloworld.cpython-35m-x86_64-linux-gnu.so
drwxr-xr-x 2 cardio cardio 4.0K Sep 10 20:21 __pycache__
However, when I try to import this module, I get error:
$ python3 -c "import helloworld"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'helloworld'
Where is the problem and how can it be solved?
CodePudding user response:
Most commonly the reason why you can't import a module is that python doesn't see it in the location it is looking at sys.path
.
You can verify this by going to the same location as the produced helloworld module, or by including it in the paths by force sys.path.insert(0,os.getcwd())
can be a current directory
or a relative/absolute path
.
I seem to notice a setup.py
, have you tried installing this with pip install .
while being in that folder, after which trying to use it?