我有一套软件包,它们一起开发并捆绑到一个分发包中。
为了便于讨论,让我们假设我有很好的理由以如下方式组织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我添加了路径配置文件spanish_inq.pth以将SpanishInq添加到sys.path中,这样我就可以直接导入weapons、.etc。
我希望能够使用setuptools来构建轮子,并在weapons目录中安装pip、expectations和characters,但不让SpanishInq成为包或名称空间。
我的setup.py:
from setuptools import setup, find_packages
setup(
name='spanish_inq',
packages=find_packages(),
include_package_data=True,
)包含以下内容的MANIFEST.in文件:
spanish_inq.pth这在以下几方面具有挑战性:
pip install直接将weapons等放在site-packages目录中,而不是在SpanishInq目录中。spanish_inq.pth文件最终出现在sys.exec_prefix dir中,而不是在我的站点包dir中,这意味着它中的相对路径现在是无用的。我能够解决的第一个问题是将SpanishInq转换成一个模块(我对此不满意),但我仍然希望能够在不使用SpanishInq作为命名空间的情况下导入weapons等,为此,我需要将SpanishInq添加到sys.path中,这正是我希望.pth文件能够到达它应该去的地方的地方。
所以..。
如何将.pth文件安装到site-packages dir中?
发布于 2022-02-16 07:30:47
这与setup.py: installing just a pth file?非常相似(就功能而言,这个问题严格来说是一个超集) --我已经修改了下面的答案的相关部分。
这里正确的做法是扩展setuptools的build_py,并将pth文件复制到构建目录中,即setuptools准备进入站点包的所有文件的位置。
from setuptools.commands 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},
)https://stackoverflow.com/questions/51032759
复制相似问题