问题描述
我的Python C Extension项目获得了以下文件结构:
I got the following file structure for my Python C Extension project:
.
├── setup.py
├── source
├── cppimplementation
│ └── fastfile.cpp
└── fastfilepackage
├── __init__.py
└── version.py
我使用以下setup.py
文件:
from setuptools import setup, Extension
setup(
name= 'fastfilepackage',
version= '0.1.1',
package_dir = {
'': 'source',
},
packages = [
'fastfilepackage',
],
ext_modules= [
Extension(
'fastfilepackage',
[
'source/cppimplementation/fastfile.cpp',
]
)
],
)
我通过以下方式安装它们:
I install them with:
$ pip3 --version
pip 19.1.1 (python 3.6)
$ python3 --version
Python 3.6.7
$ pip3 list
Package Version
---------------------- -------------
wheel 0.33.1
setuptools 40.8.0
...
fastfilepackage$ pip3 install .
问题在于,当我安装它时,我的Python C Extension模块被fastfilepackage/version.py
和fastfilepackage/__init__.py
覆盖,即,安装后,我得到了以下信息:
The problem is that when I install it, my Python C Extension module is overridden by fastfilepackage/version.py
and fastfilepackage/__init__.py
, i.e, after installing it, I got the following:
import fastfilepackage
print( dir( fastfilepackage ) )
# prints ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
# '__name__', '__package__', '__path__', '__spec__', '__version__',
# 'version']
即,没有source/cppimplementation/fastfile.cpp
导出的FastFile类,但是它具有fastfilepackage/version.py
和fastfilepackage/__init__.py
文件.
i.e., no FastFile class exported by source/cppimplementation/fastfile.cpp
, but it has the fastfilepackage/version.py
and fastfilepackage/__init__.py
files.
这是已安装的文件结构:
This is the installed file structure:
.
└── dist-packages
├── fastfilepackage
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ └── version.cpython-36.pyc
│ └── version.py
├── fastfilepackage-0.1.1.dist-info
│ ├── INSTALLER
│ ├── LICENSE.txt
│ ├── METADATA
│ ├── RECORD
│ ├── top_level.txt
│ └── WHEEL
└── fastfilepackage.cpython-36m-x86_64-linux-gnu.so
但是,如果我从安装文件中删除了package_dir = { '': 'source', },
和packages = [ 'fastfilepackage', ],
行,则说明我的Python C Extension模块已正确安装:
But if I remove the lines package_dir = { '': 'source', },
and packages = [ 'fastfilepackage', ],
from my setup file, then, my Python C Extension module is correctly installed:
import fastfilepackage
print( dir( fastfilepackage ) )
# prints ['FastFile', '__doc__', '__file__', '__loader__', '__name__',
# '__package__', '__spec__']
即,它具有source/cppimplementation/fastfile.cpp
导出的FastFile类,但没有fastfilepackage/version.py
和fastfilepackage/__init__.py
文件.
i.e., it has the FastFile class exported by source/cppimplementation/fastfile.cpp
, but it does not have the fastfilepackage/version.py
and fastfilepackage/__init__.py
files.
这是已安装的文件结构:
This was the installed file structure:
.
└── dist-packages
├── fastfilepackage-0.1.1.dist-info
│ ├── INSTALLER
│ ├── LICENSE.txt
│ ├── METADATA
│ ├── RECORD
│ ├── top_level.txt
│ └── WHEEL
└── fastfilepackage.cpython-36m-x86_64-linux-gnu.so
如何在setup.py
下放置ext_modules
和packages
以使用相同的程序包名称,而又不覆盖另一个?
How can I put ext_modules
and packages
to use the same package name under my setup.py
without one overriding the other?
推荐答案
您不能.第一个进口胜利.您不能具有相同名称的脚本/模块/软件包/扩展名-一个覆盖所有其他名称.
You cannot. The first one imported wins. You cannot have scripts/modules/packages/extensions with the same name — one overrides all others.
但是您可以将一个人带入另一个人.将扩展名命名为fastfilepackage.fastfilepackage
,您可以import fastfilepackage
导入Python包,并import fastfilepackage.fastfilepackage
导入扩展;或from fastfilepackage import fastfilepackage
.
But you can to have one inside another. Make your extension named fastfilepackage.fastfilepackage
and you can import fastfilepackage
to import the Python package and import fastfilepackage.fastfilepackage
to import the extension; or from fastfilepackage import fastfilepackage
.
这篇关于如何使用具有相同名称的setuptools软件包和ext_modules?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!