如何在Python中更正模块已加载的UserWarnings?
在命令行中运行大多数Python脚本时收到以下类型的警告:
/Library/Python/2.6/site-packages/virtualenvwrapper/hook_loader.py:16: UserWarning: Module
pkg_resources was already imported from /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.pyc, but /Library/Python/2.6/site-packages is being added to sys.path
import pkg_resources
/Library/Python/2.6/site-packages/virtualenvwrapper/hook_loader.py:16: UserWarning: Module site was already imported from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site.pyc, but /Library/Python/2.6/site-packages is being added to sys.path
import pkg_resources
我认为这与使用distribute和virtualenv的组合有关,但想检查是否有其他人遇到过这个问题或者知道如何处理修复它。
Getting the following kinds of warnings when running most python scripts in the command line:
/Library/Python/2.6/site-packages/virtualenvwrapper/hook_loader.py:16: UserWarning: Module
pkg_resources was already imported from /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.pyc, but /Library/Python/2.6/site-packages is being added to sys.path
import pkg_resources
/Library/Python/2.6/site-packages/virtualenvwrapper/hook_loader.py:16: UserWarning: Module site was already imported from /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site.pyc, but /Library/Python/2.6/site-packages is being added to sys.path
import pkg_resources
I think it has to do with a combination of using distribute and virtualenv, but wanted to check if anyone else has run in to this or would know how to go about fixing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许使用 virtualenv 选项
--no-site-packages
这样您就不会在虚拟环境中看到任何系统站点包。在 virtualenv 和系统根目录中同时安装项目可能是导致此问题的原因。创建 virtualenv 时使用
--no-site-packages
可以防止系统包之间发生任何冲突。在创建新的 virtualenv 时,我几乎总是使用该选项来防止任何冲突。虽然我可能有多个库的副本,但至少它们不会互相干扰。Perhaps use the virtualenv option
--no-site-packages
so you won't see any system site-packages within your virtual environment. Having items installed both in your virtualenv and on the system root may be the cause of this issue.Using
--no-site-packages
when creating your virtualenv prevents any conflict between system packages. I almost always use that option when creating a new virtualenv to prevent any conflicts. Though I may have several copies of libraries, at least they don't mess with each other.在 python 中,相当于在检查引擎灯上贴上一点电工胶带,即使用
-W
命令行标志或添加 警告过滤器。The python equivalent of putting a bit of electrical tape over the check engine light would be to use the
-W
command line flag or by adding a warning filter.就我而言,重新安装任何东西都没有帮助。 /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python 中留下了一些孤立的 .pyc 文件(特别是 pkg_resources.pyc)
使其工作。 此链接帮助我找到了问题。
In my case reinstalling of anything did not help. There were some orphaned .pyc files (specifically pkg_resources.pyc) left in /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
made it work. This link helped me to track down the problem.
我今天也经历了这种 Python 打包地狱之旅。
在 Ubuntu 上运行 Python 2.7.3,使用命名空间包并使用 zc.buildout。
最后,将系统范围内的分发从旧版本 0.6.30 更新到最新版本 0.6.35 解决了该问题。
I had this sort of Python packaging hell visit today too.
Running Python 2.7.3 on Ubuntu, using namespace packages and using zc.buildout.
Finally, updating system wide Distribute from older version 0.6.30 to latest version 0.6.35 resolved the problem.
如果您正在修改的程序中显示警告,请尝试以下方式(例如使用 pytz):
If the warning shows in a program you are modifying, try it this way (examply with pytz):