Is it in any way possible too compile a python script into a standalone program, that would be possible to run on another machine that does not have python installed?
Preferably in a way that the script (or, compiled program) would be able to be installed on other machines, so that it's not just a random file but an actual program that can be launched from the start menu on windows.
Being able to do this in windows is the minimum, but if any cross-platform method exists that would be a big plus.
Any help appreciated.
Actually, there are at least two decisions, as of April, 2015 both work with both Python 2 and 3.
Both I have personally used and can confirm working.
1. cx_Freeze
http://cx-freeze.sourceforge.net/
Works with Windows, OS X and Linux, although you have to compile (or should I say, "freeze") your app on each system. You may use compile your code on different machines or just use virtual machine. Beware that you should use 32-bit Python if you want your app to run on 32-bit systems and compile against it!
2. py2exe
http://www.py2exe.org/
It has added support of Python 3 just recently.
The advantage is that it is possible to wrap the whole program in single executable, while with cx_Freeze you usually end up with Python itself in one file, all your dlls and pythons libraries in separate files and all your code in library.zip file, which is compiled to .pyc files, but this operation is easily reversable, so beware that some of your users might easily hack your software!
The main disadvantage of py2exe is that it is Windows-only.
Related
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/
I want my python GUI apps to run on all three OSs without need to recompile on each platform. Is python executable created by pyinstaller (by or similar utility) platform independent like Java's bytecode? Java's bytecode runs on Windows, Mac and Linux using JRE.
It's right there in the documentation
The output of PyInstaller is specific to the active operating system
and the active version of Python. This means that to prepare a
distribution for:
a different OS
a different version of Python
a 32-bit or 64-bit OS
you run PyInstaller on that OS, under that version of Python. The
Python interpreter that executes PyInstaller is part of the bundle,
and it is specific to the OS and the word size.
In other words: no, it's not portable. You need to compile for each platform/architecture.
As stated by #juanpa.arrivillaga in the comments, this has nothing to do with the bytecode python normally executes. That is portable. The problem here is that PyInstaller bundles a python interpreter that is os- and architecture- specific (that is, this doesn't work for the same reason you wouldn't normally be able to run a Windows executable on MacOs).
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.
I have been plagued with this problem ever since I started with Python, I want to write a Python script, and then export it as an EXE that I can run on any Windows XP (and up) machine.
cx_Freeze covers all of this perfectly, the only problem is that it required Visual C++ Runtime to be installed on the client computer before the resulting EXE will run on it...
Is it possible to convert my beautiful *.py file into a nice distributable EXE that will run on a fresh install of Windows XP and up?
AFAIK if you have a Visual Studio licence, you have the right to bundle the appropriate msvcrXY.dll with your application. That will make it run without having to install the runtime files.
If you don't have a Visual Studio licence, I can think of two solutions:
One is to bundle the VS runtime installer with your application (if that is allowed by the licence), and make a tiny batch file/program that runs the installer if necessary, and then your program. This is not ideal if e.g. the user doesn't have admin rights.
The other option I can think of is for you to compile Python with Mingw-gcc, and then use that Python to create your frozen executable. Then it won't depend on the VS runtime libraries. This approach is of course much more complicated and will probably require quite a bit of tinkering. Perhaps someone has already done it though.
I have a wxPython application that is almost done & I would like to place it in my portfolio. I have to consider when someone attempts to run my app that they may not have Python, or wxPython, so if they just click the main script/python file its not going to run, right?
How should I distribute my app (how do you distribute ur apps) so that it can be run & also so that it could be run on the 3 major OS's (Unix, Windows, MacOSX)?
I know of py2exe for releasing under windows, but what can I use for Unix & MacOSX to compile the program? Whats the easiest way?
Use Gui2exe and compress with UPX to get unpacked size down.
For a setup file(exe) with uinstall info Inno-Setup is good.
I have use this with wxpython several times and got it to work on all windows versions.
For Gui2exe use optimize(2) - compressed(2) - Bundle files(3)
Bundle files(3) is the most stable,
Bundle files(1) make one big exe-file.
For one exe-file is better as last step to use Inno-Setup
You can use py2exe for Windows and py2app for Mac. PyInstaller works for Windows and Linux. Personally, I use GUI2Exe, which wraps all three and makes them a little easier to use. Note: I don't have a Mac, so I haven't tried it with that. You can check out my series on freezing Python here:
http://www.blog.pythonlibrary.org/2010/08/10/a-pyinstaller-tutorial-build-a-binary-series/
http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/
http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/
There are a couple others on the blog too.
I suggest both, script for all platforms and frozen binary for lazy windows users.
To answer your latest question, you don't compile python. Python is an interpreted language, it gets compiled on the fly when run. A python frozen binary is actually the python interpreter with your script hardcoded in it. And frozen binaries are windows-only, AFAIK. Besides, Unix and MacOS (usually) come with python pre-installed.