简体   繁体   中英

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; that is, all the statements are executed, everything that is def ed in the imported file gets defined etc. imports get imported, too.

So what's happening is this:

  1. you run

$ python MoneyCounter.py

  1. Python reads MoneyCounter.py, executes its first statement: import Password
  2. Python reads Password.py, and executes its first statement: import MoneyCounter.py
  3. Python reads MoneyCounter.py, this time it encounters import Password , but already has password in its list of known names; so it continues to

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 . In that, def system… hasn't happened yet, so Password.system is still unknown.

Generally, your architecture is questionable . Why would the Password "utility" module call into your "master" business logic? 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 .

On the assumption that MoneyCounter.py is the entry point (the name you run from the command-line), then I suggest you replace this:

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. However, you should reconsider your design.

Edit:

name is a reference to the text name of the current module. 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__ . 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. This way we can provide additional functionality depending on whether we run a module as a program or import it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM