如何使用pyinstaller?

How to use pyinstaller?

好吧,我是一个完全的编程菜鸟,我正在尝试编译我编写的一个简单程序,该程序接收一个字符串并以莫尔斯电码打印出该字符串,它被称为 morse.py。我使用

安装了 pyinstaller
 pip install pyinstaller

我正在尝试使用 pyinstaller 编译我的程序。

现在我搜索了一下,它说我需要写 pyinstaller morse.py,但我真的不知道在哪里写。我尝试移动到我的程序目录并在 CMD 中执行此操作,但它没有用。我尝试在同一目录中制作一个 python 程序并这样做,但也没有用。我找不到任何有用的信息来准确地告诉我如何编译该文件。

有人可以帮忙吗?

我建议先阅读模块本身的 Using Pyinstaller section in the documentation

您还可以使用一些教程(例如Matt Borgerson's one)。

为了重述你应该:

  • 编写您的脚本并确保它有效
  • 运行 来自 命令行:

    ~\pyinstalleryour_file_name.py

  • 此命令将生成一个 your_file_name.spec 文件,您可以在其中包含应用程序所需的所有 dll 和任何自定义设置(Using Spec Files)

  • 一旦您决定了要包含在 .exe 应用程序中的内容,您就可以从命令行运行

    ~\pyinstaller [option1] [option2] your_file_name. py

您可以在文档中找到 options 的完整列表。一个示例可以是 pyinstaller.exe --onefile --windowed --icon=app.ico app.py 其中:

  • --onefile:创建一个单文件捆绑可执行文件。
  • --windowed:如果您在 Mac OS X 或 Windows
  • 中编译,要选择的参数
  • --icon= : 选择要用作文件图标的文件。

您也可以使用 py2exe 轻松创建 exe 文件。

尝试写入 pyinstaller 的完整路径(例如 = C:\Users\user\AppData\Local\Programs\Python\Python35-32\Scripts\pyinstaller.exe)

完整的 cmd 字符串必须如下所示:

C:\Users\user\AppData\Local\Programs\Python\Python35-32\Scripts\pyinstaller.exe --onefile myscript.py

你好,我在 python 中做了一个代码,我用它把自己变成了一个 exe。

确保将其与要转换的文件放在同一目录中。

import subprocess
import shutil
import os

cmd = input("file name: ") # asks user for filename
extra = input("extra commands? eg -w -F if using two or more put a space between: ") # asks for extra options
suros = "pyinstaller "+ extra + " " + cmd + ".py" # sets run command
current_workin_path = os.getcwd() # gets current working path
dirt = current_workin_path + "/"+ cmd # set current working path for folder creation
os.mkdir(dirt) # creates folder for working path and for a copy
shutil.copy(cmd + ".py", current_workin_path + "/" + cmd + "/" + cmd + ".py") # creats copy of python file in the newly set path
os.chdir(current_workin_path + "/" + cmd + "/") # changes current working path to newly created one
subprocess.run(suros) # runs the command set eariler
os.remove(cmd + ".py") # delete the copy of python file

希望这对某人有所帮助。

我在Python方面经验不多。我能够编写简单但有用的脚本。我能够使用 pyinstaller 将它们转换为 .exe。但是我写的程序越多,我拥有的 python 版本(以及包和虚拟环境)就越多。一开始的工作突然停止工作,让我很困惑。但是从一页跳到另一页我明白了。我的post是为了帮助另一个新手。我使用的是 Windows,但有些规则可能与其他系统相同。 我是在 2020 年 3 月写的。Pyinstaller 不能正常使用最新的 python 版本,现在是 3.8。所以首先你需要安装 Python 3.7。其次根据您的系统类型选择版本。它可以是 32 或 64。 您可以安装多个 Python 的版本。在命令行中,当您键入“py”时,您的计算机将查找最新的 Python 版本——我的意思是版本号最高的版本。这是因为在您安装第一个 Python 时出现的文件 C:\Windows\py.exe。如果今天你只安装了 python 3.7,“py”将 运行 Python 3.7。但是第二天,当你安​​装 Python 3.8 并键入“py”时,它将 运行 Python 3.8。如果你想使用旧的,只需输入 py -3.7

C:\Users\Ania>py Python 3.8.2(tags/v3.8.2:7b3ab59,2020 年 2 月 25 日,23:03:10)[MSC v.1916 64 位(AM D64)] 在 win32 上 键入 "help"、"copyright"、"credits" 或 "license" 以获取更多信息。

C:\Users\Ania>py -3.7

Python 3.7.7(tags/v3.7.7:d7c567b08f,2020 年 3 月 10 日,10:41:24)[MSC v.1900 64 位 (AMD64)] 在 win32 上 键入 "help"、"copyright"、"credits" 或 "license" 以获取更多信息。

现在要使用您的项目,您需要为其创建一个虚拟环境。请记住使用正确的 python 版本。首先让我们为它创建一个文件夹。

D:\PYTHON>mkdir my_new_great_project

转到该文件夹​​:

D:\PYTHON>cd my_new_great_project

创建 venv:

D:\PYTHON\my_new_great_project>py -3.7 -m venv venv

上面的第一个“venv”是我们正在使用的python模块的名称。第二个“venv”是我们正在创建的虚拟环境的名称。

激活 venv:

D:\PYTHON\my_new_great_project>venv\Scripts\activate.bat

你应该看到:

(venv) D:\PYTHON\my_new_great_project>

现在您可以使用 pip 安装项目需要的任何外部包。

(venv) D:\PYTHON\my_new_great_project>pip install pandas

Pip“知道”您需要 python 3.7 的软件包。 当您创建项目文件时,不要将它们放在“venv”文件夹中。

从 venv 到 运行 您的项目只需键入它的名称

(venv) D:\PYTHON\my_new_great_project>great_project.py

如果你完成了你的项目,一切正常,你需要 .exe 文件,是时候在你的 venv 中安装 pyinstaller 了。

(venv) D:\PYTHON\my_new_great_project>pip install pyinstaller

要检查您已经安装了哪些外部包,只需键入“pip list”:

(venv) D:\PYTHON\my_new_great_project>pip list

包版本


altgraph 0.17 未来 0.18.2 麻木 1.18.2 pandas 1.0.3 文件 2019.4.18 点子 19.2.3 Py 安装程序 3.6 python-dateutil 2.8.1 pytz 2019.3 pywin32-ctypes 0.2.0 安装工具 41.2.0 六个 1.14.0

现在您可以从 venv 运行 pyinstaller 制作 .exe 文件:

(venv) D:\PYTHON\my_new_great_project>pyinstaller --onefile --name my_project great_project.py

现在您的项目在名为“dist”的文件夹中作为单个 .exe 文件可用。

希望对大家有所帮助。