Python:两个名称相同的软件包;您如何指定要加载的内容? [英] Python: Two packages with the same name; how do you specify which is loaded?
问题描述
我有两个软件包,它们以相同的名称安装不同的软件包.它们都是包",因为它们具有在setup
命令中指定package=['foo']
的顶级setup.py
文件.
I have two packages that install different packages with the same name. They are both "packages" in that they have top-level setup.py
files which specify package=['foo']
in the setup
command.
如果我使用distutils.core
安装,最后安装的将覆盖前一个(但我认为除非.py文件都具有相同的名称,否则不会覆盖).如果我使用setuptools
安装,则两个软件包将安装到不同的鸡蛋中.
If I install using distutils.core
, the last to be installed overwrites the previous one (but I think wouldn't overwrite unless the .py files all had the same names?). If I install using setuptools
, the two packages get installed into different eggs.
一种选择是在导入软件包名称之前显式设置sys.path
;这似乎是非pythonic"的,而且很脏.
One option would be to explicitly set sys.path
before importing the package name; this seems "un-pythonic" and rather dirty.
假设我在setuptools的不同eggs
中安装了这两个名称相同的软件包,如何指定要导入的软件包?
Assuming I have these two identically named packages installed in different eggs
from setuptools, how do I specify which is imported?
推荐答案
设置工具指南提到了-multi-version ( -m )开关,该开关从 sys.path 完全.您必须尽早在代码中使用 pkg_resources.require('package == version')来修复 sys.path .此建议是 easy_install 在使用 -m 时始终显示的内容.
Setuptools guide mentions --multi-version (-m) switch that removes package from sys.path completely. You have to use pkg_resources.require('package==version') in your code as early as possible to let it fix sys.path. This advice is what easy_install always prints when one uses -m.
But you can't have both imported at once (unless they're designed to do so using namespace packages).
这篇关于Python:两个名称相同的软件包;您如何指定要加载的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!