简体   繁体   English

我可以运行不是来自“main”的脚本吗

[英]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.编辑:使用问题 #2,因为“main.py”不是标准,只是 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.我一直在阅读 Python 速成课程,我很困惑他们如何运行一个不是“main.py”作为可执行文件的文件。 I don't know what is the best practice for this specific question so I've done 3 things:我不知道这个特定问题的最佳做法是什么,所以我做了 3 件事:

1 - "from alien_invasion import alien_script" within the "main.py" file and then run the code specifically from "alien_script" 1 - “main.py”文件中的“from alien_invasion import alien_script”,然后专门从“alien_script”运行代码

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

2 - change the main scriptpath of Pycharm 2 - 更改 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忽略所有这些,只需从模块中导入代码并在 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.经过一番挖掘,我意识到我可以将 Pycharm 中的脚本路径更改为特定文件。 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.我在想“main.py”是 python 开发人员使用的标准,但 Pycharm 只是为您创建一个文件,您可以将其更改为任何文件。 Process of learning I guess.我猜是学习的过程。

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

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