Python 如何使用pyinstaller?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34453458/
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 15:00:31  来源:igfitidea点击:

How to use pyinstaller?

pythoncompilationpyinstaller

提问by user3333708

Okay so I'm a complete noob in programming and I'm trying to compile a simple program I wrote that takes in a string and prints out the string in morse code it's called morse.py. I installed pyinstaller using

好的,所以我是一个完整的编程新手,我正在尝试编译一个我编写的简单程序,该程序接受一个字符串并以莫尔斯电码打印出该字符串,它称为 morse.py。我安装了 pyinstaller 使用

 pip install pyinstaller

and I am trying to compile my program using pyinstaller.

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

Now I've searched a bit and it says that I need to write pyinstaller morse.py, but I don't really know where to write that. I tried moving to the directory of my program and doing that in CMD but it didn't work. I tried making a python program in the same directory and doing that and that also didn't work. I couldn't find anything very helpful to tell me exactly how to compile the file.

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

Can someone please help?

有人可以帮忙吗?

采纳答案by mabe02

I would suggest to first read the Using Pyinstaller section in the documentationof the module itself.

我建议先阅读模块本身文档中的Using Pyinstaller 部分

You can also use some tutorials(e.g. Matt Borgerson's one).

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

In order to recap you should:

为了回顾你应该:

  • write your scriptand make sure that it works
  • run from the command line:

    ~\ pyinstaller your_file_name.py

  • this command will generate a your_file_name.spec file where you can include all the dll required by your application and any custom settings (Using Spec Files)

  • once you have decided what to include in your .exe application you can run from the command line

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

  • 编写脚本并确保它有效
  • 从运行命令行

    〜\ pyinstaller your_file_name的.py

  • 这个命令会产生一个your_file_name的.spec文件,其中可以包含所有应用程序和任何自定义设置所需的DLL(使用规格文件

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

    〜\ pyinstaller [选项1] [选项2] your_file_name的.py

You can find the full list the optionsin the documentation. An example could be pyinstaller.exe --onefile --windowed --icon=app.ico app.pywhere:

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

  • --onefile: Create a one-file bundled executable.
  • --windowed: Parameter to chooseif you are compiling in Mac OS X or Windows
  • --icon= : Choose the file to use as icon for file.
  • --onefile:创建一个单一文件的捆绑可执行文件。
  • --windowed: 选择在 Mac OS X 或 Windows 中编译的参数
  • --icon= :选择用作文件图标的文件。

You can create your exe file very easily also with py2exe.

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

回答by Андрей Вовченко

Try to write full path to pyinstaller (for example = C:\Users\user\AppData\Local\Programs\Python\Python35-32\Scripts\pyinstaller.exe)

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

full cmd string must look like:

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

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

回答by Darthbeaverdog

Hello i made a code in python, i used it to turn itself into a exe.

你好,我用 python 编写了一个代码,我用它把自己变成了一个 exe。

Make sure to have it in same directory as the file you want converted.

确保它与要转换的文件位于同一目录中。

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

Hope this helps someone.

希望这可以帮助某人。

回答by aniaszka

I don't have much experience in Python. I am able to write simple but useful scripts. I was able to convert them to .exe using pyinstaller. But the more programs I write, the more python's versions I have (and packages and virtual environments). What was working in the beginning suddenly stopped working and got me very confused. But jumping from one page to another I figured it out. My post is to help another newbies. I'm using Windows, but some rules are the same probably for other systems. I'm writing it in March 2020. Pyinstaller doesn't work properly with the newest python version which is now 3.8. So firstly you need to install Python 3.7. Secondly choose the version accordingly to type of your system. It can be 32 or 64. You can install more than one Python's version. In command line when you type “py” your computer will look for the newest Python's version - I mean the one with the highest version number. It's because of the file C:\Windows\py.exe which appears when you install your first Python. If today you have installed only python 3.7, “py” will run Python 3.7. But next day when you install Python 3.8 and you type “py” it will run Python 3.8. If you want to use the older one just type py -3.7

我在 Python 方面没有太多经验。我能够编写简单但有用的脚本。我能够使用 pyinstaller 将它们转换为 .exe。但是我编写的程序越多,我拥有的 Python 版本(以及包和虚拟环境)就越多。一开始工作的东西突然停止工作,让我很困惑。但是从一页跳到另一页我想​​通了。我的帖子是为了帮助另一个新手。我使用的是 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>pyPython 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

C:\Users\Ania>pyPython 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AM D64)] on win32 输入“help”、“copyright”、“credits”或“许可证”以获取更多信息。

C:\Users\Ania>py -3.7

Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 位 (AMD64)] on win32 输入“help”、“copyright”、“credits”或“许可证”以获取更多信息。

Now to work with your project you need to create a virtual environment for it. Remember to use the correct python version. First let's make a folder for it.

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

D:\PYTHON>mkdir my_new_great_project

D:\PYTHON>mkdir my_new_great_project

Go to that folder:

转到该文件夹​​:

D:\PYTHON>cd my_new_great_project

Create the venv:

创建 venv:

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

The first “venv” above is the name of a python module which we are using. The second “venv” is the name of the virtual environment which we are creating.

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

Activate the venv:

激活 venv:

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

You should see:

你应该看到:

(venv) D:\PYTHON\my_new_great_project>

Now you can use pip to install whatever external packages your project needs.

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

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

Pip “knows” that you need packages for python 3.7. When you create your project files don't put them in “venv” folder.

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

To run your project from the venv just type its name

要从 venv 运行您的项目,只需键入其名称

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

If you are done with your project, everything works perfectly and you need .exe file, it's time to install pyinstaller in your venv.

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

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

To check what external packages you've already installed just type “pip list”:

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

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

Package Version

套餐版本



altgraph 0.17 future 0.18.2 numpy 1.18.2 pandas 1.0.3 pefile 2019.4.18 pip 19.2.3 PyInstaller 3.6 python-dateutil 2.8.1 pytz 2019.3 pywin32-ctypes 0.2.0 setuptools 41.2.0 six 1.14.0

altgraph 0.17 未来 0.18.2 numpy 1.18.2 pandas 1.0.3 pefile 2019.4.18 pip 19.2.3 PyInstaller 3.6 python-dateutil 2.8.1 pytz 2019.3 pywin32-ctypes.0.24.01.0.0.16.1.0

Now from venv you can run pyinstaller to make an .exe file:

现在从 venv 你可以运行 pyinstaller 来制作一个 .exe 文件:

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

Now your project is available as an single .exe file in the folder called “dist”.

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

I hope it helps someone.

我希望它可以帮助某人。