Python 如何从 PyCharm 项目到我的“exe”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19071910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:44:34  来源:igfitidea点击:

How to my "exe" from PyCharm project

pythonexepycharm

提问by Anton Bondar

Writing some project on Python via PyCharm. I want to get an exe file from it. I've tried to "Save as->XXX.exe"- but ,when i'm trying to execute it there is an error "file is not supported with such kind of OS"p.s. i've got win7 x64,it doesn't work on x32 too.

通过 PyCharm 在 Python 上编写一些项目。我想从中获取一个exe文件。我试过“另存为-> XXX.exe”-但是,当我尝试执行它时出现错误“此类操作系统不支持文件”ps我有win7 x64,它没有也不适用于 x32。

回答by Michael0x2a

You cannot directly save a Python file as an exe and expect it to work -- the computer cannot automatically understand whatever code you happened to type in a text file. Instead, you need to use another program to transform your Python code into an exe.

您不能直接将 Python 文件保存为 exe 并期望它能够工作——计算机无法自动理解您在文本文件中输入的任何代码。相反,您需要使用另一个程序将您的 Python 代码转换为 exe。

I recommend using a program like Pyinstaller. It essentially takes the Python interpreter and bundles it with your script to turn it into a standalone exe that can be run on arbitrary computers that don't have Python installed (typically Windows computers, since Linux tends to come pre-installed with Python).

我建议使用像Pyinstaller这样的程序。它本质上需要 Python 解释器并将其与您的脚本捆绑在一起,将其转换为一个独立的 exe,可以在没有安装 Python 的任意计算机上运行(通常是 Windows 计算机,因为 Linux 往往预装了 Python)。

To install it, you can either download it from the linked website or use the command:

要安装它,您可以从链接的网站下载或使用以下命令:

pip install pyinstaller

...from the command line. Then, for the most part, you simply navigate to the folder containing your source code via the command line and run:

...从命令行。然后,在大多数情况下,您只需通过命令行导航到包含源代码的文件夹并运行:

pyinstaller myscript.py

You can find more information about how to use Pyinstaller and customize the build process via the documentation.

您可以通过文档找到有关如何使用 Pyinstaller 和自定义构建过程的更多信息。



You don't necessarily have to use Pyinstaller, though. Here's a comparisonof different programs that can be used to turn your Python code into an executable.

不过,您不一定必须使用 Pyinstaller。这是可用于将 Python 代码转换为可执行文件的不同程序的比较