PDA

View Full Version : [Python] "_mysql was already imported" error



unutbu
December 4th, 2008, 04:15 AM
Can you explain what is causing this error?


% cat test.py
#!/usr/bin/env python
import MySQLdb
import matplotlib
% test.py
/usr/lib/python2.5/site-packages/pytz/__init__.py:29: UserWarning: Module _mysql was already imported from /var/lib/python-support/python2.5/_mysql.so, but /var/lib/python-support/python2.5 is being added to sys.path
from pkg_resources import resource_stream

But if I switch the order of the imports, the error goes away:

% cat test.py
#!/usr/bin/env python
import matplotlib
import MySQLdb
% test.py
%

This is occurring on a fresh Intrepid install. I tried googling
"_mysql was already imported" for an answer, but came up empty.



% apt-cache policy python-matplotlib
python-matplotlib:
Installed: 0.98.3-4ubuntu1

% apt-cache policy python-mysqldb
python-mysqldb:
Installed: 1.2.2-7

pp.
December 4th, 2008, 07:47 AM
It would appear that matplotlib imports mySQLdb and that it does it in a manner that a message is issued if mySQLdb already is imported.

If it's an issue, I'd have a look into the initialization of matplotlib, particularly into the method __init__ of the class pytz which is named in the warning message.

unutbu
December 4th, 2008, 08:48 PM
Thanks, pp., but I'm still confused.

If you have python-mysqldb and python-matplotlib installed,
can you reproduce the same error?


As far as I can tell, matplotlib does not import _mysql.
For one thing, python-matplotlib does not list mysql as a dependency.
Isn't it true that packages can be imported repeatedly with no adverse consequence? If so, why should pytz throw a UserWarning about this?
/usr/lib/python2.5/site-packages/pytz has to do with timezones.
Is matplotlib or mysqldb importing pytz? I haven't been able to track down where this is happening.


Thank you for your time and thought. I'd greatly appreciate any additional help you can give.

pp.
December 4th, 2008, 09:25 PM
Perhaps it would be useful to study this file, particularly line 29:

/usr/lib/python2.5/site-packages/pytz/__init__.py:29

unutbu
December 4th, 2008, 11:09 PM
I managed to make the warning go away by editing /usr/lib/python2.5/site-packages/pkg_resources.py:

I changed line 2272:

From:

if fn and normalize_path(fn).startswith(loc):

To:

if fn and (fn.startswith(loc) or normalize_path(fn).startswith(loc)):

Thanks for your help, pp.!

pp.
December 5th, 2008, 07:25 AM
Thanks for your help, pp.!

You're welcome, but I suggest to read this thread again: All I did was to selectively quote the error message to you which you included in your first post. There's a lesson somewhere hidden.