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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Don't.
If you have accidentally chosen a standard library module name, change your module name to end the conflict.
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+:
The best way to do that is to put your project in one directory let's say "myproject" that resides on:
/home/me/myproject
Make a symbolic link to myproject directory to one of the default python paths, for example:
# ln -s /home/me/myproject /usr/local/lib/python2.7/dist-packages/myproject
Now when you want to import a module within your project just open a python interpreters.
>>> from myproject.mymodule import *
It will work like a charm.
One last thing, don't name your project the same as the ones found in the python path, so you won't have issues while importing.
You can select the module you want to import with the imp module: