How do you correct Module already loaded UserWarnings in Python?
Asked Answered
T

5

18

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.

Thurman answered 5/10, 2010 at 6:38 Comment(1)
That can happen if you install the same library using two different installation methods (an OS package, and easy_install, f.i).Decato
W
7

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.

Whaling answered 29/1, 2011 at 2:54 Comment(2)
Yeah I practice this as well. I don't remember if I was using virtualenv when I had this issue, but I am pretty sure it's related to this.Thurman
Note that since November 2011, --no-site-packages has been the default.Enzymology
M
4

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.

Mulct answered 4/1, 2011 at 16:19 Comment(1)
That would indeed be the electrical tape fix haha. I am opting for the more solid route of removing distribute and reinstalling, but haven't found a decent method for this yet without botching my setuptools installation.Thurman
B
2

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.

Bioscope answered 21/1, 2013 at 14:55 Comment(0)
K
0

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.

Kuhlman answered 4/4, 2013 at 23:0 Comment(0)
C
0

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')  
Clabo answered 29/4, 2016 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.