[英]How to send an argument to another file in python 3
So, I have been looking at other threads on this topic but they do not use the Module version of passing the argument to the other file, if so wasn't explained clearly enough for me to understand. 因此,我一直在研究有关该主题的其他线程,但是如果没有足够清晰地解释我无法理解,则它们不使用将参数传递给另一个文件的模块版本。
I have tried just to show I'm not just asking: 我只是试图表明我不只是在问:
#MoneyCounter.py
import Password
enter = False
Password.system(enter)
def start(check):
if check == True:
main()
elif check == False:
print("Critical Error occured")
exit()
And my other file 还有我的其他档案
#Password.py
import MoneyCounter
def system(check):
if check == False:
password() #This goes to password def not mentioned in this code
elif check == True:
MoneyCounter.start(check)
The error I am getting is the Module Password has no attribute system 我收到的错误是模块密码没有属性系统
The error I am getting is the Module Password has no attribute system
我收到的错误是模块密码没有属性系统
Of course it doesn't. 当然不是。 The definition doesn't exist by the time the line of code is executed, since execution of the first file got interrupted by the import.
在执行代码行时该定义不存在,因为第一个文件的执行被导入中断。
Either refactor or reorder your code so that the name isn't accessed until it exists, or remove the requirement that each module has for the other. 可以对代码进行重构或重新排序,以便在名称存在之前不能访问该名称,或者删除每个模块对另一个模块的要求。
Your problem here is circular dependency/imports. 您的问题是循环依赖/导入。
An import
statement really executes the code in the imported file; import
语句实际上执行导入文件中的代码。 that is, all the statements are executed, everything that is def
ed in the imported file gets defined etc. imports
get imported, too. 也就是说,所有的语句执行,即一切
def
导入文件编被定义等imports
获得进口了。
So what's happening is this: 所以这是怎么回事:
$ python MoneyCounter.py
import Password
import Password
import MoneyCounter.py
import MoneyCounter.py
import Password
, but already has password in its list of known names; import Password
,但是在已知名称列表中已经有密码; so it continues to enter=False; Password.system(enter)
enter=False; Password.system(enter)
. enter=False; Password.system(enter)
。
Password
in its name lookup dictionary: the half-imported Password
. Password
:半导入的Password
。 In that, def system…
hasn't happened yet, so Password.system
is still unknown. def system…
尚未发生,因此Password.system
仍然是未知的。 Generally, your architecture is questionable . 通常,您的体系结构是有问题的 。 Why would the
Password
"utility" module call into your "master" business logic? 为什么
Password
“实用程序”模块会调用您的“主”业务逻辑? Better not do that, but write code that actually checks the return value of Password.system
and acts based on that in your MoneyCounter.py
. 最好不要这样做,而是编写代码来实际检查
Password.system
的返回值,并根据MoneyCounter.py
中的MoneyCounter.py
。
On the assumption that MoneyCounter.py
is the entry point (the name you run from the command-line), then I suggest you replace this: 假设
MoneyCounter.py
是入口点(您从命令行运行的名称),那么我建议您替换为:
enter = False
Password.system(enter)
with this: 有了这个:
if __name__ == "__main__":
enter = False
Password.system(enter)
That will only be executed from the entry-point (which is called __main__
) and not when it is imported. 那只会从入口点(称为
__main__
)执行,而不是在导入时执行。 However, you should reconsider your design. 但是,您应该重新考虑您的设计。
Edit: 编辑:
name
is a reference to the text name of the current module. name
是对当前模块的文本名称的引用。 With modules that are explicitly imported the name is taken from the filename, but the entry-point (where the program starts) is always called __main__
. 对于显式导入的模块,其名称取自文件名,但是入口点(程序开始的地方)始终称为
__main__
。 So we can always test if we are running as an imported module or as an entry-point. 因此,我们始终可以测试是作为导入模块还是作为入口点运行。
This test is extremely common in python. 该测试在python中极为常见。 This way we can provide additional functionality depending on whether we run a module as a program or
import
it. 通过这种方式,我们可以提供附加功能,具体取决于我们将模块作为程序运行还是
import
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.