How to package a Python 3 console application as a windows binary? - python

I am doing this practice project to implement a LISP interpreter in Python, using help from here. I wanted to create an exe file for the project, executing which would start a REPL.
I tried using py2exe and pyInstaller but an error is thrown when I execute the output binary, saying that this script cannot run.
Where did I go wrong with my approach and what alternative ways can I use?
Thank you.

It is hard to know for sure but have you checked that all of the required dependencies for your project are either in the same folder as the created executable or (at least) in your path?
The other alternative that I am aware of (and use) is cx_Freeze. This particular exe builder has cross platform support.
cx_Freeze will attempt to automatically find all dependent python modules and include them in the final build. I imagine that the other two options work in the same manner. Packages that cannot be automatically located and binary dependencies (eg dlls, sos) must be explicitly specified in the build configuration scripts.
One method I have for debugging for missing dependencies is to manually copy the suspected missing dependency into the same folder as the .exe to see if it fixes the issue. If it does then I will specify it in the build configuration script.
See https://cx-freeze.readthedocs.io/en/latest/distutils.html for cx_Freeze documentation, in particular section titled build_exe.
Here is a good example of a non-trival setup.py for cx_Freeze: http://www.pythonexample.com/code/cx_freeze-setup/

Related

Remove dependencies when compiling .py to .exe [duplicate]

I need to package my Python application, its dependencies, and Python itself into a single MSI installer for distribution to users. The end result should desirably be:
Python is installed in the standard location
the package and its dependencies are installed in a separate directory (possibly site-packages)
the installation directory should contain the Python uncompressed and a standalone executable is not required
Kind of a dup of this question about how to make a python into an executable.
It boils down to:
py2exe on windows, Freeze on Linux, and
py2app on Mac.
I use PyInstaller (the svn version) to create a stand-alone version of my program that includes Python and all the dependencies. It takes a little fiddling to get it to work right and include everything (as does py2exe and other similar programs, see this question), but then it works very well.
You then need to create an installer. NSIS Works great for that and is free, but it creates .exe files not .msi. If .msi is not necessary, I highly recommend it. Otherwise check out the answers to this question for other options.
My company uses the free InnoSetup tool. It is a moderately complex program that has tons of flexibility for building installers for windows. I believe that it creates .exe and not .msi files, however. InnoSetup is not python specific but we have created an installer for one of our products that installs python along with dependencies to locations specified by the user at install time.
I've had much better results with dependencies and custom folder structures using pyinstaller, and it lets you find and specify hidden imports and hooks for larger dependencies like numpy and scipy. Also a PITA, though.
py2exe will make windows executables with python bundled in.
py2exe is the best way to do this. It's a bit of a PITA to use, but the end result works very well.
Ok, I have used py2exe before and it works perfectly except for one thing... It only works on executable windows machines. I then learned about Jython which turn a python script into a .Jar file. Which as you know is executable from any machine that has Java ("To your latest running version") installed. Which is great because both unix, windows, and ios (Most of the time) Run java. That means its executable from all of the following machines. As long as they run Java. No need for "py2mac + py2exe + freeze" just to run on all operating systems. Just Jython
For more information on how it works and how you can use it click here.
http://www.jython.org/

Compile python virtual environment

I created a Python script for a Freelance job and I can't find how to compile/build/package it for easy sharing. The person for which I created it is not a technical one, so I can't explain him how to activate a virtualenv, install requirements and so on.
What is the easiest way for him to run the project right after downloading it?
Can the whole virtualenv be compiled into an .exe? If yes, can this be done inside a macOS system?
Yes you can package your python programs and it's dependencies with
Cx_Freeze
There are other python modules that do the same, personally i prefer cx_Freeze because it's cross platform and was the only one that worked out of the box for me.
By default cx_Freeze automatically discovers modules and adds them to the exe to be generated. but sometimes this doesn't work and you might have to add them by yourself
To create a simple executable from a file. you can just use the bundled cxfreeze script
cxfreeze hello.py --target-dir dist
but more for more complex applications that have different files you'll have to create a distutils setup script.
Note that cx_freeze doesn't compile your code like a traditional compiler. it simply zips your python files and all it's dependencies ( as byte code) together, and includes the python interpreter to make your program run. So your code can be disassembled. (if anyone wants to) and you'll also notice that the size of your program would be larger than it was initially (Because of the extra files included)
I ended up using PyInstaller as this worked out of the box for me.

Python compile all modules into a single python file

I am writing a program in python to be sent to other people, who are running the same python version, however these some 3rd party modules that need to be installed to use it.
Is there a way to compile into a .pyc (I only say pyc because its a python compiled file) that has the all the dependant modules inside it as well?
So they can run the programme without needing to install the modules separately?
Edit:
Sorry if it wasnt clear, but I am aware of things such as cx_freeze etc but what im trying to is just a single python file.
So they can just type "python myapp.py" and then it will run. No installation of anything. As if all the module codes are in my .py file.
If you are on python 2.3 or later and your dependencies are pure python:
If you don't want to go the setuptools or distutiles routes, you can provide a zip file with the pycs for your code and all of its dependencies. You will have to do a little work to make any complex pathing inside the zip file available (if the dependencies are just lying around at the root of the zip this is not necessary. Then just add the zip location to your path and it should work just as if the dependencies files has been installed.
If your dependencies include .pyds or other binary dependencies you'll probably have to fall back on distutils.
You can simply include .pyc files for the libraries required, but no - .pyc cannot work as a container for multiple files (unless you will collect all the source into one .py file and then compile it).
It sounds like what you're after is the ability for your end users to run one command, e.g. install my_custom_package_and_all_required_dependencies, and have it assemble everything it needs.
This is a perfect use case for distutils, with which you can make manifests for your own code that link out to external dependencies. If your 3rd party modules are available publicly in a standard format (they should be, and if they're not, it's pretty easy to package them yourself), then this approach has the benefit of allowing you to very easily change what versions of 3rd party libraries your code runs against (see this section of the above linked doc). If you're dead set on packaging others' code with your own, you can always include the required files in the .egg you create with distutils.
Two options:
build a package that will install the dependencies for them (I don't recommend this if the only dependencies are python packages that are installed with pip)
Use virtual environments. You use an existing python on their system but python modules are installed into the virtualenv.
or I suppose you could just punt, and create a shell script that installs them, and tell them to run it once before they run your stuff.

cx_Freeze python single file?

I've been using cx_Freeze for a while now and there is one thing I've really wanted to do: put ALL files into ONE executable that I can distribute. It's not really user-friendly to send around a folder filled with 30 files, all in the same directory. How can I accomplish this?
Thanks.
It is not supported in cx_freeze. There was discussion about it on mailing list. As I recall, the author stated that others (PyInstaller, Py2Exe) use some dirty hacks to achieve that. Some anti-virus programs can consider that behavior as a virus also.
I used PyInstaller for some time, but went back to cx_freeze because of Python 2.7 support. I pack everything using Inno Setup, so it is easy to distribute. Depends if you use Windows or not.
Isn't this what bbfreeze does?
Tutorial here: http://www.blog.pythonlibrary.org/2010/08/19/a-bbfreeze-tutorial-build-a-binary-series/
It's actually not that hard to roll your own with Python zipimport http://docs.python.org/library/zipimport.html
You may prefer to use something like UPX which is a more general solution, not Python only. http://upx.sourceforge.net/
You can use 'bdist_msi' instead of 'build'. This will create a dist folder in the directory with a single setup application.
http://cx-freeze.readthedocs.io/en/latest/distutils.html
According to the documentation
cx_Freeze does not support building a single file exe, where all of the libraries for your application are embedded in one executable file.
The documentation gives some options to obtain a single file.
A further option under Windows is to use the bdist_msi command to create a single Microsoft Installer file (.msi) for your application, as mentioned by #QuaziRabbi. This command has few cx_Freeze-specific options documented here to customize the installer. This command overloads the bdist_msi command of the distutils package which itself brings some more options to customize the installer. The available options are not documented, one need to look at the source code of the distutils package. Interesting examples are Use cx-freeze to create an msi that adds a shortcut to the desktop and cx_freeze bdist_msi: create registry entries?
Another option is to use another tool to create a more customizable single-file installer for the frozen application after the cx_Freeze build step. The script-based tool NSIS (Nullsoft Scriptable Install System) allows one to create a very customizable installer and the use of a script means that this step can be completely automatized. #Fenisko's answers mentions another tool, and there are many more.

Application is not working under raw system

I've an python GUI application, I use pyQt4.
I build binary with bbfreeze (before I was using py2exe but it didn't work with email module well).
On system where I build this app, everything works properly, but when I install it on raw windows (without all those vc_redist and set of python libraries) binary does not work.
Where should I start to find a solution, since I have no messages/exceptions/crashes, it simply ends immediately after i run it from command line.
I predict that if I'd install some of tools from "build system" I would run it.
Is this the only way?
I mean, if I would find the missing lib (if it's a lib problem), would adding this library to bbfreeze script would solve this problem?
cheers
P.
Get Dependency Walker, and run depends.exe on your executable. It will examine the full tree of DLL dependencies, and mark with a red error the ones that are missing.
It will likely be a MSCVRTxx.dll.

Categories

Resources