如何将多个.py文件编译成一个exe?

发布于 2025-01-15 15:28:03 字数 646 浏览 3 评论 0原文

我构建了一个简单的项目,总共包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-22 15:28:03

如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文