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.
Related
I have a python project which contains app.py and source.py files. Project also has other files for configs and logs. I have converted app.py to app.exe using pyinstaller
pyinstaller app.py
This has created a dist directory. I have copy pasted source.py, configs and the log files in the dist directory.
I also want to create a setup for this which will be installed on another machine. I cannot share the source.py file as it is thus instead of .py I have used source.pyc. I have now used inno compiler and have created a setup file.
As per my understanding, pyinstaller automatically binds the python interpreter so we do not need to install python on any other machine. I simply installed the exe and it started working fine.
In my project, I have a functionality which calls the source.pyc file like below:
exec(os.system("source.pyc install"))
This was working fine on my development machine but in other machine, it is giving me bad magic error.
As per online forums, this normally happens when we try to run the pyc file with different python version interpreter but in my case I am using the same pyinstaller interpreter. Then how come this error is coming.
Is there any other of compiling the additional files apart from app.py using pyinstaller. How can I remove this error. Thanks
PyInstaller does not bundle or bind a Python interpreter with the EXE file. The EXE file is a compiled binary file which, as it is compiled, does not need an interpreter. A PYC file still requires an interpreter.
You can compile the other files (i.e. source.py) using PyInstaller as well, and then move the compiled EXE file to the same directory as app.exe. In that case you would be able to run your command of import os; os.system("source.exe") from the main program. If your other compiled scripts require modules/libraries not used/included in the app.exe compilation, then when moving the compiled source.exe, you will need to move the bundled libraries to the app.exe directory as well.
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.
I already pointed at the problem of exporting my pygame into an executable for distribution purpose. I still have the problem that when I run the setup.py (I use python version 3.7.0) and build the app, the app directly crashes and I cannot open the unix executable either.
Here is exactly what I did so far:
my setup.py:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
build_exe_options = {"include_files" : ["pic.png", "sound.wav"]} # there are more files, i.e. all pics and audio files used
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('pythonGame.py', base=base)
]
setup(name='MyGame',
version = '1.0',
description = 'blabla',
options = dict(build_exe = build_exe_options),
executables = executables)
when I run the setup.py to create stand-alone app via:
python setup.py bdist_mac
I get (many) error messages (cf. last 3 lines of terminal output):
> error: /Library/Developer/CommandLineTools/usr/bin/install_name_tool:
> input file:
> build/GesaGame-1.0.app/Contents/MacOS/lib/pygame/pygame_icon.icns is
> not a Mach-O file #loader_path/.dylibs/libSDL-1.2.0.dylib error: can't
> copy '#loader_path/.dylibs/libSDL-1.2.0.dylib': doesn't exist or not a
> regular file
or further above
> error: /Library/Developer/CommandLineTools/usr/bin/install_name_tool:
> input file: build/GesaGame-1.0.app/Contents/MacOS/RunningCleats.wav is
> not a Mach-O file
Nevertheless, the build folder has been created. When opening it I find the specified program, but it directly crashes after starting it.
What am I doing wrong here? I suspect it has something to do with the included files, but I am not able to make sense of it.
As I don't know the macos environment and don't have any system to test, I can only guess potential problems with your setup script.
cx_Freeze does not yet support Python 3.7, it has a bug. A bugfix exists but has not yet been released, however you can apply it manually, see What could be the reason for fatal python error:initfsencoding:unable to load the file system codec? and Cx_freeze crashing Python3.7.0. Or you can rollback to Python 3.6 if this is an option for you.
Dynamically imported packages as well as DLL resources (.dll/.so/.dylib) often do no get included automatically by cx_Freeze, you need to tell cx_Freeze to include them using the build_exe options packages and include_files. Or they get included into the wrong place (see next point).
cx_Freeze version 5.1.1 (the current version) freezes the packages into a lib subdirectory of the build directory, whereas the main script and all dependent files in the directory of the main script get frozen directly into the build directory. Thus, the relative path between any file in a package and the directory of the main script or executable changes in the frozen application (it gets an additional lib/). This means that if a package tries to find a file located in a package directory using a relative path from the directory of the main application or vice versa, this mechanism will fail in the frozen application. Go through the stack trace of the error message and for every file reported missing check whether this file is in the build directory and whether the frozen application looks for it at the right place. Make manual copies of the "missing" files into the build directory or into its lib subdirectory as necessary until it works. Once you have identified the correct place for the file, you can use a tuple (source, destination) as item in the include_files list to let cx_Freeze include a file from source to a specific destination into the build directory. See also the FAQ Using data files in the cx_Freeze documentation.
As a general advice, reduce your main script to a minimal application using only a minimal GUI and no further package and make it work on your system. Re-add then the packages and dependencies (icons, pictures, sounds, videos, ...) you need one by one and check that the unfrozen and frozen applications work at each step.
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
Background: I have had success in the past installing and using Pyinstaller to transform my python projects into one-file executables. I don't think it is an issue with my source code or pyinstaller files.
Problem: I used a free, open-source library/module called easygui imported into my source code to build an application. The application works perfectly run natively or through the Python IDE. I am pretty sure the problem is that Pyinstaller is not finding the EasyGUI module to import (it automatically includes and compiles any libraries you import in the script).
Actions: My python folder is not in the C:\ drive, it is in the E:\ drive. I'm able to access the pyinstaller path in "E:\program files\python" but it is not reading the easygui library, I don't think. I installed pyinstaller and easygui using pip.
Reading a LOT of pyinstaller's documentation i tried to run it to include a paths dir like:
E:\Program Files Hard Disk\Python\Scripts>pyinstaller --paths
DIR "E:\Program Files Hard Disk\Python" --onefile "E:\Program Files\Python"
It does output the single executable in the build file but does not launch correctly. From what I can see in the console window the brief moment it's up, it looks like an easygui issue. Here is the result of attempting to launch the executable from the command-line:
Here is the compiling in the command window:
Please help