I have a custom module in one of the directories in my PYTHONPATH with the same name as one of the standard library modules, so that when I import module_name
, that module gets loaded. If I want to use the original standard library module, is there any way to force Python to import from the standard library rather than from the PYTHONPATH directory, short of renaming the custom module and changing every reference to point to the new name?
views:
145answers:
3
+2
A:
You can select the module you want to import with the imp module:
import imp
mymodule = imp.load_module(name, file, pathname, description)
joaquin
2010-06-01 18:05:03
+1
A:
The ideal solution would be to rename your module to something not in the standard library.
You can also switch absolute imports on if you're on Python 2.5+:
from __future__ import absolute_import
Luper Rouch
2010-06-01 18:08:24
How would I use relative imports to grab the standard library version?
jrdioko
2010-06-01 18:10:09
I misunderstood your question, edited answer.
Luper Rouch
2010-06-01 18:19:51
+1
A:
Don't.
If you have accidentally chosen a standard library module name, change your module name to end the conflict.
S.Lott
2010-06-01 21:15:57