I am using PyInstaller to create a single file executable for my program. I can get it to work with the pyinstaller command and also by editing a .spec file to add the options I need.
I am wondering which is the preferred way of doing thins? or in other words: Should I have a spec file in my git repository or a shell script with all the parameters.
Related
I created a python software, it uses configuration files in external folder.
I give an example of the folder's structure
--Src
--Src/main.py
--reader
--reader/conf.ini
when I run the comand pyinstaller --onefile main.py the final executable file main.exe connot read from the configuration file reader/conf.ini, but if I run the comand pyinstaller main.py the executable file main.exe is able to read from conf.ini.
ho can I use both external configuration file and the option --onefile?
Since files are read at runtime instead of compile-time, the config file would need to exist when the executable is ran. If you don't want this, you can hard-code these values as constants into your program, however, the configuration wouldn't be able to be modified unless you re-compile the source code.
Accidentally deleted the python code, all i have is a single unix executable file generate by pyinstaller.
Is there a way to reverse this and to extract my code ?
Currently I am working on a fork of someone else's project, that is written in the Python programming language.
I have access to all the source code I need, with all the changes I wanted to make, and everything 'set' to how I need it.
My current step is trying to somehow compile it, so it runs as windows in a stand-alone application. I know this is possible because this is how the source-application runs. Currently I have access to Visual Studio with the python extension module, WinPython, the kivy framework itself that the GUI was built with, etc.
But I cannot seem to figure out how to do this. My cursory research has suggested a program called py2exe but that does not work with what I need, based on what I can tell.
copied from http://kivy.org/docs/guide/packaging-windows.html
Create the spec file
For this example, we’ll package the touchtracer example and embed a custom icon. The touchtracer example is the kivy\examples\demo\touchtracer directory and the main file is named main.py.
Double click on the Kivy.bat and a console will open.
Go to the pyinstaller 2.1 directory and create the initial spec:
cd pyinstaller-2.1
python pyinstaller.py --name touchtracer ..\kivy\examples\demo\touchtracer\main.py
You can also add an icon.ico file to the application folder in order to create an icon for the executable. If you don’t have a .ico file available, you can convert your icon.png file to ico using the web app ConvertICO. Save the icon.ico in the touchtracer directory and type:
python pyinstaller.py --name touchtracer --icon ..\kivy\examples\demo\touchtracer\icon.ico ..\kivy\examples\demo\touchtracer\main.py
For more options, please consult the PyInstaller 2 Manual.
The spec file will be touchtracer.spec located in inside the pyinstaller + touchtracer directory. Now we need to edit the spec file to add kivy hooks to correctly build the exe. Open the spec file with your favorite editor and add theses lines at the beginning of the spec:
from kivy.tools.packaging.pyinstaller_hooks import install_hooks
install_hooks(globals())
In the Analysis() function, remove the hookspath=None parameter. If you don’t do this, the kivy package hook will not be used at all.
Then you need to change the COLLECT() call to add the data for touchtracer (touchtracer.kv, particle.png, ...). Change the line to add a Tree() object. This Tree will search and add every file found in the touchtracer directory to your final package:
coll = COLLECT( exe, Tree('../kivy/examples/demo/touchtracer/'),
a.binaries,
#...
)
We are done. Your spec is ready to be executed!
Build the spec
Double click on Kivy.bat
Go to the pyinstaller directory, and build the spec:
cd pyinstaller-2.1
python pyinstaller.py touchtracer\touchtracer.spec
The package will be in the touchtracer\dist\touchtracer directory.
This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 3 years ago.
I wrote a script that will help a Windows user in her daily life. I want to simply send her the .exe and not ask her to install python, dlls or have to deal with any additional files.
I've read plenty of the stackoverflow entries regarding compiling Python scripts into executable files. I am a bit confused as there are many options but some seem dated (no updates since 2008) and none were simple enough for me not to be asking this right now after a few hours spent on this.
I'm hoping there's a better, up-to-date way to do this.
I looked into:
pylunch
py2exe
cx_Freeze
py2app (only for Mac)
pyinstaller
bbfreeze
but either I couldn't get them to work or couldn't understand how to get the result I need. The closest I got was with py2exe but it still gave me the MSVCR71.dll
I would appreciate a step-by-step answer as I was also unable to follow some of the tweaking answers here that require some prior understanding of how to use py2exe or some of the other tools.
I'm using Python 2.5 as one of the modules is only available for that version.
PyInstaller will create a single-file executable if you use the --onefile option (though what it actually does is extracts then runs itself).
There's a simple PyInstaller tutorial here. If you have any questions about using it, please post them...
Using py2exe, include this in your setup.py:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
windows = [{'script': "YourScript.py"}],
zipfile = None,
)
then you can run it through command prompt / Idle, both works for me. Hope it helps
i would recommend going to http://sourceforge.net/projects/py2exe/files/latest/download?source=files to download py2exe. Then make a python file named setup.py.
Inside it, type
from distutils.core import setup
import py2exe
setup(console=['nameoffile.py'])
Save in your user folder
Also save the file you want converted in that same folder
Run window's command prompt
type in setup.py install py2exe
It should print many lines of code...
Next, open the dist folder.
Run the exe file.
If there are needed files for the program to work, move them to the folder
Copy/Send the dist folder to person.
Optional: Change the name of the dist folder
Hope it works!:)
I would join #Nicholas in recommending PyInstaller (with the --onefile flag), but be warned: do not use the "latest release", PyInstaller 1.3 -- it's years old. Use the "pre-release" 1.4, download it here -- or even better the code from the svn repo -- install SVN and run svn co http://svn.pyinstaller.org/trunk pyinstaller.
As #Nicholas implies, dynamic libraries cannot be run from the same file as the rest of the executable -- but fortunately they can be packed together with all the rest in a "self-unpacking" executable that will unpack itself into some temporary directory as needed; PyInstaller does a good job at this (and at many other things -- py2exe is more popular, but pyinstaller in my opinion is preferable in all other respects).
1) Get py2exe from here, according to your Python version.
2) Make a file called "setup.py" in the same folder as the script you want to convert, having the following code:
from distutils.core import setup
import py2exe
setup(console=['myscript.py']) #change 'myscript' to your script
3) Go to command prompt, navigate to that folder, and type:
python setup.py py2exe
4) It will generate a "dist" folder in the same folder as the script. This folder contains the .exe file.
you may want to see if your app can run under IronPython. If so, you can compile it to an exe
http://www.codeplex.com/IronPython
You can create executable from python script using NSIS (Nullsoft scriptable install system). Follow the below steps to convert your python files to executable.
Download and install NSIS in your system.
Compress the folder in the .zip file that you want to export into the executable.
Start NSIS and select Installer based on ZIP file. Find and provide a path to your compressed file.
Provide your Installer Name and Default Folder path and click on Generate to generate your exe file.
Once its done you can click on Test to test executable or Close to complete the process.
The executable generated can be installed on the system and can be distributed to use this application without even worrying about installing the required python and its packages.
For a video tutorial follow: How to Convert any Python File to .EXE
You could create an installer for you EXE file by:
1. Press WinKey + R
2. Type "iexpress" (without quotes) into the run window
3. Complete the wizard for creating the installation program.
4. Distribute the completed EXE.
My Problem is the following:
I want to create an script that can create other executables. These new executables have to be standalone, so they don't require any DLL's, etc.
I know this is possible with PyInstaller, but only from console/command line.
So essentially, what I want to do is make a python script that imports pyinstaller, creates another .py-file and uses pyinstaller to compile the new script to a .exe, so people who don't have python installed can use this program.
EDIT: The script itself should only use one file, so it can also be a one-file executable
Supposing you have already installed Pyinstaller in PYINSTALLER_PATH (you should have called the Configure.py script in the distribution for the first time), Pyinstaller generates a spec file from your main script by calling the Makespec.py. You can add some flags to generate one dir binary distribution or one file. Finally you have to call Build.py with spec file.
This is easy scriptable with a couple of system calls. Something like :
import os
PROJECT_NAME = "test"
PROJECT_MAIN_SCRIPT = "main_script.py"
MAKESPEC_CMD = """%s %s\Makespec.py -X -n %s -F %s""" % (PYTHON_EXECUTABLE, PYINSTALLER_PATH, PROJECT_NAME, PROJECT_MAIN_SCRIPT)
BUILD_CMD = """%s %s\Build.py %s.spec""" % (PYTHON_EXECUTABLE, PYINSTALLER_PATH, PROJECT_NAME)
os.system(MAKESPEC_CMD)
os.system(BUILD_CMD)
You can avoid to generate the spec file every time and hack it, adding embedded resources (i.e. xml files or configuration) and specifying some other flag. Basically this is a python file, with the definition of some dictionaries.
I don't think there is a Pyinstaller module you can use directly, but you can look at Build.py and mimic its behaviour to do the same. Build.py is the main script which does the trick.
You may want to check out cx_Freeze, which can be used to do this kind of thing.
There are three different ways to use cx_Freeze. The first is to use the included cxfreeze script which works well for simple scripts. The second is to create a distutils setup script which can be used for more complicated configuration or to retain the configuration for future use. The third method involves working directly with the classes and modules used internally by cx_Freeze and should be reserved for complicated scripts or extending or embedding.
Source
Try downloading Pyinstaller's latest development code. There they trying to implement GUI toolkit for building executables.