简体   繁体   English

如何导入与系统模块同名的用户模块?

[英]How to import a user module which has the same name with a system module?

I have a legacy python program which defined a module whose name conflict with a system module in the current python, what's the easiest method to fix this? 我有一个旧版python程序,该程序定义了一个模块,该模块的名称与当前python中的系统模块冲突,最简单的方法是解决此问题? This module has been imported in many place, so it would be great if no need to change the the old python source. 该模块已在很多地方导入,因此,如果不需要更改旧的python源代码,那就太好了。

You could create a custom import routine using the imp module . 您可以使用imp module创建自定义导入例程。 For example, here I'm importing a module named string which shadows the real string module: 例如,在这里我要导入一个名为string的模块,该模块会遮盖真实的字符串模块:

import imp

def user_import(name, path):
    mod_file, mod_path, mod_desc = imp.find_module(name, [path])
    return imp.load_module(name, mod_file, mod_path, mod_desc)

# load the real 'string' module
import string
print string.digits

# load the user 'string' module which lives under the path './foo'
mod = user_import('string', './foo')
print mod.digits

Output: 输出:

0123456789
thesearenotdigits

You can always rename a module when you import it, such as: 导入模块时,您始终可以重命名模块,例如:

import sys as realsys 将sys导入为realsys

realsys.exit(1) realsys.exit(1)

It seems like you'll have to do this either for the system module whose name you have overloaded or for your module, depending on which causes the least pain. 似乎您必须对名称已重载的系统模块或模块进行此操作,具体取决于哪个模块导致的痛苦最少。

Bottom line, when you create modules, it's good to more fully qualify the name, eg instead of creating sys (in this example), you could have created mylib.sys . 最重要的是,创建模块时,最好完全限定名称,例如,可以创建mylib.sys而不是创建sys (在此示例中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何导入与子包具有相同名称的模块? - Python: how to import a module which has the same name as a subpackage? 如何从与保留的 function 或 Python 中的关键字同名的模块中成功导入某些内容? - How do I successfully import something from a module which has the same name as a reserved function or keyword in Python? 导入与系统模块具有相同名称的模块 - Importing a module which have the same name with a system module 与标准库中的模块同名的模块,并且还需要导入相同的模块 - A module that has the same name as a module in standard library and also need to import the same module 当模块名称中包含“-”破折号或连字符时如何导入模块? - How to import module when module name has a '-' dash or hyphen in it? 如何导入一个模块,该模块在与模块相同的目录中打开文件? - How to import a module which opens a file in the same directory as the module? 如何使用现有的python模块名称创建自定义模块并将该系统模块导入自定义模块 - How to create custom module with existing python module name and import that system module in the custom module Python-导入模块的文件名称不同? - Python - Import module which has a different name to file? 如何导入站点包的模块而不是当前目录中具有相同名称的模块(python 3.x)? - How to import site package's module instead of one in current directory that has the same name (python 3.x)? 如何导入与带有子包和模块的纯python包同名的python扩展模块? - How to import python extension module that has the same name as a pure python package with subpackages and modules?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM