简体   繁体   中英

Can I run a script not from “main”

EDIT: Use question #2 since "main.py" is not a standard, just a naming convention for Pycharm. Changing the scriptpath is what to do.

I've been reading Python Crash Course and I'm confused as to how they are running a file that is not "main.py" as the executable. I don't know what is the best practice for this specific question so I've done 3 things:

1 - "from alien_invasion import alien_script" within the "main.py" file and then run the code specifically from "alien_script"

if __name__ == "alien_invasion.alien_script":
run_game()

2 - change the main scriptpath of Pycharm

"C:\Users\Administrator\PycharmProjects\unit tests\main.py" 
into  
"C:\Users\Administrator\PycharmProjects\unit tests\alien_invasion\alien_script.py
  1. Ignore all of this and just import the code from the module and run it inside main

If anyone has documentation to help I'd greatly appreciate it!

Also, if you are asking for adding multiple classes inside one script, you would do this:

class ClassName:
    variable = "test"

classTest = ClassName()
print(classTest.variable) --> "test"

After doing some digging, I realized I can just change the scriptpath in Pycharm to the specific file. I was thinking "main.py" was a standard for python developers to use but Pycharm just makes a file for you and you can just change it to whatever. Process of learning I guess.

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