I’m learning cython, so I’ve created a small Matrix lib on C++ to create cython wrapper for it. Then I created setup.py file to build this module, the code is bellow:
setup.py
from setuptools import setup from Cython.Build import cythonize setup( ext_modules=cythonize("Matrix.pyx"), extra_compile_args=["-std=c++11"] )
After that I’ve built module with command python3 ./setup.py build_ext --inplace, which created Matrix.cpython-310-x86_64-linux-gnu.so file, and run small script to test the module:
python3 ./setup.py build_ext --inplace
Matrix.cpython-310-x86_64-linux-gnu.so
from Matrix import PyMatrix matrix = PyMatrix(rows=5, cols=5) matrix.set(row=0, col=0, value=42) matrix = matrix + matrix value = matrix.get(0, 0) d = PyMatrix.as_matrix([[1,2,3,4], [5,6,7,8]]) print(d.to_string())
Everything still worked fine. After that I decided to create a new clear venv, copy there my test script and .so file, everything still worked. Then I removed .so file, to be sure, that everything worked because .so was “near” to executable script and not because it was installed somewhere else. As I expected, when .so file was removed, script failed to run with such error:
.so
Traceback (most recent call last): File "$HOME_PATH/PycharmProjects/CythonTests/./main.py", line 1, in <module> from Matrix import PyMatrix ModuleNotFoundError: No module named 'Matrix'
Now the final part: After that I copied this two files to the other computer with similar Linux (I use Ubuntu 22.04, other computer uses also Debian-based Linux). There I put this two files near each other and ran python3 ./main.py, but it failed to run with this error:
python3 ./main.py
Traceback (most recent call last): File "./main.py", line 1, in <module> from Matrix import PyMatrix, TransformType ModuleNotFoundError: No module named 'Matrix'
Can somebody explain why python can find module built by cython on my computer, but can’t find on another, and how to fix it?
The issue you’re encountering is likely due to differences in the Python environments between the two computers. When you run python3 ./main.py, Python searches for modules in the directories listed in sys.path. The discrepancy in behavior may be caused by differences in the environments, such as the presence of virtual environments, module installation paths, or the location of shared libraries.
sys.path
Here are a few suggestions to troubleshoot and resolve the issue:
Check Python Version Compatibility: Ensure that both computers are using compatible Python versions. If the module is built for Python 3.10, make sure Python 3.10 is installed on the other computer.
Check sys.path: Print the sys.path on both computers to see the directories where Python searches for modules. Add the directory containing your .so file to the sys.path if it’s not already included.
import sys print(sys.path)
Check Virtual Environments: If you are using a virtual environment, make sure that the virtual environment is activated when running the script. The virtual environment may have its own site-packages directory where the .so file should be placed.
site-packages
Use Absolute Paths: Instead of relying on the relative path to the .so file, use an absolute path in your script. This ensures that the script can locate the shared library regardless of the current working directory.
from Matrix import PyMatrix
Change to:
from /absolute/path/to/Matrix import PyMatrix
Check Library Dependencies: Ensure that the other computer has all the necessary dependencies for the shared library. If there are shared libraries that your .so file depends on, they must be present on the other computer.
Rebuild the Module: Consider rebuilding the Cython module on the other computer. Run the setup.py script on the other computer to generate the .so file locally.
python3 setup.py build_ext --inplace
Then try running your script again.
pip
pip install .
This ensures that the module is installed in a location that Python recognizes.
By addressing these points, you should be able to troubleshoot and resolve the issue of the module not being found on the other computer.