This question already has answers here:
Create a directly-executable cross-platform GUI app using Python
(13 answers)
Closed 8 years ago.
I wrote a small program in Python. Actually it's not even a program really :p.
It's just this:
print(1+1)
When I saved it, it saved it as py1.py (a python file). And apparently, Python files can be executed, and it executed fine.
Is there anyway to be able to compile it into bytecode? Also is there a way to make it a stand alone application?
I could be getting terms wrong, I'm more of a Java person. I'm new to Python.
Thanks!
First of all, as #emh pointed out this is a duplicate of several other questions. One of them is stackoverflow.com/questions/2933/an-executable-python-app
There are several sets of scripts and modules that are available that allow you to convert a .py file into an standalone executable file. Some of the more popular ones are py2exe, py2app, Pyinstaller, and Cx_freeze. The one that is best for you depends you how you are using it. First, it depends on what operating you are using. py2exe is meant specifically for creating .exe files or windows executable files, py2app is the same as py2exe except it builds .app files for Mac OS, pyinstaller and cx_freeze can build an executable for multiple systems, including Windows, Mac OS, and Linux. Next, what you use depends on what version of Python you have. Cx_Freeze is the only one that I mentioned that supports python 3.X; the others only support Python 2.X. The advantage pyinstaller has, is it builds all the dependent files into one executable file that unpacks right before execution, whereas the others create a folder with lots dependent files along with the executable file. I use Cx_freeze, because of its python 3.x support and relatively easy building process.
As for converting it to bytecode, there are a couple python modules for this. One is py_compile. an example of this is:
import py_compile
py_compile.compile('filepathandname')
this will create a .pyc file, which python will put in a folder labeled __pycache__ in the same directory as the original file.
Hope this helped.
Related
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.
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.
This question already has answers here:
Create a directly-executable cross-platform GUI app using Python
(13 answers)
How to deploy Python to Windows users?
(4 answers)
Create a single executable from a Python project [closed]
(3 answers)
Closed 9 years ago.
I've used several modules to make EXEs for Python, but I'm not sure if I'm doing it right.
How should I go about this, and why? Please base your answers on personal experience, and provide references where necessary.
Auto PY to EXE - A .py to .exe converter using a simple graphical interface built using Eel and PyInstaller in Python.
py2exe is probably what you want, but it only works on Windows.
PyInstaller works on Windows and Linux.
Py2app works on the Mac.
I found this presentation to be very helpfull.
How I Distribute Python applications on Windows - py2exe & InnoSetup
From the site:
There are many deployment options for
Python code. I'll share what has
worked well for me on Windows,
packaging command line tools and
services using py2exe and InnoSetup.
I'll demonstrate a simple build script
which creates windows binaries and an
InnoSetup installer in one step. In
addition, I'll go over common errors
which come up when using py2exe and
hints on troubleshooting them. This is
a short talk, so there will be a
follow-up Open Space session to share
experience and help each other solve
distribution problems.
Also known as Frozen Binaries but not the same as as the output of a true compiler- they run byte code through a virtual machine (PVM). Run the same as a compiled program just larger because the program is being compiled along with the PVM. Py2exe can freeze standalone programs that use the tkinter, PMW, wxPython, and PyGTK GUI libraties; programs that use the pygame game programming toolkit; win32com client programs; and more.
The Stackless Python system is a standard CPython implementation variant that does not save state on the C language call stack. This makes Python more easy to port to small stack architectures, provides efficient multiprocessing options, and fosters novel programming structures such as coroutines. Other systems of study that are working on future development: Pyrex is working on the Cython system, the Parrot project, the PyPy is working on replacing the PVM altogether, and of course the founder of Python is working with Google to get Python to run 5 times faster than C with the Unladen Swallow project. In short, py2exe is the easiest and Cython is more efficient for now until these projects improve the Python Virtual Machine (PVM) for standalone files.
Not on the freehackers list is gui2exe which can be used to build standalone Windows executables, Linux applications and Mac OS application bundles and plugins starting from Python scripts.
Use cx_Freeze to make exe your python program
py2exe:
py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.
See a short list of python packaging tools on FreeHackers.org.
This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I am writing code (Python and wxpython for GUI) which will run on Debian OS on Raspberry PI. I want to protect/hide the source code. Is there any way to do it? Probably py2exe, or converting it to a library or something else?
The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).
Simply:
run your application
then copy all the relevant .pyc files into another folder and you should be able to
run it all from the new location
So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)
The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.
py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.
Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.
I have been given a project to do. One of the main essential requirements is that this is given to the customer to run as single exe. It does not matter which programming language, however it will be comparing files between a set of default files and the customer’s files.
Is there any way I can do this so that I have one exe?
The py2exe library allows you to create exe files from your python code. I've not used it but it may do the job!
http://www.py2exe.org/
Alternatively, you can try pyinstaller.
See also: py2exe - generate single executable file
You're in luck! You can do just that with Python using the py2exe conversion utility.
You can find it at: http://www.py2exe.org/