简体   繁体   中英

Can't import my compiled cython module in docker

I can successfully compile my cython module on windows (visual studio) and run python code that uses the cython module.

I can successfully compile my cython module on linux (gcc/Docker container), but I get a ModuleNotFoundError when I run the same python code that works on windows.

Here is my project structure on linux. When I compile it puts the .so file at /engine for some reason (doesn't do this on windows). So this is the structure after I move it to the proper spot in bbmajors_compute/compute_engine/engine:

+-- bbmajors_compute
|   +-- compute_engine
|   |   +-- engine
|   |   |   +-- __init__.py
|   |   |   +-- compute_cpp.pyx
|   |   |   +-- compute_cpp.pxd
|   |   |   +-- compute_cpp.cpp
|   |   |   +-- compute_cpp.cpython-37m-x86_64-linux-gnu.so
|   |   +-- __init__.py
|   |   +-- views.py
+-- __init__.py
+-- setup.py

Here is my setup.py:

from distutils.core import setup

from Cython.Build import cythonize

setup(ext_modules=cythonize("bbmajors_compute/compute_engine/engine/compute_cpp.pyx"))

Here is the build command:

python ./setup.py build_ext --inplace

Here is the code trying to import the cython module (last line is one that gives error):

from django.shortcuts import render
from django.shortcuts import redirect
from django.http import HttpResponseRedirect
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

from .forms import TeamForm

from .engine.player_costs_handler import create_costs_dict
from .engine.player_stats_handler import create_stats_dict
from .engine.player_merge_handler import merge_player_data, create_results_dict

# Cython modules
from .engine.compute_cpp import CalculateCombinationsCpp

Here is the error:

web_1  |   File "/code/bbmajors_compute/compute_engine/views.py", line 14, in <module>
web_1  |     from .engine.compute_cpp import CalculateCombinationsCpp
web_1  | ModuleNotFoundError: No module named 'bbmajors_compute.compute_engine.engine.compute_cpp'

Any help is appreciated! I've been looking at this for a while...

I was having a similar issue for a while and just about pulling my hair out trying to solve it. Luckily I came across this gist which proved very helpful.

The solution may be for you to add the following line to your Dockerfile.

RUN python setup.py build_ext --inplace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM