I have the following package structure:
setup.py
/cpp_graph_test
fastgraph.pyx
graph.cpp
graph.h
graph.pxd
heap.cpp
heap.h
__init__.py
I've created a setup.py as follows:
from setuptools import Extension, setup
from Cython.Build import cythonize
sourcefiles = ['cpp_graph_test/fastgraph.pyx', 'cpp_graph_test/graph.cpp', 'cpp_graph_test/heap.cpp']
extensions = [
Extension(
name="cpp_graph_test.fastgraph",
sources=sourcefiles,
extra_compile_args=['-O3']
)
]
setup(
name='cpp_graph_test',
packages=['cpp_graph_test'],
ext_modules=cythonize(extensions, language_level=3, include_path=["cpp_graph_test"]),
version='0.0.1'
)
I install, and things seem to go fine...
$ sudo pip3 install .
Processing /home/le_user/Documents/cpp_graph_test
Preparing metadata (setup.py) ... done
Building wheels for collected packages: cpp-graph-test
Building wheel for cpp-graph-test (setup.py) ... done
Created wheel for cpp-graph-test: filename=cpp_graph_test-0.0.1-cp310-cp310-linux_x86_64.whl size=641411 sha256=8e3b20a0bec7a8f5a739deba272289bce370ab99abae02a3787d3f10718b03c9
Stored in directory: /tmp/pip-ephem-wheel-cache-ofm34m2v/wheels/c1/03/6a/f746b1b945b60e93aa67ee67e8e6a2c4537c0a87dbb72ffa34
Successfully built cpp-graph-test
Installing collected packages: cpp-graph-test
Attempting uninstall: cpp-graph-test
Found existing installation: cpp-graph-test 0.0.1
Uninstalling cpp-graph-test-0.0.1:
Successfully uninstalled cpp-graph-test-0.0.1
Successfully installed cpp-graph-test-0.0.1
However, this fails...
$ python3
Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from cpp_graph_test.fastgraph import FastGraph
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cpp_graph_test.fastgraph'
I can import the cpp_graph_test package in its entirety, but if I try to use pkgutil to list all the modules in the package, there are none.
Is there something wrong with my setup.py file?
EDIT: When I run the install with pip, the pyx file is translated to C++, and I get binary generated in the build directory, so the Cython build is running, and apparently not creating errors.
EDIT2: While experimenting, I got this error: ImportError: cannot import name 'FastGraph' from 'cpp_graph_test' (/home/le_user/Documents/cpp_graph_test/cpp_graph_test/__init__.py) This error makes it seem like Python is looking at the raw code for the package instead of looking at the actual built, installed package after the pip install. It's like there's some weird symlink somewhere or something...
EDIT3: I can sudo pip3 uninstall cpp_graph_test and it'll tell me "skipping cpp_graph_test as it is not installed." But then I can start a Python shell (from any folder) and say import cpp_graph_test and it'll be successful. Not sure how to uninstall a package that's already uninstalled but that lives on anyway?