首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用.pth文件进行Python打包

用.pth文件进行Python打包
EN

Stack Overflow用户
提问于 2018-06-25 22:51:52
回答 1查看 1.8K关注 0票数 5

我有一套软件包,它们一起开发并捆绑到一个分发包中。

为了便于讨论,让我们假设我有很好的理由以如下方式组织python发行包:

代码语言:javascript
复制
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、expectationscharacters,但不让SpanishInq成为包或名称空间。

我的setup.py:

代码语言:javascript
复制
  from setuptools import setup, find_packages

  setup(
    name='spanish_inq',
    packages=find_packages(),
    include_package_data=True,       
   )

包含以下内容的MANIFEST.in文件:

代码语言:javascript
复制
   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中?

EN

回答 1

Stack Overflow用户

发布于 2022-02-16 07:30:47

这与setup.py: installing just a pth file?非常相似(就功能而言,这个问题严格来说是一个超集) --我已经修改了下面的答案的相关部分。

这里正确的做法是扩展setuptools的build_py,并将pth文件复制到构建目录中,即setuptools准备进入站点包的所有文件的位置。

代码语言:javascript
复制
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},
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51032759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档