Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

build a .exe for Windows from a python 3 script

I would like building an executable for a python 3 script that:

  • 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+

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 ?

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.

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 FilesPython 3.5clcl8.6'
os.environ['TK_LIBRARY'] = r'C:Program FilesPython 3.5clk8.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'
        )]
    )

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

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: 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: see updates

  • 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.


update august, 18, 2017, 9:20am

Following the suggestion, I opened a new post concerning pyinstaller: build a .exe for Windows from a python 3 script importing theano with pyinstaller.

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.


update september, 2, 2pm:

I eventually managed to build a .exe with pyinstaller after many episodes.

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...