如何将多个.py文件编译成一个exe?
我构建了一个简单的项目,总共包含 4 个 .py 文件。它可以使用 pycharm 运行,但当我尝试将其作为 exe 运行时失败。在主文件中,我使用 subprocess.Popen 打开这些文件,并使用 sys.exit() 退出主脚本。这是 main.py 文件中的内容:
from subprocess import Popen
import sys
def none_contr_run():
Popen(["python", "none_contr.py"])
sys.exit()
def div_rein_run():
Popen(["python", "div_rein.py"])
sys.exit()
def stable_annual_run():
Popen(["python", "stable_annual.py"])
sys.exit()
并且在每个文件中都有一个像这样的返回函数:
def return_main():
Popen(['python','main.py'])
sys.exit()
当我尝试将此程序作为 exe 运行时,main.py 中的内容工作正常,但脚本关闭一旦我调用上面的函数之一,它就会自动运行。什么可以解决这个问题?
I've built a simple project containing 4 .py files in total. It works using pycharm but fails when I try to run it as an exe. In the main file, I use subprocess.Popen to open these files and sys.exit() to exit out of the main script. This is what is in the main.py file:
from subprocess import Popen
import sys
def none_contr_run():
Popen(["python", "none_contr.py"])
sys.exit()
def div_rein_run():
Popen(["python", "div_rein.py"])
sys.exit()
def stable_annual_run():
Popen(["python", "stable_annual.py"])
sys.exit()
and in each of the files there is a return function like this:
def return_main():
Popen(['python','main.py'])
sys.exit()
When I try to run this program as an exe, what is inside of main.py works fine, but the script closes itself as soon as I call one of the functions above. What could be a solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Pyinstaller 创建了 .exe 文件,它将存储在 dist 路径中。您唯一要做的就是将其移至主文件中,问题就会得到解决。
这是由于路径差异造成的。
If you have created .exe file using Pyinstaller it will be stored in dist path. The only thing you have to do is move it into your main files and the problem will be resolved.
It was due to the path difference.