如何在Python中更正模块已加载的UserWarnings?

发布于 2024-09-26 02:41:12 字数 741 浏览 3 评论 0原文

在命令行中运行大多数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-10-03 02:41:12

也许使用 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.

人间不值得 2024-10-03 02:41:12

在 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.

◇流星雨 2024-10-03 02:41:12

就我而言,重新安装任何东西都没有帮助。 /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python 中留下了一些孤立的 .pyc 文件(特别是 pkg_resources.pyc)

sudo find . -type f -name "*.pyc" -delete

使其工作。 此链接帮助我找到了问题。

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

sudo find . -type f -name "*.pyc" -delete

made it work. This link helped me to track down the problem.

夜空下最亮的亮点 2024-10-03 02:41:12

我今天也经历了这种 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.

蓝颜夕 2024-10-03 02:41:12

如果您正在修改的程序中显示警告,请尝试以下方式(例如使用 pytz):

try:  
    import pytz  
except ImportError:  
    from pkg_resources import require  
    require('pytz')  

If the warning shows in a program you are modifying, try it this way (examply with pytz):

try:  
    import pytz  
except ImportError:  
    from pkg_resources import require  
    require('pytz')  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文