简体   繁体   English

如何在程序中实现控制功能并避免AttributeError:'module'对象没有属性错误

[英]How can I implement a control function in my program and avoid the AttributeError: 'module' object has no attribute error

I have a simlair problem as in this question: AttributeError: 'module' object has no attribute 我在这个问题中有一个simlair问题: AttributeError:'module'对象没有属性

I'm doing a mutual top level import which is causing me to have AttributeError: 'module' object has no attribute error so but I'm not sure if I can import within a function because I'm basically importing a file that controls all the other modules. 我正在做一个共同的顶级导入,这导致我有AttributeError:'module'对象没有属性错误所以但我不确定我是否可以导入函数,因为我基本上导入一个控制所有的文件其他模块。

Here's an example(file2.py): 这是一个例子(file2.py):

import file1
count = 0
while count < file1.number_of_loops:
 .....

My functions are being triggered by the control variables so I'm unable to import within the function itself. 我的函数是由控制变量触发的,所以我无法在函数本身内导入。 Short of creating a sperate file to control, is there a way to refer to variables in another file without doing a mutual top level import? 如果没有创建要控制的sperate文件,有没有办法在不进行共同顶级导入的情况下引用另一个文件中的变量?

As pointed out by a comment, if you're using file1.py to store some configuration variables, then it would be better to have a look at alternative ways of dealing with that configuration. 正如评论所指出的,如果你使用file1.py来存储一些配置变量,那么最好先看看处理该配置的其他方法。

In my opinion, a simple way to address your problem would be to move all those variables to another module called, for example, settings.py so that you can get the configuration from there without having to use any mutual import. 在我看来,解决问题的一个简单方法是将所有这些变量移动到另一个名为settings.py模块,以便您可以从那里获取配置,而无需使用任何相互导入。

I will advice against an import from a module that controls "all the other modules" . 我将建议不要从控制“所有其他模块”的模块导入

My suggestion is to put all that control code inside functions (or classes), and then import the functions and the paramenters where you need to execute them. 我的建议是将所有控制代码放在函数(或类)中,然后导入需要执行它们的函数和参数。

For example, file2.py : 例如, file2.py

def loop_control(num_loops):
    count = 0
    while count < num_loops:
        # ...

another file: 另一个文件:

import file1
import file2

file2.loop_control(file.number_of_loops)

If you need to communicate back and forth the results of this controls you may consider building a Manager just for that (implemented in the way that you prefer). 如果您需要来回沟通此控件的结果,您可以考虑为此构建一个Manager(以您喜欢的方式实现)。

暂无
暂无

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

相关问题 我得到了AttributeError:当我在Windows上运行我的程序时,&#39;module&#39;对象没有属性&#39;fork&#39;。 我怎样才能解决这个问题? - I'm getting AttributeError: 'module' object has no attribute 'fork' when i run my program on windows. How can i fix this? AttributeError: 'function' object 没有属性 'read' — 我该如何修复? - AttributeError: 'function' object has no attribute 'read' — HOW CAN I FIX? AttributeError: 'function' object 没有属性 'split'。 我该如何解决? - AttributeError: 'function' object has no attribute 'split'. How I can fix it? 如何修复错误 AttributeError: 'function' object has no attribute 'str' - How can I fix the error AttributeError: 'function' object has no attribute 'str' 如何解决名为AttributeError的python程序中的错误:&#39;module&#39;对象没有属性&#39;TensorFlowLinearClassifier&#39; - How to solve error in python program named: AttributeError: 'module' object has no attribute 'TensorFlowLinearClassifier' 为什么我会收到错误AttributeError:'module'对象在我的SMS应用程序中没有与Twilio接口的属性'Response'? - Why do I get the error AttributeError: 'module' object has no attribute 'Response' in my SMS app that interfaces with Twilio? 如何修复 AttributeError: &#39;module&#39; 对象没有属性 &#39;function&#39;? - How to fix AttributeError: 'module' object has no attribute 'function'? 如何解决 Pandas 的属性错误:“AttributeError: module 'pandas' has no attribute 'StringDtype'”? - How can I solve attribute error for Pandas : “AttributeError: module 'pandas' has no attribute 'StringDtype'”? AttributeError:&#39;模块&#39;对象没有属性&#39;Popen&#39;错误 - AttributeError: 'module' object has no attribute 'Popen' error Python错误:AttributeError:&#39;module&#39;对象没有属性 - Python error: AttributeError: 'module' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM