Rya*_*eer 5 python setuptools python-2.7 pth python-packaging
我有一套一起开发并捆绑到一个分发包中的软件包。
为了便于讨论,我们假设我有充分的理由按以下方式组织我的 python 发行包:
SpanishInqProject/
|---SpanishInq/
| |- weapons/
| | |- __init__.py
| | |- fear.py
| | |- surprise.py
| |- expectations/
| | |- __init__.py
| | |- noone.py
| |- characters/
| |- __init__.py
| |- biggles.py
| |- cardinal.py
|- tests/
|- setup.py
|- spanish_inq.pth
Run Code Online (Sandbox Code Playgroud)
spanish_inq.pth我已经添加了要添加SpanishInq到 的路径配置文件sys.path,因此我可以直接导入weapons, .etc 。
我希望能够使用 setuptools 构建轮子并在目录中安装 pip install ,但weapons不创建包或命名空间。expectationscharactersSpanishInqSpanishInq
我的设置.py:
from setuptools import setup, find_packages
setup(
name='spanish_inq',
packages=find_packages(),
include_package_data=True,
)
Run Code Online (Sandbox Code Playgroud)
文件包含MANIFEST.in:
spanish_inq.pth
Run Code Online (Sandbox Code Playgroud)
这在几个方面具有挑战性:
pip install将weapons等直接放在site-packages目录中,而不是放在SpanishInq目录中。spanish_inq.pth文件最终位于 sys.exec_prefix 目录中,而不是我的 site-packages 目录中,这意味着其中的相对路径现在毫无用处。我能够通过将SpanishInq变成一个模块来解决第一个问题(我对此并不满意),但我仍然希望能够在weapons没有SpanishInq作为命名空间的情况下导入等,为此我需要SpanishInq添加到 sys.path 中,这是我希望该.pth文件能有所帮助的地方......但我无法让它到达它应该去的地方。
所以...
如何将.pth文件安装到site-packages目录中?
这与 setup.py 非常相似:只安装一个 pth 文件?(就功能而言,这个问题严格来说是一个超集)——我已经在下面调整了我的答案的相关部分。
这里正确的做法是扩展 setuptools' build_py,并将 pth 文件复制到构建目录中的目录中,在 setuptools 准备进入站点包的所有文件的位置中。
from setuptools.command.build_py import build_py
class build_py_with_pth_file(build_py):
"""Include the .pth file for this project, in the generated wheel."""
def run(self):
super().run()
destination_in_wheel = "spanish_inq.pth"
location_in_source_tree = "spanish_inq.pth"
outfile = os.path.join(self.build_lib, destination_in_wheel)
self.copy_file(location_in_source_tree, outfile, preserve_mode=0)
setup(
...,
cmdclass={"build_py": build_py_with_pth_file},
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2559 次 |
| 最近记录: |