build a .exe for Windows from a python 3 script - python

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 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'
)]
)
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 ?

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

Related

How to run pyinstaller from python script already converted to exe?

I'm trying to run pyinstaller in python exe file in order to someone without python can use pyinstaller but no idea how to do it.
I tried import PyInstaller with other needed modules and convert this script to exe but I got error "The 'PyInstaller' distribution was not found and is required by the application". I also tried to pack PyInstaller exe file but didn't worked too. Python 3.6.5
Any ideas how to do it?
Unfortunately, what you're describing is not possible with PyInstaller. I submitted an issue on GitHub, and this is what one of the developers said:
Nope, this won't work.
PyInstaller internally uses a lot of sub-processes where it is assumed that sys.executable points to a python interpreter (e.g., here); this is not true in a frozen application, where it points to the frozen executable, which ends up in effectively endless recursion.
Even if this was not a problem, the frozen application includes only a subset of python environment, so it cannot be used to freeze an arbitrary script (not to mention the issue of 3rd party packages).
So whatever use case you have for this, it cannot be supported by PyInstaller.
check the requirements for the Pyinstaller from this link initially
https://pythonhosted.org/PyInstaller/requirements.html
Then install Pyinstaller by,
pip install pyinstaller
To create .exe file use this command for basically,
pyinstaller your_script.py
To run the created .exe
METHOD 1
Do double click the .exe file in your directory.
METHOD 2
In your cmd prompt load in to your current directory or project directory then do
: .\dist\your_script.exe
because the create .exe files are saved in dist folder inside to the project folder with the name of your script file names only.

Standalone Executable error: exe has stopped working: Made with Pyqt5 + Pyinstaller

I made an app with Pyqt, which I freezed with Pyinstaller with following options:- (Consider python file name as pyqt_link6.py)
pyi-makespec pyqt_link6.py
pyinstaller --noupx --debug pyqt_link6.spec
Resulting executable launches on my computer and several others (all Windows 7) and functions as expected. However on some machines (same Windows 7 version), it gives following problem:-
(pyqt_link6 is also name of directory where distribution is automatically stored by pyinstaller)
I thought adding --debug option during build was supposed to capture initialization process. But that is not happening.
My main problem is lack of indication of error. What should I look for to correct?
My Python version is 2.7.14 (Anaconda distribution), pyqt 5.6.0, pyinstaller 3.3.1.
I have intentionally not provided any more info, thinking that it may not be necessary and would be like spamming the forum. But should anything more (my python code or spec file be) posted here?. Any indication towards what the problem might be would be helpful.
As some machines can run exe and some can't; what difference should be looked at?
Regards,

Fatal Python error when trying to run an executable Python script

I have a Python script that I have turned into an executable using cx-freeze-4.3.4.win32-py3.4. I have Python 3.4 installed on a Windows 7 64-bit machine.
Here is my simple setup.py file:
from cx_Freeze import setup, Executable
setup( name = "myfilename" ,
version = "0.1" ,
description = "This is my file" ,
executables = [Executable("myfilename.py")] , )
I ran python setup.py build from command prompt in the C:\Python34 folder with both the script I was trying to convert and the setup.py file.
This created another folder called build within was another folder called exe.win32-3.4. In that folder I found my executable file, a bunch of .pyd files, a single .dll file, and a zipped archive called library of a bunch of .pyc files.
If I run the executable from within the exe.win32-3.4 with the library zip archive it executes fine. However, without the library archive of .pyc files (basically if I try just to run the .exe by itself, which is what I am supposed to be able to do) the executable throws out this error:
Fatal Python error: cannot get zipimpirter instance
Current thread 0x000001b48 (most recet call first):
I did some preliminary searching around the web for potential resolutions to the issue but could not find anything substantial. If anyone knows how to troubleshoot this issue that would be much appreciated.
From the docs:
Single-file executables
cx_Freeze does not support building a single file exe, where all of the libraries for your application are embedded in one executable file.
For a single-file solution using py2exe and others, see this question.
In 3.5 there is also the new zipapp module, although the basic functionality has been around for a while.

How to convert python .py file into an executable file for use cross platform?

