I really like the PY2EXE module, it really helps me share scripts with other co-workers that are super easy for them to use.
My question is: when the PY2EXE module compiles the code into an executable, does the resulting executable process faster?
Thanks for any replies!
py2exe just bundles the Python interpreter and all the needed libraries into the executable and a few library files. When you run the executable, it uses the bundled interpreter to run your script.
Since it doesn't actually generate native code, the speed of execution should be about the same, possibly slower because of the overhead of everything being packaged up.
Partly, it bundles the python environment with the 'precompiled' pyc files. These are already
parsed into python byte code but they aren't native speed executables
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 know a way to do this is using pyinstaller, however, after some research, I found out that a pyinstaller exe can be reverse engineered to get the original python script.
Do you know a way to compile a python script into an executable which can be shared with anyone ,even if they don't have the dependencies I used, or python, installed, and can not be reverse engineered?
I found two other threads talking about this, maybe you would like them:
How to make an encrypted executable file
https://www.quora.com/How-can-I-protect-my-Python-code-but-still-make-it-available-to-run
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.
I'm building a standalone executable using Cython on Linux.
I have the following code:
import psycopg2 as pg
conn = pg.connect('dbname=**** user=**** password=****')
cur = conn.cursor()
cur.execute('SELECT version()')
print(cur.fetchone())
The problem is when the machine does not have the Python package psycopg2 installed, throws the following exception:
Traceback (most recent call last):
File "test.py", line 2, in init test (test.c:872)
import psycopg2 as pg
ImportError: No module named 'psycopg2'
Im building using the --embed cython flag.
How can I make Cython to compile that particular package too?
From my experience, it is not that straightforward to create a standalone executable from multiples python files (yours or from dependencies like psycopg2).
I would say there are a couple of approaches here I would try:
The first one would be cython_freeze https://github.com/cython/cython/tree/master/Demos/freeze I do not use it myself, so I cannot tell much.
The second one is to use pyinstaller to create such executable. It takes as input the .py or .pyc files and embed them into one executable, together with the python interpreter and required dependencies, so you don't have to install anything on the target machine. Note, however, that your code will run as interpreted python and can be easily decompiled and inspected.
If you really need to compile (cythonize) your code, then you can first cythonize() and the build with setup() your extensions, then run pyinstaller as above (taking care that it doesnt find the .py or .pyc files, just the .pyd or .so extensions) to generate the standalone executable. In both cases, pyinstaller will collect all your dependencies and embed them in the executable (even if it fails, you can tell pyinstaller to embed them with hidden_imports).
There are surely other approaches, like py2exe, but when I researched and played with several technologies some months ago, pyinstaller was the best option for me. I do the process in win, linux and mac without many changes.
EDIT: I didn't realize that the example is python 3. Pyinstaller only works for 2.x now.
--embed means the Python interpreter is embedded in your executable. It does not mean independence from Python. It does not do what you think. It sounds more like you need a tool like py2exe, py2app or pyfreeze.
Nuitka is the tool that you need.
You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
Right now Nuitka is a good replacement for the Python interpreter and compiles every construct that CPython 2.6, 2.7, 3.2, 3.3 and 3.4 offer. It translates the Python into a C++ program that then uses "libpython" to execute in the same way as CPython does, in a very compatible way.
It is somewhat faster than CPython already, but currently it doesn't make all the optimizations possible, but a 258% factor on pystone is a good start (number is from version 0.3.11).