简体   繁体   English

尝试“重新导入”模块时,为什么会遇到ImportError?

[英]Why am I encountering an ImportError when I try to 're-import' a module?

I have a series of python modules written which are held in the same directory and I am having trouble with an ImportError. 我编写了一系列的python模块,这些模块保存在同一目录中,而ImportError遇到了麻烦。

The three modules I am using are draw_menu.py , errors.py and file_operations.py . 我正在使用的三个模块是draw_menu.pyerrors.pyfile_operations.py

In errors.py I requires a list of error codes, I am using a custom method defined in file_operations.py to open a file containing the codes therefore I am using import file_operations just below the she-bang (above the class definition). errors.py我需要一个错误代码列表,我使用在file_operations.py定义的自定义方法来打开一个包含该代码的文件,因此,我在she-bang下方(类定义上方)使用import file_operations

In file_operations.py I use a method defined in error.py to print error messages upon errors (eg file not found etc.). file_operations.py我使用error.py定义的方法来在出现错误(例如,找不到文件等)时显示错误消息。 I therefore import errors in the same way here. 因此,我在这里以相同的方式import errors

The above has been working fine but when I come to using draw_menu.py which uses a file to define the options in an ascii menu (therefore I am using import file_operations ) an ImportError is encountered. 上面的工作正常,但是当我使用draw_menu.py ,该文件使用文件定义ascii菜单中的选项(因此我使用import file_operations ),遇到了ImportError。

ImportError: cannot import name file_operations

I understand that this is because the 'import tree' if you like flows as follows: 我了解这是因为如果您喜欢以下流程,则是“导入树”:

draw_menu <- file_operations <- errors <- file_operations draw_menu < - file_operations < - errors < - file_operations

It is important that each module can be used individually, why is this an issue and how can I overcome this without removing import file_operations from errors.py ? 每个模块可以单独使用是很重要的,这是一个问题,为什么要解决这个问题而又不从errors.py删除import file_operations呢?

Thankyou 谢谢

Tom 汤姆

Circular imports can cause issues in Python (as you might expect). 循环导入可能会在Python中引起问题(您可能会想到)。 It's probably worth checking if: 可能值得检查是否:

A) errors.py and file_operation.py should be single module (if they both rely so heavily on each other, do they need to be separate?) A)errors.py和file_operation.py应该是单个模块(如果它们彼此非常依赖,是否需要分开?)

B) you can delay the import in one or the other module. B)您可以延迟一个或另一个模块中的import An import statement in a function will not run until the function is called, and while it's normally good practice to import at the beginning of a module there is no requirement to in Python. 在函数调用之前,该函数中的import语句将不会运行,虽然通常将其导入模块的开头是一种好习惯,但在Python中并不需要。 In situations like this it can avoid the circular reference during import. 在这种情况下,可以避免在导入过程中使用循环引用。

The problem is not the imports themselves, but the dependencies . 问题不是导入本身,而是依赖项 file_operations cannot be processed until it has imported errors , but errors cannot be processed until it has imported file_operations . 在导入errors之前无法处理file_operations ,但是在导入file_operations之前无法处理errors Python recognises this as an impossible situation, and raises an error. Python认为这是不可能的情况,并引发错误。

The best solution to this is to refactor your files so that you don't have this circular dependency any more. 最好的解决方案是重构文件,以使您不再具有这种循环依赖关系。 If this really isn't possible, the alternative solution is to change one of the modules so that the offending import happens inside the function that needs it, rather than at the top level. 如果这确实不可能,则替代解决方案是更改模块之一,以便有问题的导入发生在需要它的函数内,而不是在顶层。 This means the initial processing of the module isn't dependent on the import, so it will succeed. 这意味着模块的初始处理不依赖于导入,因此它将成功。

Apart from breaking the circular dependency you can try moving the locations of your import calls. 除了打破循环依赖之外,您还可以尝试移动导入调用的位置。 Don't forget that imports in Python are just regular statements, so you can import inside functions for example. 不要忘记,Python中的导入只是常规语句,因此您可以导入内部函数。

The trouble is that import (as a side-effect) will actually run the module being imported (the first time you call import). 问题是导入(作为副作用)实际上将运行正在导入的模块(第一次调用导入)。 So if you are importing a module, which imports the original module things get confused. 因此,如果您要导入一个模块,而该模块又导入了原始模块,那么事情会很混乱。

You may find that you can ease the problem by only importing errors/file_operations at the point you actually need to use it. 您可能会发现,只需在实际需要使用的位置导入错误/ file_operations,就可以缓解该问题。 This could be inside a function. 这可以在函数内部。 So maybe wrap the call to the function in errors: 因此,也许将对函数的调用包装在错误中:


def print_error_message(err):
    from errors import print_error_message as _print_error_message
    _print_error_message(err)

That way you will only import errors after the regular imports have run. 这样,仅在常规导入运行后才导入错误。

暂无
暂无

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

相关问题 重新导入模块“ as” - Re-import module “as” 尝试运行Pyramid时为什么会出现ImportError? - Why am I getting an ImportError when I try to run Pyramid? 当我尝试将mwclient模块用于Python时,为什么会出现错误“无法导入名称扫描仪”? - Why am I getting the error “cannot import name Scanner” when I try to use the mwclient module for Python? 尝试在终端中导入 docx 时为什么会收到此 ImportError? - Why am I getting this ImportError when trying to import docx in the Terminal? 尝试通过解释器运行Python脚本时,为什么会出现“ ImportError:未命名模块”的提示? - Why do I get “ImportError: No module named” when I try to run my Python script via the Interpreter? 重新导入功能定义中的模块或使用Try-除外? - Re-import modules in function definition or using Try - except? 为什么我不能导入 re 模块? - Why can't I import the re module? 当我尝试从熊猫导入DataFrame时出现ImportError - ImportError when I try to import DataFrame from pandas 我尝试导入请求时的ImportError消息字符串 - String of ImportError messages when I try to import requests 如何使python脚本(X)在另一个模块(Y)中重新加载动态更改的变量,然后在同一脚本(X)中重新导入更新的模块(Y)? - How can I make python script(X) reload dynamically changing variables in another module(Y) and then re-import updated module(Y) in same script(X)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM