Python:两个名称相同的软件包;您如何指定要加载的内容? [英] Python: Two packages with the same name; how do you specify which is loaded?

查看:111
本文介绍了Python:两个名称相同的软件包;您如何指定要加载的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个软件包,它们以相同的名称安装不同的软件包.它们都是包",因为它们具有在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