How can I make portable python desktop application? - python

The requirement is to make an application portable, meaning no installer. I looked at py2exe and I am afraid I need to run install if I want to run it under Windows.
So my question is, can I make a portable python desktop application without any installation (all dependencies and libs are packaged), dragging from USB / CD will run it?
(This is critical because it's a headache for users to install C++ Run Time library...)
Thanks.

You can use this method with py2exe: http://www.py2exe.org/index.cgi/SingleFileExecutable
Basically, you use NSIS to package all of the required files and folders into a single executable. When you run it, the required files are expanded to a temporary directory, the executable is run, and when it exits, the temporary files are deleted automatically.
There is also an example that comes with py2exe which uses Inno Setup instead of NSIS to achieve the same result. It's installed to site-packages\py2exe\samples\extending.

You can also fork Portable Python and modify to include your application and libraries you need. It runs from any drive/network location without installation and you can pick do you want 2.x.x or 3.x.x based Python core

Related

Bundling and distributing nautilus-python extensions with application

I've written a python application and I use cx_freeze to freeze the scripts and create the executable. Then I'm making it into a single executable bin package using shell scripts.
Recently I developed a context menu extension using nautilus-python and would like to include the same with my application bundle. Obviously I can't place the .py file under under ~/.local/share/nautilus-python/extensions. I tried with just placing the .pyc file alone with executable bit enabled for the script which didn't work.
Any pointers reg this would be greatly helpful.
cx_Freeze is fine for Windows or Mac but on Linux systems, applications are expected to be installed with package manager. Usually, distributions have designated people who will create the packages. Alternatively, you can choose distributions you want to support and create the packages for them yourself, or you can use service like OBS.
If you really want to provide a single executable for people to drop into a directory they have on PATH, you will need to provide the extension separately.
Please do not make the application install the extension on start-up, user should retain control over their computer. Or if you do that, please add a setup.py flag, so that distributions can easily disable it.

Py2app issue: python binary does not have a shared library (or framework) at all

I am trying to build a distribution for a script. I have used py2exe to create an exe file and it works fine. I am trying to use py2app to create something similar for Mac.
However, I am getting this error when I use the command
python setup.py py2app
Error Message: python binary does not have a shared library (or framework) at all
Any idea why this is the case?
I am importing some modules like BeautifulSoup apart from the standard ones like urllib, math in the main script file
I am running the entire thing on a 64-bit Windows machine running python 2.7
You need to run py2app on a Mac.
The py2app code uses the copy of Python that's being used to run it to build the standalone executable. If that Python isn't a Mac build (which it won't be, if you're running on Windows), it won't be able to create a Mac executable.
Technically, the error message is telling you that it can't find the libpython.dylib or Python.framework associated with sys.executable, which is true, but could be more useful in this case.
There are alternatives to py2app, like cx_freeze, but they all work the same way: building an executable out of the Python installation used to run them.
So, if you want to build a Mac executable on Windows, there's no automated way to do it.
But there are a few possibilities.
First, you can buy a used Mac Mini for probably $100 or so. Get it set up for development, turn on Remote Login access (in the Sharing pane of Preferences) and leave it running in the corner. From Windows, you can use a little 4-liner ssh script to tell the Mac to check out the source, py2app it, zip up the result, and scp it over to the Windows box (or copy it via Windows file sharing, or ftp it, or check it into source control, or whatever).
If that's not feasible for some reason, hopefully you can at least get access to a Mac once in a while. (If not, how are you ever going to test things?). If so, you can build an app with py2app and zip it up to use as a template. Each time you want to make a new build, you can do that on Windows, just by modifying what's in the template.
The main foo.py script goes in foo.app/Contents/Resources/foo.py. Any other Python modules (whether standard-library, third-party, or your own code), with a few exceptions, go into the fake-standard-library zipfile in foo.app/Contents/Resources/lib. Any C extension modules go into a directory like foo.app/Contents/Resources/lib/python2.7/lib-dynload.
As long as you don't modify any C extensions, upgrade to a new version of Python, or add new third-party libraries that you don't know how to install manually (e.g., because you got them as an egg via easy_install), this will continue to work. If you do any of those things, you'll need to go back to the Mac and create a new template with py2app.
If you don't have access to a Mac at all, you may be able to find a pre-built py2app-generated app for some other project that happens to contain everything you need, in which case you can use it as a template. But this is a huge stretch.

How to convert python .py file into an executable file for use cross platform?

I've been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don't understand much of the documentation, nor am I confident enough to experiment too much.
Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I'd appreciate knowing that too.
Running Vista 32bit, python 2.7
There are two distinct ways of freezing python scripts to create executables:
Packing the interpreter and *.pyc files into one exe file-container. Such an approach is used by tools like PyInstaller, Py2exe, cx_freeze.
Creating native code from Python source, usually using a middle step of converting Python-source to C or C++ code. This is done by such tools as Shed-skin and Nuitka. The problem of this aproach is that such tools do not always support all the functionality of Python (e.g. they can have some typing limitations and so on)
The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that's the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).
You can start with Py2exe tutorial and 'hello-world' to get acquainted with that how compilation is done. As far as I know it's a simplest way to get your goal.
And the last thing, you can't create cross-platform native executables as their file formats are strongly operating system and hardware dependent.
Download py2exe
Download this msvcp90.dll
Copy your FileCode.py AND msvcp90.dll to C:\Python27\
In C:\Python27\ create new text file, then enter this code inside it:
from distutils.core import setup
import py2exe
setup(console=['Avril.py'])
Replace Avril.py with YourFileName.py
Save the file as setup.txt
Open CMD and type this:
cd C:\Python27\
python setup.txt py2exe
Now go to C:\Python27\dist\ and there's your .exe program.
Source: Manvir Singh
Python scripts can be made directly executable, like shell scripts, by putting the python environment path in the top of the script file.
#!/usr/bin/env python3.5
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
Detailed description also for linux is here.
Install pyinstaller, a program that converts .py to .exe for python 2.7 to where python is located:
cd C:\python27\scripts
pip install pyinstaller
then move whatever python file you want to compile to C:\python27\scripts, compile from there by using:
pyinstaller --onefile yourfile.py
the --onefile is optional but it packages the whole thing(in this example yourfile.py) into one .exe. Once everything is done there will be 2 new folders along with a .spec file. From C:\python27\scripts open the folder dist. Your .exe will be located there in one file which you can double tap to execute and distribute to anyone who doesn't have python. Hope it helps.

how to make package for my python project

I have a python project, which uses qt(UI) for user input and then creates the pdf using latex.
I want to make full package of my whole projects in some form of exe.
I came to know about pyinstaller, pyexe and cx_frezer.
So which one should I pick up first.
My exe should run in windows 98, xp, vista and linux.
So how does MiKTeX ( windows) and qt get bundle to my exe.
Does I have to make different exe depending upon the operation system.
Any link for reading will be nice.
This might be a viable alternative to freeze and py2exe. Python allows compilation into bytecode. You will have to do this on all of your target platforms for specific python versions. Run in root of your project folder
python -m compileall
See http://docs.python.org/library/compileall.html for details.
After that, delete all *.py files leaving only *.pyc. Note that such compilations must work up to a specific three digits' version of python's runtime, so this is not very convenient.

How to deploy Python to Windows users?

I'm soon to launch a beta app and this have the option to create custom integration scripts on Python.
The app will target Mac OS X and Windows, and my problem is with Windows where Python normally is not present.
My actual aproach is silently run the Python 2.6 install. However I face the problem that is not activated by default and the path is not set when use the command line options. And I fear that if Python is installed before and I upgrade to a new version this could break something else...
So, I wonder how this can be done cleanly. Is it OK if I copy the whole Python 2.6 directory, and put it in a sub-directory of my app and install everything there? Or with virtualenv is posible run diferents versions of Python (if Python is already installed in the machine?).
I also play before embedding Python with a DLL, and found it easy but I lost the ability to debug, so I switch to command-line plug-ins.
I execute the plug-ins from command line and read the STDOUT and STDERR output. The app is made with Delphi/Lazarus. I install others modules like JSON and RPC clients, Win32com, ORM, etc. I create the installer with bitrock.
UPDATE: The end-users are small business owners, and the Python scripts are made by developers. I want to avoid any additional step in the deployment, so I want a fully integrated setup.
Copy a Portable Python folder out of your installer, into the same folder as your Delphi/Lazarus app. Set all paths appropriately for that.
You might try using py2exe. It creates a .exe file with Python already included!
Integrate the python interpreter into your Delphi app with P4D. These components actually work, and in both directions too (Delphi classes exposed to Python as binary extensions, and Python interpreter inside Delphi). I also saw a patch for Lazarus compatibility on the Google Code "issues" page, but it seems there might be some unresolved issues there.
I think there's no problem combining .EXE packaging with a tool like PyInstaller or py2exe and Python-written plugins. The created .EXE can easily detect where it's installed and the code inside can then simply import files from some pre-determined plugin directory. Don't forget that once you package a Python script into an executable, it also packages the Python interpreter inside, so there you have it - a full Python environment customized with your own code.

Categories

Resources