在标准库副本中优先使用pip安装的模块 [英] Favoring a pip-installed module over the standard library copy

查看:138
本文介绍了在标准库副本中优先使用pip安装的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究在Python 2.7中运行ipython和其他各种程序时收到的Module was already imported警告:

I've been digging into a Module was already imported warning I get when I run ipython and various other programs in Python 2.7:

$ ipython
[path to python]/lib/python2.7/site-packages/path.py:122:
UserWarning: Module argparse was already imported from [path to python]/lib/python2.7/argparse.pyc, but 
[path to python]/lib/python2.7/site-packages is being added to sys.path
  import pkg_resources

这是由于argparse被同时包含在标准库中并作为PyPI中的模块.

This stems from argparse being included both in the standard library and as a module from PyPI.

就我而言, stevedore 尚未解决的讨论 ://www.python.org/dev/peps/pep-0496/"rel =" nofollow noreferrer>约束此要求到Python 2.6(没有内置argparse),但是装卸工人只是一个例子:许多软件包都需要argparse,有些甚至甚至需要比标准库中捆绑的版本更高的版本. (例如, remotecv 需要

In my case, the stevedore package required the argparse module be installed. There's unsettled discussion on stevedore's development site about constraining this requirement to Python 2.6 (which didn't have argparse built-in), but stevedore is just an example: many packages require argparse, and some even require a version higher than what's bundled in the standard library. (For example, remotecv requires argparse>=1.2.1,<1.3.0, as of this writing.)

假设您确实想继续使用第三方程序包,那么处理此警告的最Pythonic方法是什么?

What's the most Pythonic way to deal with this warning, assuming you do want to keep using the third-party package?

  • 抑制警告?
  • 是否违反要求卸载了argparse模块?
  • 从标准库中消费argparse吗?
  • 还有别的吗?
  • Suppress the warning?
  • Uninstall the argparse module in defiance of requirements?
  • Excise argparse from your standard library?
  • Something else?

在">如何中,您也有类似的问题正确的模块已经在Python中加载了UserWarnings?"和"模块pytz是已经导入."我将让明智的人群决定这是否是重复项,但我不相信这是重复项.都是在4年前问过的,软件包分发工具已经改变(这与 distribute .egg s),尽管警告是相同的,但我认为它们不是由重叠的stdlib/PyPI问题引起的.

There are similar questions at "How do you correct Module already loaded UserWarnings in Python?" and "Module pytz was already imported." I'll let the wise crowds decide if this is a duplicate, but I don't believe it is. Those were both asked 4+ years ago, the package distribution tools have changed (this isn't about distribute or .eggs), and though the warning is the same, I don't think they were caused by this overlapping stdlib/PyPI issue.

建议对此进行研究的任何人:pip list可能排除 argparse的结果(有些争议).

Heads up to anyone researching this: pip list may exclude argparse from its results (somewhat controversially).

推荐答案

我不能帮助安装先前安装的argparse版本,但是可能无法提供有用的意见.

I can't help with the previously installed argparse versions, but may be able to make few helpful observations.

ipythonargparse的重度用户,尽管以其自己的方式.它从config文件构建一个解析器,并使用该解析器来处理您的命令行.因此,您的命令行在最后一刻有机会覆盖许多comfig选项.

ipython is a heavy user of argparse, though in its own way. It builds a parser from the config files, and uses that to process your commandline. So your commandline has a last minute chance to override many comfig options.

%magic命令使用argparse版本进行解析.

And %magic commands are parsed with a version of argparse.

PyPi版本成为标准版本(Python 2.7)后,便停止了其开发.标准中的版本号是没有意义的.它仍然是(在最新的Python 3.5中).

Development of the PyPi version ceased once it became standard (Python 2.7). Version number in the standard is meaningless. It is still (in the latest Python 3.5).

In [4]: argparse.__version__
Out[4]: '1.1'

我不知道该怎么做,但是任何试图安装argparse的模块都应首先检查Python版本.如果是2.7或更高版本,则应使用标准的argparse.

I don't know how it can be done, but any module that seeks to install argparse should first check the Python version. If it 2.7 or newer, it should use the standard argparse.

请记住,argparse模块是一个自包含文件,argparse.py.以我的经验,ipython可以在我自己当前的工作目录中使用该文件的副本正常工作.在测试补丁中的argparse错误/问题时,我总是这样做.

Keep in mind that the argparse module is one self contained file, argparse.py. In my experience ipython works fine with a copy of that file in my own current working directory. I do that all the time when testing patches for argparse bug/issues.

除了2.6兼容性之外,我想不出一个原因,为什么一个软件包需要argparse的PyPi版本而不是标准版本.是否有任何讨论表明可能出现哪些不兼容?

Other than 2.6 compatibility, I can't think of a reason why a package would want a PyPi version of argparse instead of the standard one. Does any of the discussions indicate what kinds of incompatibilities might arise?

根据当前的PyPi版本(1.4)NEWS.txt,存储库现在为

According the current PyPi version (1.4) NEWS.txt, the repository is now

https://github.com/ThomasWaldmann/argparse

在那里进行探索,以了解比标准版本和标准版本之间存在哪些兼容性问题.

It may be work exploring there to see what compatibility issues there are between than and the standard version(s).

PyPi版本和标准库都具有test_argparse.py单元测试文件.它们可以用来测试兼容性.

Both the PyPi version and the standard library have test_argparse.py unittest files. They could be used to test for compatibilities.

这篇关于在标准库副本中优先使用pip安装的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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