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
This stems from argparse
being included both in the standard library and as a module from PyPI.
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.)
What's the most Pythonic way to deal with this warning, assuming you do want to keep using the third-party package?
- Suppress the warning?
- Uninstall the
argparse
module in defiance of requirements? - Excise
argparse
from your standard library? - Something else?
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 .egg
s), and though the warning is the same, I don't think they were caused by this overlapping stdlib/PyPI issue.
Heads up to anyone researching this: pip list
may exclude argparse
from its results (somewhat controversially).