Python is a popular programming language that is widely used by beginners and longtime developers alike. Modern Mac OS versions come with Python 2.7.x installed (or Python 2.6.1 if an older Mac OS X version), but many Python users may need to update Python in Mac OS to a newer version like Python 3.8.x or newer. Write and run Python code using our online compiler (interpreter). You can use Python Shell like IDLE, and take inputs from the user in our Python compiler. There are some alternatives. Personally, I installed Vim on Mac OS X using MacPorts. After downloading and installing graphically the port command, you can use the following from command line to install vim with support for Python 2.7: $ sudo port install vim +python27 Notice that this also installs a Python 2.7 interpreter from MacPorts. For most Unix systems, you must download and compile the source code. The same source code archive can also be used to build the Windows and Mac versions, and is the starting point for ports to all other platforms. Download the latest Python 3 and Python 2 source. It can be difficult to install a Python machine learning environment on Mac OS X. Python itself must be installed first, and then there are many packages to install, and it can be confusing for beginners. In this tutorial, you will discover how to setup a Python 3 machine learning and deep learning development environment using macports.
Python for Mac OS X
Python comes pre-installed on Mac OS X so it is easy to startusing. However, to take advantage of the latest versions ofPython, you will need to download and install newer versionsalongside the system ones. The easiest way to do that is toinstall one of the binary installers for OS X from thePython Download page. Installers areavailable for the latest Python 3 and Python 2 releases thatwill work on all Macs that run Mac OS X 10.5 and later.
Python releases include IDLE, Python's built-in interactivedevelopment environment. If you download and install Pythonfrom the release page, you may also need to download and installa newer version of Tcl/Tk for OS X. See theIDLE and Tkinter on OS X page formore information.
You can run a Python interpreter by double-clicking onApplications / Utilities / Terminal and typing python3 (if you'veinstalled a version of Python 3) or python Print screen mac on windows. (to use Python 2) inthe window that opens up. You can also launch IDLE for the Pythonversion you have installed by double-clicking its icon inthe appropriate Python version folder in the Applications folderor you can also just type idle3 or idle in a terminal window.
There are many thousands of additional Python software packagesavailable through PyPI, the PythonPackage Index. We recommend you use thepip tool to simplifyinstalling and managing additional packages. pip is includedwith Python 3.4 releases; for earlier releases, follow the pipinstall instructions.
Among the packages available through PyPI are some that arespecifically for OS X environments. Among these are:
- pyobjc which providesa bridge between Python and Objective-C, allowing you to writefull-featured native Cocoa applications in pure Python.
- py2app which allowsyou to make standalone OS X double-clickable application bundlesand plugins from Python scripts.
For more information about Python on OS X, see the mailing list and archivesfor thePython-Macintosh Special Interest Group.
Alternative Packages for Mac OS X.
- ActiveState ActivePython(commercial and community versions, including scientific computing modules).
- Enthought Python DistributionThe Enthought Python Distribution provides scientists with a comprehensive setof tools to perform rigorous data analysis and visualization.
Python and a comprehensive set of third-party packages and libraries are alsoavailable from several open source package manager projects for OS X,including:
In this tutorial we’ll be using py2app to create a standalone OSX application from a Python 2 or 3 source code with asimple Tkinter user interface.
'py2app is a Python setuptools command which will allow you to make standalone application bundles and plugins from Python scripts. py2app is similar in purpose and design to py2exe for Windows.'
Relevant links about py2app:
- Source on BitBucket (last commit 2015-05-05)
- Issue Tracker, Mailing List
This guide is loosely based on the official tutorial.Based on a Python file called
Sandwich.py
, we’ll create an application called Sandwich.app
.Prerequisites
Create a custom directory and create a virtualenv:
Now create a very simple Tkinter app with the filename
Sandwich.py
:This little app will look like this:
Install py2app
The original version of py2app has a bug due to a newer version of ModuleGraph. Imade a fork of the project and fixed this bug on Github.Install it with pip like this:
Create a setup.py
file
py2app includes
py2applet
, a helper which generates a setup.py file for you:Python Compiler For Mac
This
setup.py
is a basic definition of the app:If your application uses some data files, like a JSON, text files or images, you should include them in DATA_FILES. For example:
Build the app for development and testing
py2app builds the standalone application based on the definition in
setup.py
.For testing and development, py2app provides an “alias mode”, which builds anapp with symbolic links to the development files:
This creates the following files and directories:
This is not a standalone application, and the applications built in alias mode are not portable to other machines!
The app built with alias mode simply references the original code files, so any changes you make to the original
Sandwich.py
file are instantly available on the next app start.The resulting development app in
dist/Sandwich.app
can be opened just like any other .app with the Finderor the open command ($ open dist/Sandwich.app
). To run your application directly from the Terminalyou can just run:Building for deployment
When everything is tested you can produce a build for deployment with a calling
python setup.py py2app
. Make sure that any old build
and dist
directories are removed:This will assemble your application as
dist/Sandwich.app
. Since this application is self-contained, you will have to run the py2app command again any time you change any source code, data files, options, etc.The original py2app has a bug which would display “
AttributeError: 'ModuleGraph' object has no attribute 'scan_code'
” or load_module
. If you encounter this error, takea look at this StackOverflow thread or use my fork of py2app.The easiest way to wrap your application up for distribution at this point is simply to right-click the application from Finder and choose “Create Archive”.
Adding an icon
Simply add
'iconfile': 'youricon.icns'
to the OPTIONS
dict:You can find free icons in icns format around the web (eg. on IconFinder or freepik).
Advanced app settings
You can tweak the application information and behaviour with modificationsto the
Info.plist
. The most complete reference for the keys available is Apple’s Runtime Configuration Guidelines.Here is an example with more modifications:
With these settings, the app will have the following infos:
References
- py2app documentation, examples
- Tkinter, Tkinter resource collection, An Introduction to Tkinter
See Also
- PyInstaller - Another tool to create cross-platform standalone apps (libraries like PyQt, Django or matplotlib are fully supported)
- rumps - Ridiculously Uncomplicated Mac OS X Python Statusbar Apps
- py2exe - same as py2app but for Windows .exe files
- cx_Freeze - Another packager to create Windows .exe files
Compiler For Python Download
If you have suggestions, feedback or ideas, please reach out to me @metachris.