【发布时间】:2019-12-27 13:12:56
【问题描述】:
我有一个从 pyx 文件编译为 c 文件的 Cython 模块,我试图在 python 模块中导入和使用。我在 Mac 上运行 python 3.6。当我运行 gcc -v 时,输出是:
配置为:--prefix=/Library/Developer/CommandLineTools/usr - -with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2 .1 Apple LLVM 版本 10.0.1 (clang-1001.0.46.4) 目标:x86_64-apple-darwin18.7.0 线程模型:posix InstalledDir:/Library/Developer/CommandLineTools/usr/bin
运行 python setup.py build 和 python setup.py install 没有错误,对应文件的 .so 和 .c 文件出现在路径上的正确目录中。
当我尝试导入模块时,我在 init 文件中收到错误,该错误来自尝试导入另一个子模块的行:
from . import subModule
我已经尝试更新 python 和 Cython,并确保 gcc 在 user/lib 中。
这是我的 setup.py 文件:
from Cython.Build import cythonize
setupArgs = dict(
name="module",
version="1.1.0",
description=description,
url="https://github.com/XXXX/XXXXX/module",
author="CTcue",
author_email="info@XXXXXX.com",
ext_modules=cythonize(["module/*.pyx"]),
)
# Use distutils setup
from distutils.core import setup
setup(**setupArgs)
这是错误信息:
File "module/subModule.pyx", line 4, in init module.subModule
ModuleNotFoundError: No module named 'thirdModule'
有问题的第三个模块有一个 .c 文件和一个 .so 文件,它们对应于 .pyx 文件,据我所知,那里的一切都是有序的。
模块的init.py:
__title__ = 'Pseudonomizer'
__version__ = '1.0.2'
__author__ = 'CTcue'
__license__ = 'Proprietary'
__copyright__ = 'Copyright 2016 CTcue'
from . import subModule
子模块:
import thirdModule
thirdModule.doSomething()
第三个模块:
import re
from . import anotherModule
def doSomething:
#code that does something
编辑:为了查看编译器是否有问题,我尝试使用“gcc thirdModule”手动编译thirdModule的.c文件,并得到以下错误:
Undefined symbols for architecture x86_64:
这似乎表明问题与编译器有关,但我仍然没有找到解决方案。
任何帮助将不胜感激。
【问题讨论】:
-
@hoefling 我添加了一个小例子和一个进一步的线索,以防你能想到什么。
-
您的问题与 Cython 无关,而是与 Python3 禁用隐式相对导入有关(请参阅 *.com/q/12172791/5769463)。这意味着 subModule 应该使用
from . import thirdModule而不是import thirdModule这仅适用于隐式相对导入(即 Python2) -
@ead 我更改了导入,但现在`我遇到了与以前相同的错误。我忘了在堆栈跟踪的底部有这个:主可执行文件的隐式入口/启动(也许你的意思是:___pyx_module_is_main_pseudonymizer__address)ld:未找到架构x86_64 collect2的符号:错误:ld返回1退出状态
-
而
thirdModule.so相对于subModule.so在哪里,在同一目录中?