检查是否已在“可编辑"(egg-link)模式下安装了 python 包? [英] Check whether a python package has been installed in 'editable' (egg-link) mode or not?
问题描述
有没有什么办法可以检查Python包是否正常安装(pip install
/setup.py install
)或可编辑/egg-link模式(<代码>pip install -e/setup.py develop
)?
Is there any way to check whether a Python package has been installed normally (pip install
/ setup.py install
) or in editable/egg-link mode (pip install -e
/ setup.py develop
)?
我知道我可以检查包的路径是否包含 site-packages
这很可能意味着它是一个不可编辑"的安装,但这感觉非常脏,我宁愿避免这种情况.
I know I could check whether the path to the package contains site-packages
which would most likely mean it's a "non-editable" install, but this feels extremely dirty and I would rather avoid this.
我尝试检查的原因是我的应用程序正在检查各个位置的配置文件,例如 /etc/myapp.conf
和 ~/.myapp.conf代码>.对于开发人员,我想签入
<pkgdir>/myapp.conf
但由于我显示了可能的位置列表,以防找不到配置,我真的不想包含 pkgdir包已安装到站点包时的选项(因为用户不应在其中创建配置文件).
The reason I'm trying to check this is that my application is checking for config files in various places, such as /etc/myapp.conf
and ~/.myapp.conf
. For developers I'd like to check in <pkgdir>/myapp.conf
but since I show the list of possible locations in case no config was found, I really don't want to include the pkgdir option when the package has been installed to site-packages (since users should not create a config file in there).
推荐答案
pip
包含 code(它被 pip freeze
用来在行前加上 -e
).由于pip
的API不能保证稳定,所以最好把代码复制到自己的应用中,而不是从pip
中导入:
pip
contains code for this (it's used by pip freeze
to prefix the line with -e
). Since pip
's API is not guaranteed to be stable, it's best to copy the code into the own application instead of importing it from pip
:
def dist_is_editable(dist):
"""Is distribution an editable install?"""
for path_item in sys.path:
egg_link = os.path.join(path_item, dist.project_name + '.egg-link')
if os.path.isfile(egg_link):
return True
return False
该代码已获得 MIT 许可,因此可以安全地复制和粘贴到几乎任何项目中.
The code is MIT-licensed so it should be safe to copy&paste into pretty much any project.
这篇关于检查是否已在“可编辑"(egg-link)模式下安装了 python 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!