gpt4 book ai didi

python - 如何使用文件扩展 sys.path?

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:35 25 4
gpt4 key购买 nike

我使用的是没有 setuptools 的 python 环境 (iOS Pythonista),因此 python 包中的 setup.py 将无法运行。有一个 site-packages 目录被添加到 sys.path 中。我正在寻找一种聪明的方法来将包从 pypi 压缩到站点包中,而不必将带有 __init__.py 的实际包移动到站点包中。我想保留包含站点包中所有文档的文件夹。有没有办法使用 __init__.py 或另一个 python 保留文件来扩展 sys.path?

简化的 pypi 包结构:

MyPackage-1.10.0/    mypackage/        __init__.py        somethinga.py    setup.py

当前方式:将 mypackage 移动到 site-packages 中,丢弃其余部分。

或者当站点包被添加到 sys.path 时,有没有办法使用像 site-packages/__init__.py 这样的东西来为包文件夹添加 sys.path?

最佳答案

您可以再创建一个矿石.pth site-packages 中的文件;它们的内容将添加到 sys.path在场时。

来自 site module documentation :

A path configuration file is a file whose name has the form <em>name</em>.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.

使用它将包目录的完整或相对路径添加到 sys.path .

例如,对于您的 MyPackage-1.10.0包,你只需要一个 MyPackage-1.10.0.pth文件内容 MyPackage-1.10.0单独一行,让 Python 将完整的目录路径添加到 sys.path .

关于python - 如何使用文件扩展 sys.path?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27696627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com