简体   繁体   English

如何在python 3中将参数发送到另一个文件

[英]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: 所以这是怎么回事:

  1. you run 你跑

$ python MoneyCounter.py

  1. Python reads MoneyCounter.py, executes its first statement: import Password Python读取MoneyCounter.py,执行其第一条语句: import Password
  2. Python reads Password.py, and executes its first statement: import MoneyCounter.py Python读取Password.py,并执行其第一条语句: import MoneyCounter.py
  3. Python reads MoneyCounter.py, this time it encounters import Password , but already has password in its list of known names; Python读取MoneyCounter.py,这次它遇到import Password ,但是在已知名称列表中已经有密码; so it continues to 所以它继续

enter=False; Password.system(enter) enter=False; Password.system(enter) . enter=False; Password.system(enter)

  1. Now, Python already has a Password in its name lookup dictionary: the half-imported Password . 现在,Python的名称查找字典中已经有一个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.

相关问题 如何使用 Python 套接字中的第三个参数发送文件? - How to send file using third argument in Python Socket? 如何将 Python 文件中的值发送到另一个 Python 文件 - How to send a value in a Python file to another Python file Python /套接字:如何将文件发送到其他网络上的另一台计算机? - Python/socket: How to send a file to another computer which is on a different network? 如何将数据从一个python脚本文件发送到pygtk中的另一个 - how to send data from one python script file to another in pygtk 如何在python中执行代码以将终端发送到另一个文件 - How to execute code in python to send the terminal to another file 如何将存储在函数中的变量值发送到另一个 Python 文件? - How to send values of variables stored in a function to another Python file? 如何将字典作为参数从 make 文件发送到 python 文件? - How do I send a dictionary as an argument to python file from a make file? 使用另一个 Python 文件中的参数调用 function - Call function with argument in another Python file 如何调用另一个Python文件中需要命令行参数的python文件? - How do you call a python file that requires a command line argument from within another python file? 如何使用另一个 python 脚本文件中的参数执行 python 脚本文件 - How to execute a python script file with an argument from inside another python script file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM