通过python 3脚本为Windows构建.exe [英] build a .exe for Windows from a python 3 script
问题描述
我想为python 3脚本构建可执行文件:
I would like building an executable for a python 3 script that:
- 导入pyqtgraph(带有pyqt5)
- 导入theano和pymc3
- 也导入numpy,scipy,sys,os
- 打开一个用qt设计器制作并存储的简单GUI .ui文件中的
- 将分发到装有Windows 7+的计算机上
- imports pyqtgraph (with pyqt5)
- imports theano and pymc3
- also imports numpy, scipy, sys, os
- opens a simple GUI made with qt designer and stored in a ‘.ui' file
- will be distributed on machines with Windows 7+
我在几个小时内尝试了几种工具(py2exe,pyinstaller,pynsist,cx_Freeze),但每次都失败。我的更糟糕的结果是使用pyinstaller(请参见下文),没有theano部分(因此也没有脚本的一部分)。有人可以帮我吗?
I tried several tools (py2exe, pyinstaller, pynsist, cx_Freeze) during hours but failed each time. my 'less worse’ result was with pyinstaller (see below) without theano part (and so without a part of the script). Can anyone help me ?
我有3个文件:2个'.py'文件(一个带有主要定义,另一个带有一堆定义)和一个'.ui描述GUI。该脚本会进行一些统计分析,然后绘制一些曲线。
I have 3 files: 2 '.py' files (1 with the main and the other with a bunch of definitions) and a '.ui' describing the GUI. The script makes some statistical analyses, then plots some curves.
这是我使用python 3.5和cx_Freeze失败的一个示例(我认为这是我尝试过的最先进的尝试,但我并不特别局限于那些工具):我将3个文件放在Windows机器上的目录中,该机器上的所有安装都很痛苦(使用anaconda)。我添加了一个文件'setup.py',对于cx_Freeze,它是:
Here is an example of my failure with python 3.5 and cx_Freeze (which I think is the most advanced try I had, but I’m not restricted to those tools in particular): I put my 3 files in a directory on a windows machine where everything was painfully installed (with anaconda). I add a file ’setup.py’, which for cx_Freeze is:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python 3.5\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python 3.5\tcl\tk8.6'
os.environ['PYQTGRAPH_QT_LIB'] = 'PyQt5'
setup(
name = ‘concentrationprofiles',
version = '0.1',
description = 'simple tool to simulate concentration profiles. preliminary',
author = 'SPH',
options = dict(
build_exe = dict(
packages = ['os','sys','numpy','theano','pymc3','pyqtgraph'],#omitting ‘scipy’ ! for some reason when I put ’scipy’ in this list the building fails, but it works without… probably the ‘import scipy’ inside the code is properly interpreted
includes = ['numpy.core._methods','numpy.lib.format',
'pyqtgraph.debug','pyqtgraph.functions',
'pyqtgraph.ThreadsafeTimer','cp_util_jul17'],
include_files = ['GUI_cprofiles_jul17.ui']
)),
executables = [Executable(
script='cprofiles_jul17.py',
base='Win32GUI',
targetName=‘concentprofiles.exe'
)]
)
然后我在包含4个文件的目录中的anaconda提示符(相当于我所知的命令提示符)中执行命令行 python setup.py build。经过大量的情节和几小时的战斗之后,建筑物看起来不错(100行,没有错误消息,直到结束),它创建了一个带有 exe.win-amd64-3.5子目录的 build目录,其中包含所需的所有内容+ .exe。但是,当我尝试运行此.exe时,我什么也没得到:没有错误消息,没有控制台或窗口打开,没有快速打开关闭窗口,而且我找不到日志…什么都没有
I then execute the command line ’python setup.py build’ in the anaconda prompt (equivalent to the command prompt to my knowledge) in the directory with the 4 files. After a lot of episodes and hours of fighting, the building looks fine (100s lines with no error message and going until the end), it creates a ‘build’ directory with a ‘exe.win-amd64-3.5’ subdirectory containing everything needed + the .exe. But when I try and run this .exe I get nothing: no error message, no console or window opening, no fast opening-closing and I can’t find a log… just nothing
我试图将'base'选项从'Win32GUI'更改为base ='Console'和base = None。在这些情况下,我猜有一个快速的控制台打开关闭操作,因为找不到日志,所以我无法阅读。
I tried to change the ‘base’ option from ‘Win32GUI’ to base=’Console’ and base=None. In these cases I guess there is a fast console-opening-closing that I cannot read since I don’t find the log.
在其他情况下,其他一些不良结果尝试:
Here are few other bad results during other tries:
-
py2exe:原来与我通常的python 3.6不兼容,所以我降级到3.5。但是即使在3.5中,几行之后它仍然冻结:再次没有错误消息,没有控制台或窗口打开,没有快速打开关闭窗口,而且我找不到日志…什么也没有。甚至没有 build目录。再次,我也尝试使用python 3.4替代方法,但是遇到了有关缺少的 msvcr100.dll的错误,我尝试按照以下说明在论坛上进行安装。当我最终获得修改系统目录的权限时,一条指令 regsvr32失败了(不是仅32位吗?但是没有 regsvr64…)。我最终放弃了
py2exe: turns out to be incompatible with my usual python 3.6, so I downgraded to 3.5. But even in 3.5, after a few lines it froze: once again no error message, no console or window opening, no fast opening-closing and I can’t find a log… just nothing. not even a ‘build’ directory. Another time I also tried an alternative with python 3.4 but I got an error concerning a missing ‘msvcr100.dll’ that I tried to install following instructions on forums. When I eventually got the permission to modify system directories, an instruction ‘regsvr32’ failed (isn’t this for 32 bits only ? but there is no ‘regsvr64’…). I eventually gave up
pyinstaller:查看更新
pyinstaller: see updates
pynsist:pynsist的原理是您没有可执行文件,而只有安装程序。为什么不 ?只要我可以分发代码,就不需要.exe。不幸的是,在构建安装程序(没有错误)并安装它(再次没有可见错误)之后,该程序没有提供任何内容,就像在cx_Freeze情况下一样。
pynsist: The principle of pynsist is that you don’t get an executable but an installer only. Why not ? I don’t need a .exe as long as I can distribute the code. Unfortunately, after building the installer (with no error) and installing it (again with no visible error), the program gives nothing, like in the cx_Freeze case.
我可以根据需要添加指向脚本文件的链接。
I can add a link to the script files if you want/need.
更新八月18,2017,9:20 am
update august, 18, 2017, 9:20am
根据建议,我打开了一个有关pyinstaller的新帖子:使用pyinstaller从theano导入theano的python 3脚本为Windows构建.exe 。
Following the suggestion, I opened a new post concerning pyinstaller: build a .exe for Windows from a python 3 script importing theano with pyinstaller.
我邀请您回答有关pyinstaller的问题。如果我的问题是使用py2exe或cx_freeze解决的,则此问题将被标记为已回答。
I invite you to answer the pyinstaller concerns there. This question will be marked as answered if my problem is solved with py2exe or cx_freeze.
9月2日下午2点更新:
update september, 2, 2pm:
在许多情节之后,我最终设法用pyinstaller构建了一个.exe。
I eventually managed to build a .exe with pyinstaller after many episodes.
不幸的是,我未能应对 theano模块(在我的情况下是 pymc3模块所必需的),我不得不修改.py文件并放弃了部分应用程序。有人可以帮我用'theano'模块为Windows 7+构建.exe吗?
Unfortunately I failed to deal with the ‘theano’ module (that is required in my case by the ‘pymc3’ module) and I had to modify the .py files and give up part of the application. Could anyone help me building a .exe for windows 7+, with the ‘theano’ module ?
推荐答案
要创建可执行文件Python程序,请在CMD中运行以下命令。首先,您需要使用以下命令安装pyinstaller:
To create an executable file of your Python program, run the following command in CMD.First you need to install pyinstaller, with the following command:
pip install pyinstaller
然后执行以下操作来创建Python程序的一个可执行文件,首先,转到程序路径(带有 cd
)的Python(.py)文件所在的位置,然后:
And then do the following to create one executable file of your Python program, first, Go to your program path, (with cd
) where your Python (.py) file is, and then:
pyinstaller -w -F YourPyFile
这篇关于通过python 3脚本为Windows构建.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!