简体   繁体   English

程序包无法导入鸡蛋中的cython库

[英]Package can't import cython library in the egg

I can use my package from its folder, but as soon as I install it as an egg package, cython-based modules can't get imported. 我可以从其文件夹中使用程序包,但是一旦将其安装为egg程序包,就无法导入基于cython的模块。

I have the following package structure 我有以下包装结构

src/
├── mypkg/
│   ├── __init__.py
│   ├── myfile.py
├── cython/
│   ├── fastcode.pyx
setup.py

The module myfile.py imports import fastcode . myfile.py模块import fastcode In setup.py , I have setup.py ,我有

setup(
    name='mypkg',
    version='0.1',
    packages=find_packages(where="src"),
    package_dir={'':'src'},
    cmdclass={'build_ext': build_ext},
    ext_modules=df_ext_modules,
)

I also have in setup.cfg , 我也有setup.cfg

[build_ext]
build-lib=src/mypkg

If I run python setup.py install , the cython code gets compiled and creates fastcode.pyd (or fastcode.so in Linux) in mypkg folder as instructed by the config file. 如果我跑python setup.py install时,用Cython代码被编译并创建fastcode.pyd (或fastcode.so在Linux上) mypkg通过配置文件的指示文件夹。

Now, if I go to the mypkg folder, and run python , I can safely do import mypkg.myfile . 现在,如果我转到mypkg文件夹并运行python ,则可以安全地import mypkg.myfile However, when I run python from some other folder (such that the installed .egg file gets used) I get error ImportError: No module named fastcode . 但是,当我从其他某个文件夹运行python(以便使用已安装的.egg文件)时,出现错误ImportError: No module named fastcode

I tried to add 我试图添加

package_data={
    'mypkg': ['*.pyd', '*.so']
},

to setup.py , this adds fastcode.pyd to the egg file (by exploring the unzipped version), but still doesn't work for importing. setup.py ,这会将fastcode.pyd添加到egg文件(通过浏览解压缩版本),但是对于导入仍然不起作用。

I found how to fix it in my case: had to remove setup.cfg . 我发现了如何解决此问题:必须删除setup.cfg For those who are interested, the problem is that with the new setup, the .egg file also contains generated fastcode.py file, and fastcode.pyc file, not just fastcode.pyd file (which I would expect by looking at mypkg after the compilations with the previous setup where only .pyd files were present). 对于那些有兴趣谁,问题是,随着新的设置中, .egg文件还包含生成fastcode.py文件, fastcode.pyc文件,而不仅仅是fastcode.pyd文件(我将通过查看预计mypkg后使用仅存在.pyd文件的先前设置进行编译)。

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

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