Force import module from Python standard library instead of PYTHONPATH default

人走茶凉 提交于 2019-12-18 19:01:49

问题


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?


回答1:


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



回答2:


Don't.

If you have accidentally chosen a standard library module name, change your module name to end the conflict.




回答3:


You can select the module you want to import with the imp module:

import imp
mymodule = imp.load_module(name, file, pathname, description) 


来源:https://stackoverflow.com/questions/2952045/force-import-module-from-python-standard-library-instead-of-pythonpath-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!