简体   繁体   English

无法在 docker 中导入我编译的 cython 模块

[英]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.我可以在 windows (visual studio) 上成功编译我的 cython 模块并运行使用 cython 模块的 python 代码。

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.我可以在 linux(gcc/Docker 容器)上成功编译我的 cython 模块,但是当我运行在 Windows 上运行的相同 python 代码时,我得到一个 ModuleNotFoundError 。

Here is my project structure on linux.这是我在 linux 上的项目结构。 When I compile it puts the .so file at /engine for some reason (doesn't do this on windows).当我编译时,它出于某种原因将 .so 文件放在 /engine 中(在 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 中适当位置后的结构:

+-- 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:这是我的 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):这是尝试导入 cython 模块的代码(最后一行是错误的):

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.解决方案可能是将以下行添加到 Dockerfile。

RUN python setup.py build_ext --inplace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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