I've been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don't understand much of the documentation, nor am I confident enough to experiment too much.
Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I'd appreciate knowing that too.
Running Vista 32bit, python 2.7
There are two distinct ways of freezing python scripts to create executables:
Packing the interpreter and *.pyc files into one exe file-container. Such an approach is used by tools like PyInstaller, Py2exe, cx_freeze.
Creating native code from Python source, usually using a middle step of converting Python-source to C or C++ code. This is done by such tools as Shed-skin and Nuitka. The problem of this aproach is that such tools do not always support all the functionality of Python (e.g. they can have some typing limitations and so on)
The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that's the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).
You can start with Py2exe tutorial and 'hello-world' to get acquainted with that how compilation is done. As far as I know it's a simplest way to get your goal.
And the last thing, you can't create cross-platform native executables as their file formats are strongly operating system and hardware dependent.
Download py2exe
Download this msvcp90.dll
Copy your FileCode.py AND msvcp90.dll to C:\Python27\
In C:\Python27\ create new text file, then enter this code inside it:
from distutils.core import setup
import py2exe
setup(console=['Avril.py'])
Replace Avril.py with YourFileName.py
Save the file as setup.txt
Open CMD and type this:
cd C:\Python27\
python setup.txt py2exe
Now go to C:\Python27\dist\ and there's your .exe program.
Source: Manvir Singh
Python scripts can be made directly executable, like shell scripts, by putting the python environment path in the top of the script file.
#!/usr/bin/env python3.5
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
Detailed description also for linux is here.
Install pyinstaller, a program that converts .py to .exe for python 2.7 to where python is located:
cd C:\python27\scripts
pip install pyinstaller
then move whatever python file you want to compile to C:\python27\scripts, compile from there by using:
pyinstaller --onefile yourfile.py
the --onefile is optional but it packages the whole thing(in this example yourfile.py) into one .exe. Once everything is done there will be 2 new folders along with a .spec file. From C:\python27\scripts open the folder dist. Your .exe will be located there in one file which you can double tap to execute and distribute to anyone who doesn't have python. Hope it helps.

Creating an executable file from python script

I have created a GUI program using python and wxpython. It is now ready to be turned into an executable file. I have tried for the last two days following various instructions from py2exe. All instructions for Command Prompt refer to older versions of windows and I'm using windows 7. I have never used Command Prompt before as I'm still new to programming. Is py2exe going to best way to create an executable file or is there a better option?
I have followed instructions on creating a setup.py file and it reads:
from distutils.core import setup
import py2exe
setup(windows = ["Core.py"])
When I enter the command in Command Prompt:
C:\Python27\Cylinderdrawer\python setup.py py2exe
I get the following:
'C:\Python27\Cylinderdrawer\python' is not recognized as an internal or eternal command, operable program or batch file
There are more than two options. You can use py2exe, PyInstaller, cx_freeze and bb_freeze. I enjoy using the GUI2Exe wrapper script that wraps all of these plus py2app (for Macs) as it makes tweaking the settings a breeze.
As far as I can tell, cx_freeze, PyInstaller and bb_freeze have the newest releases with py2exe's last release in 2008.
As for your issue, it sounds like you don't have Python on your system path. Try typing out the full path to your python instead. Something like this:
c:\python27\python.exe setup.py py2exe
I don't know what "Cylinderdrawer" is, but that is NOT a standard location for the Python executable on Windows.
See also:
a py2exe tutorial
a GUI2Exe tutorial
a cx_freeze tutorial
a bb_freeze tutorial
a PyInstaller tutorial
Your two options are py2exe and cx_freeze. py2exe is more widely used, and it can be tricky to get all the details right. If you provide details about what is going wrong, we might be able to help.
Thanks everyone for their help. As much as I have not worked it out completely yet, this problem has been solved. I'll ask seperatly about the subsequent problem if I cannot work it out. For anyone else having a similar problem the following is how I solved it.
C:\Python27\Cylinderdrawer\python setup.py py2exe
This was what I originally put into Command Prompt. Cylinderdrawer is my project folder. This is wrong it needs to be where python.exe is located. the next part, i.e., "setup.py" is the setup file. The only way I could get it to work was to explicitly state where the file was. Bellow is the command that worked.
C:\Python27\python "C:\Python27\Cylinderdrawer\setup.py" py2exe
Once again, thanks to all those who helped.

Categories

Resources