Do you want to share a Python script to a friend that doesn’t have Python installed? In this post you’ll learn how 🙂
With the py2app package you can compile a Python script and create a portable Mac application. If you’re using windows, you can check py2exe.
First of all, we need to install the py2app python package. If you have a virtual environment, activate it now 🙂
$ pip install -U py2app
Next, we need to create a setup.py file for our project. In your working directory (the one that contains your Python script), type:
$ py2applet --make-setup MyApplication.py
This will create the file setup.py, which is responsible to tell setuptools how to build your application.
If you edit this file, you should see something like:
""" This is a setup.py script generated by py2applet Usage: python setup.py py2app """ from setuptools import setup APP = ['MyApplication.py'] DATA_FILES = [] OPTIONS = {'argv_emulation': True} setup( app=APP, data_files=DATA_FILES, options={'py2app': OPTIONS}, setup_requires=['py2app'], )
You should put the name of your starting python script (the one your run to start) in the APP variable.
APP = ['application_name.py']
Also, if your application uses some data files, like a json or a txt file, you should include it in DATA_FILES. For example:
DATA_FILES = ['myfile.json', 'myfile.txt']
Now, we will compile our project in Alias mode. This instructs py2app to build an application that uses the source data files in-place. Note that this means that the application will not be portable to other machines (we’ll do that next!).
$ python setup.py py2app -A
You will have two new folders inside your working directory: build and dist. The first one is used for building your app (you don’t have to touch it) and the second one contains your application bundle.
From the working directory, you can run your app using:
$ ./dist/application_name.app/Contents/MacOS/application_name
or you can find your application in Finder and open it from there (you’ll find it in dist/application_name).
Once your application is running, we are ready to create the stand alone version. First remove the build and dist folders with:
$ rm -rf build dist
and next, build your application:
$ python setup.py py2app
During the building process, I encounter some problems with the ModuleGraph package (remember that this package is installed at the same time as py2app).
If you get one of the following errors:
AttributeError: 'ModuleGraph' object has no attribute 'scan_code' AttributeError: 'ModuleGraph' object has no attribute 'load_module'
Edit the file where this error ocured (for me, it was inside the virtual environment folder, named myenv), at:
myenv/lib/python2.7/site-packages/py2app/recipes/virtualenv.py
Look for the functions scan_code or load_module and add it an underscore before them, _scan_code and _load_module. Build again your application, it should work now 🙂
Next, try to run your app, and see if it works.
If you get errors with external packages, like certificates with httplib2 or requests, missing files in selenium webdriver, or import errors that normally work, you’ll have to fully include these packages in your application. Edit again the setup.py file and add the package that raised the error:
OPTIONS = { 'argv_emulation': True, 'packages': ['httplib2', 'requests', 'selenium'] }
After saving the file, remove again the build and dist directories, build your application and try to run it. Maybe another package failed? Try to include it in the setup file and start over.
However, not all the errors you get will be solved like this, sorry…!
Once you’re application runs correctly, and you’re happy with the results, you can share it by copying the application bundle dist/application_name.
That’s all! Hope it was useful!
And please, don’t forget to share it with your friends, they might find it useful too 😉
Marina Mele has experience in artificial intelligence implementation and has led tech teams for over a decade. On her personal blog (marinamele.com), she writes about personal growth, family values, AI, and other topics she’s passionate about. Marina also publishes a weekly AI newsletter featuring the latest advancements and innovations in the field (marinamele.substack.com)