This chapter is a quick guide to setting up Python 2.7, OpenCV, and related libraries. After setup, we also look at OpenCV's Python sample scripts and documentation.
The following related libraries are covered:
OpenCV for Mac focuses mainly towards real-time image processing, as such, if it finds Intel's Integrated Performance Primitives on the system, it will use these commercial optimized routines to accelerate itself. OpenCV library supports: Real-time capture. Video file import. Object detection. Basic image treatment: brightness, contrast, threshold. Pip install -no-binary opencv-python opencv-python; pip install -no-binary:all: opencv-python; If you need contrib modules or headless version, just change the package name (step 4 in the previous section is not needed). However, any additional CMake flags can be provided via environment variables as described in step 3 of the manual build. Hello, I'm trying to install opencv using this tutorial. The problem opencv can install though in the lib/ there is no python2.7 folder. I'm using OSX Yosemite. Any input is welcome. I can provide any other detail, if you guys need. Hello everybody. I would appreciate if someone can help me with this. I have installed Opencv with python on a windows PC. No problem, I am trying to do the same on a mac. (snow leopard) However, I did the following: -Installed python 2.7 ( i know mac comes with python but i got recommended that i had to reinstall it) -Installed Numpy So if I do something like 'import numpy as np' in python.
NumPy: This is a dependency of OpenCV's Python bindings. It provides numeric computing functionality, including efficient arrays.
SciPy: This is a scientific computing library that is closely related to NumPy. It is not required by OpenCV but it is useful for manipulating the data in OpenCV images.
OpenNI: This is an optional dependency of OpenCV. It adds support for certain depth cameras, such as Asus XtionPRO.
SensorKinect: This is an OpenNI plugin and optional dependency of OpenCV. It adds support for the Microsoft Kinect depth camera.
For this book's purposes, OpenNI and SensorKinect can be considered optional. They are used throughout Chapter 5, Separating Foreground/Background Regions Depth, but are not used in the other chapters or appendices.
At the time of writing, OpenCV 2.4.3 is the latest version. On some operating systems, it is easier to set up an earlier version (2.3.1). The differences between these versions should not affect the project that we are going to build in this book.
Some additional information, particularly about OpenCV's build options and their dependencies, is available in the OpenCV wiki at http://opencv.willowgarage.com/wiki/InstallGuide. However, at the time of writing, the wiki is not up-to-date with OpenCV 2.4.3.
We are free to choose among various setup tools, depending on our operating system and how much configuration we want to do. Let's take an overview of the tools for Windows, Mac, Ubuntu, and other Unix-like systems.
Windows does not come with Python preinstalled. However, installation wizards are available for precompiled Python, NumPy, SciPy, and OpenCV. Alternatively, we can build from source. OpenCV's build system uses CMake for configuration and either Visual Studio or MinGW for compilation.
If we want support for depth cameras including Kinect, we should first install OpenNI and SensorKinect, which are available as precompiled binaries with installation wizards. Then, we must build OpenCV from source.
The precompiled version of OpenCV does not offer support for depth cameras.
On Windows, OpenCV offers better support for 32-bit Python than 64-bit Python. Even if we are building from source, I recommend using 32-bit Python. Fortunately, 32-bit Python works fine on either 32-bit or 64-bit editions of Windows.
Some of the following steps refer to editing the system's Path
variable. This task can be done in the Environment Variables window of Control Panel.
On Windows Vista/Windows 7/Windows 8, open the Start menu and launch Control Panel. Now, go to System and SecuritySystemAdvanced system settings. Click on the Environment Variables button.
On Windows XP, open the Start menu and go to Control PanelSystem. Select the Advanced tab. Click on the Environment Variables button.
Now, under System variables, select Path and click on the Edit button. Make changes as directed. To apply the changes, click on all the OK buttons (until we are back in the main window of Control Panel). Then, log out and log back in (alternatively, reboot).
Here are the steps to set up 32-bit Python 2.7, NumPy, and OpenCV:
Download and install 32-bit Python 2.7.3 from http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi.
Download and install NumPy 1.6.2 from http://sourceforge.net/projects/numpy/files/NumPy/1.6.2/numpy-1.6.2-win32-superpack-python2.7.exe/download.
Download and install SciPy 11.0 from http://sourceforge.net/projects/scipy/files/scipy/0.11.0/scipy-0.11.0-win32-superpack-python2.7.exe/download.
Download the self-extracting ZIP of OpenCV 2.4.3 from http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.3/OpenCV-2.4.3.exe/download. Run the self-extracting ZIP and, when prompted, enter any destination folder, which we will refer to as <unzip_destination>
. A subfolder, <unzip_destination>opencv
, is created.
Copy <unzip_destination>opencvbuildpython2.7cv2.pyd
to C:Python2.7Libsite-packages
(assuming we installed Python 2.7 to the default location). Now, the new Python installation can find OpenCV.
A final step is necessary if we want Python scripts to run using the new Python installation by default. Edit the system's Path
variable and append ;C:Python2.7
(assuming we installed Python 2.7 to the default location). Remove any previous Python paths, such as ;C:Python2.6
. Log out and log back in (alternatively, reboot).
Windows does not come with any compilers or CMake. We need to install them. If we want support for depth cameras, including Kinect, we also need to install OpenNI and SensorKinect.
Let's assume that we have already installed 32-bit Python 2.7, NumPy, and SciPy either from binaries (as described previously) or from source. Now, we can proceed with installing compilers and CMake, optionally installing OpenNI and SensorKinect, and then building OpenCV from source:
Download and install CMake 2.8.9 from http://www.cmake.org/files/v2.8/cmake-2.8.9-win32-x86.exe. When running the installer, select either Add CMake to the system PATH for all users or Add CMake to the system PATH for current user.
Download and install Microsoft Visual Studio 2010, Microsoft Visual C++ Express 2010, or MinGW. Note that OpenCV 2.4.3 cannot be compiled with the more recent versions (Microsoft Visual Studio 2012 and Microsoft Visual Studio Express 2012).
For Microsoft Visual Studio 2010, use any installation media you purchased. During installation, include any optional C++ components. Reboot after installation is complete.
For Microsoft Visual C++ Express 2010, get the installer from http://www.microsoft.com/visualstudio/eng/downloads. Reboot after installation is complete.
For MinGW get the installer from http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20120426/mingw-get-inst-20120426.exe/download. When running the installer, make sure that the destination path does not contain spaces and that the optional C++ compiler is included. Edit the system's Path
variable and append ;C:MinGWbin
(assuming MinGW is installed to the default location.) Reboot the system.
Optionally, download and install OpenNI 1.5.4.0 from http://www.openni.org/wp-content/uploads/2012/12/OpenNI-Win32-1.5.4.0-Dev1.zip (32 bit). Alternatively, for 64-bit Python, use http://www.openni.org/wp-content/uploads/2012/12/OpenNI-Win64-1.5.4.0-Dev.zip (64 bit).
Optionally, download and install SensorKinect 0.93 from https://github.com/avin2/SensorKinect/blob/unstable/Bin/SensorKinect093-Bin-Win32-v5.1.2.1.msi?raw=true (32 bit). Alternatively, for 64-bit Python, use https://github.com/avin2/SensorKinect/blob/unstable/Bin/SensorKinect093-Bin-Win64-v5.1.2.1.msi?raw=true (64 bit).
Download the self-extracting ZIP of OpenCV 2.4.3 from http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.3/OpenCV-2.4.3.exe/download. Run the self-extracting ZIP and, when prompted, enter any destination folder, which we will refer to as <unzip_destination>
. A subfolder, <unzip_destination>opencv
, is created.
Open Command Prompt and make another folder where our build will go:
Change directory to the build folder:
Now, we are ready to configure our build. To understand all the options, we could read the code in <unzip_destination>opencvCMakeLists.txt
. However, for this book's purposes, we only need to use the options that will give us a release build with Python bindings and, optionally, depth camera support via OpenNI and SensorKinect.
For Visual Studio 2010 or Visual C++ Express 2010, run:
Alternatively, for MinGW, run:
If OpenNI is not installed, omit -D:WITH_OPENNI=ON
. (In this case, depth cameras will not be supported.) If OpenNI and SensorKinect are installed to non-default locations, modify the command to include -D:OPENNI_LIB_DIR=<openni_install_destination>Lib -D:OPENNI_INCLUDE_DIR=<openni_install_destination>Include -D:OPENNI_PRIME_SENSOR_MODULE_BIN_DIR=<sensorkinect_install_destination>SensorBin
.
CMake might report that it has failed to find some dependencies. Many of OpenCV's dependencies are optional; so, do not be too concerned yet. If the build fails to complete or you run into problems later, try installing missing dependencies (often available as prebuilt binaries) and then rebuild OpenCV from this step.
Having configured our build system, we are ready to compile.
For Visual Studio or Visual C++ Express, open <build_folder>/OpenCV.sln
. Select Release
configuration and build. If you get build errors, double-check that Release
configuration is selected.
Alternatively, for MinGW, run:
Copy <build_folder>libReleasecv2.pyd
(from a Visual Studio build) or <build_folder>libcv2.pyd
(from a MinGW build) to C:Python2.7Libsite-packages
(assuming Python 2.7 is installed to the default location). Now, the Python installation can find part of OpenCV.
Finally, we need to make sure that Python and other processes can find the rest of OpenCV. Edit the system's Path
variable and append ;<build_folder>/bin/Release
(for a Visual Studio build) or ;<build_folder>/bin
(for a MinGW build). Reboot your system.
Some versions of Mac come with Python 2.7 preinstalled. However, the preinstalled Python is customized by Apple for the system's internal needs. Normally, we should not install any libraries atop Apple's Python. If we do, our libraries might break during system updates or, worse, might conflict with preinstalled libraries that the system requires. Instead, we should install standard Python 2.7 and then install our libraries atop it.
For Mac, there are several possible approaches to obtaining standard Python 2.7, NumPy, SciPy, and OpenCV. All approaches ultimately require OpenCV to be compiled from source using Xcode Developer Tools. However, depending on the approach, this task is automated for us by third-party tools in various ways. We will look at approaches using MacPorts or Homebrew. These tools can potentially do everything that CMake can do, plus they help us resolve dependencies and separate our development libraries from the system libraries.
I recommend MacPorts, especially if you want to compile OpenCV with depth camera support via OpenNI and SensorKinect. Relevant patches and build scripts, including some that I maintain, are ready-made for MacPorts. By contrast, Homebrew does not currently provide a ready-made solution for compiling OpenCV with depth camera support.
Before proceeding, let's make sure that the Xcode Developer Tools are properly set up:
Download and install Xcode from the Mac App Store or http://connect.apple.com/. During installation, if there is an option to install Command Line Tools, select it.
Open Xcode and accept the license agreement.
A final step is necessary if the installer did not give us the option to install Command Line Tools. Go to XcodePreferencesDownloads and click on the Install button next to Command Line Tools. Wait for the installation to finish and quit Xcode.
Now we have the required compilers for any approach.
We can use the MacPorts package manager to help us set up Python 2.7, NumPy, and OpenCV. MacPorts provides Terminal commands that automate the process of downloading, compiling, and installing various pieces of open source software (OSS). MacPorts also installs dependencies as needed. For each piece of software, the dependencies and build recipe are defined in a configuration file called a Portfile. A MacPorts repository is a collection of Portfiles.
Starting from a system where Xcode and its Command Line Tools are already set up, the following steps will give us an OpenCV installation via MacPorts:
Download and install MacPorts from http://www.macports.org/install.php.
If we want support for the Kinect depth camera, we need to tell MacPorts where to download some custom Portfiles that I have written. To do so, edit /opt/local/etc/macports/sources.conf
(assuming MacPorts is installed to the default location). Just above the line rsync://rsync.macports.org/release/ports/ [default]
, add the following line:
Save the file. Now, MacPorts knows to search for Portfiles in my online repository first and, then, the default online repository.
Open Terminal and run the following command to update MacPorts:
When prompted, enter your password.
Now (if we are using my repository), run the following command to install OpenCV with Python 2.7 bindings and support for depth cameras including Kinect:
Alternatively (with or without my repository), run the following command to install OpenCV with Python 2.7 bindings and support for depth cameras excluding Kinect:
Dependencies, including Python 2.7, NumPy, OpenNI, and (in the first example) SensorKinect, are automatically installed as well.
By adding +python27
to the command, we are specifying that we want the opencv
variant (build configuration) with Python 2.7 bindings. Similarly, +openni_sensorkinect
specifies the variant with the broadest possible support for depth cameras via OpenNI and SensorKinect. You may omit +openni_sensorkinect
if you do not intend to use depth cameras or you may replace it with +openni
if you do intend to use OpenNI-compatible depth cameras but just not Kinect. To see the full list of available variants before installing, we can enter:
Depending on our customization needs, we can add other variants to the install
command. For even more flexibility, we can write our own variants (as described in the next section).
Also, run the following command to install SciPy:
The Python installation's executable is named python2.7
. If we want to link the default python
executable to python2.7
, let's also run:
With a few extra steps, we can change the way that MacPorts compiles OpenCV or any other piece of software. As previously mentioned, MacPorts' build recipes are defined in configuration files called Portfiles. By creating or editing Portfiles, we can access highly configurable build tools, such as CMake, while also benefitting from MacPorts' features, such as dependency resolution.
Let's assume that we already have MacPorts installed. Now, we can configure MacPorts to use custom Portfiles that we write:
Create a folder somewhere to hold our custom Portfiles. We will refer to this folder as <local_repository>
.
Edit the file /opt/local/etc/macports/sources.conf
(assuming MacPorts is installed to the default location). Just above the line rsync://rsync.macports.org/release/ports/ [default]
, add this line:
For example, if <local_repository>
is /Users/Joe/Portfiles
, add:
Note the triple slashes.
Save the file. Now, MacPorts knows to search for Portfiles in <local_repository>
first and, then, its default online repository.
Open Terminal and update MacPorts to ensure that we have the latest Portfiles from the default repository:
Let's copy the default repository's opencv
Portfile as an example. We should also copy the directory structure, which determines how the package is categorized by MacPorts.
Alternatively, for an example that includes Kinect support, we could download my online repository from http://nummist.com/opencv/ports.tar.gz, unzip it and copy its entire graphics
folder into <local_repository>
:
Edit <local_repository>/graphics/opencv/Portfile
. Note that this file specifies CMake configuration flags, dependencies, and variants. For details on Portfile editing, go to http://guide.macports.org/#development.
To see which CMake configuration flags are relevant to OpenCV, we need to look at its source code. Download the source code archive from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download
, unzip it to any location, and read <unzip_destination>/OpenCV-2.4.3/CMakeLists.txt
.
After making any edits to the Portfile, save it.
Now, we need to generate an index file in our local repository so that MacPorts can find the new Portfile:
From now on, we can treat our custom opencv
just like any other MacPorts package. For example, we can install it as follows:
Note that our local repository's Portfile takes precedence over the default repository's Portfile because of the order in which they are listed in /opt/local/etc/macports/sources.conf
.
Homebrew is another package manager that can help us. Normally, MacPorts and Homebrew should not be installed on the same machine.
/epson-printer-document-capture-scan-mac-download.html. Starting from a system where Xcode and its Command Line Tools are already set up, the following steps will give us an OpenCV installation via Homebrew:
Open Terminal and run the following command to install Homebrew:
Unlike MacPorts, Homebrew does not automatically put its executables in PATH
. To do so, create or edit the file ~/.profile
and add this line at the top:
Save the file and run this command to refresh PATH
:
Note that executables installed by Homebrew now take precedence over executables installed by the system.
For Homebrew's self-diagnostic report, run:
Follow any troubleshooting advice it gives.
Now, update Homebrew:
Run the following command to install Python 2.7:
Now, we can install NumPy. Homebrew's selection of Python library packages is limited so we use a separate package management tool called pip, which comes with Homebrew's Python:
SciPy contains some Fortran code, so we need an appropriate compiler. We can use Homebrew to install the gfortran
compiler:
Now, we can install SciPy:
To install OpenCV on a 64-bit system (all new Mac hardware since late 2006), run:
Alternatively, to install OpenCV on a 32-bit system, run:
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com
. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support
and register to have the files e-mailed directly to you.
Homebrew makes it easy to edit existing package definitions:
The package definitions are actually scripts in the Ruby programming language. Tips on editing them can be found in the Homebrew wiki at https://github.com/mxcl/homebrew/wiki/Formula-Cookbook. A script may specify Make or CMake configuration flags, among other things.
To see which CMake configuration flags are relevant to OpenCV, we need to look at its source code. Download the source code archive from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download, unzip it to any location, and read <unzip_destination>/OpenCV-2.4.3/CMakeLists.txt
.
After making any edits to the Ruby script, save it.
The customized package can be treated as normal. For example, it can be installed as follows:
Ubuntu comes with Python 2.7 preinstalled. The standard Ubuntu repository contains OpenCV 2.3.1 packages without support for depth cameras. Alternatively, OpenCV 2.4.3 can be built from source using CMake and GCC. When built from source, OpenCV can support depth cameras via OpenNI and SensorKinect, which are available as precompiled binaries with installation scripts.
We can install OpenCV 2.3.1 and its dependencies using the Apt package manager:
Open Terminal and run this command to update Apt:
Now, run these commands to install NumPy, SciPy, and OpenCV with Python bindings:
Enter Y
whenever prompted about package installation.
Equivalently, we could have used Ubuntu Software Center, which is Apt's graphical frontend.
Ubuntu comes with the GCC compilers preinstalled. However, we need to install the CMake build system. We also need to install or reinstall various other libraries, some of which need to be specially configured for compatibility with OpenCV. Because the dependencies are complex, I have written a script that downloads, configures, and builds OpenCV and related libraries so that the resulting OpenCV installation has support for depth cameras including Kinect:
Download my installation script from http://nummist.com/opencv/install_opencv_ubuntu.sh and put it in any destination, say <script_folder>
.
Optionally, edit the script to customize OpenCV's build configuration. To see which CMake configuration flags are relevant to OpenCV, we need to look at its source code. Download the source code archive from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download, unzip it to any location, and read <unzip_destination>/OpenCV-2.4.3/CMakeLists.txt
.
After making any edits to the script, save it.
Open Terminal and run this command to update Apt:
Change directory to <script_folder>
:
Set the script's permissions so that it is executable:
Execute the script:
When prompted, enter your password. Enter Y
whenever prompted about package installation.
The installation script creates a folder, <script_folder>/opencv
, which contains downloads and built files that are temporarily used by the script. After the installation script terminates, <script_folder>/opencv
may safely be deleted; although, first, you might want to look at OpenCV's Python samples in <script_folder>/opencv/samples/python
and <script_folder>/opencv/samples/python2
.
The approaches for Ubuntu (as described previously) are likely to work on any Linux distribution derived from Ubuntu 12.04 LTS or Ubuntu 12.10, such as:
Kubuntu 12.04 LTS or Kubuntu 12.10
Xubuntu 12.04 LTS or Xubuntu 12.10
Linux Mint 13 or Linux Mint 14
On Debian Linux and its derivatives, the Apt package manager works the same as on Ubuntu, though the available packages may differ.
On Gentoo Linux and its derivatives, the Portage package manager is similar to MacPorts (as described previously), though the available packages may differ.
On other Unix-like systems, the package manager and available packages may differ. Consult your package manager's documentation and search for any packages with opencv
in their names. Remember that OpenCV and its Python bindings might be split into multiple packages.
Also, look for any installation notes published by the system provider, the repository maintainer, or the community. Because OpenCV uses camera drivers and media codecs, getting all of its functionality to work can be tricky on systems with poor multimedia support. Under some circumstances, system packages might need to be reconfigured or reinstalled for compatibility.
If packages are available for OpenCV, check their version number. OpenCV 2.3.1 or greater is recommended for this book's purposes. Also check whether the packages offer Python bindings and whether they offer depth camera support via OpenNI and SensorKinect. Finally, check whether anyone in the developer community has reported success or failure in using the packages.
If instead we want to do a custom build of OpenCV from source, it might be helpful to refer to the installation script for Ubuntu (discussed previously) and adapt it to the package manager and packages that are present on another system.
Running a few sample scripts is a good way to test that OpenCV is correctly set up. The samples are included in OpenCV's source code archive.
On Windows, we should have already downloaded and unzipped OpenCV's self-extracting ZIP. Find the samples in <unzip_destination>/opencv/samples
.
On Unix-like systems, including Mac, download the source code archive from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download and unzip it to any location (if we have not already done so). Find the samples in <unzip_destination>/OpenCV-2.4.3/samples
.
Some of the sample scripts require command-line arguments. However, the following scripts (among others) should work without any arguments:
python/camera.py
: This displays a webcam feed (assuming a webcam is plugged in).
python/drawing.py
: This draws a series of shapes, like a screensaver.
python2/hist.py
: This displays a photo. Press A, B, C, D, or E to see variations of the photo, along with a corresponding histogram of color or grayscale values.
python2/opt_flow.py
(missing from the Ubuntu package): This displays a webcam feed with a superimposed visualization of optical flow (direction of motion). For example, slowly wave your hand at the webcam to see the effect. Press 1 or 2 for alternative visualizations.
To exit a script, press Esc (not the window's close button).
If we encounter the message, ImportError: No module named cv2.cv
, then we are running the script from a Python installation that does not know anything about OpenCV. There are two possible explanations:
Some steps in the OpenCV installation might have failed or been missed. Go back and review the steps.
If we have multiple Python installations on the machine, we might be using the wrong Python to launch the script. For example, on Mac, it might be the case that OpenCV is installed for MacPorts Python but we are running the script with the system's Python. Go back and review the installation steps about editing the system path. Also, try launching the script manually from the command line using commands such as:
You can also use the following command:
As another possible means of selecting a different Python installation, try editing the sample script to remove #!
lines. These lines might explicitly associate the script with the wrong Python installation (for our particular setup).
OpenCV's documentation is online at http://docs.opencv.org/. The documentation includes a combined API reference for OpenCV's new C++ API, its new Python API (which is based on the C++ API), its old C API, and its old Python API (which is based on the C API). When looking up a class or function, be sure to read the section about the new Python API (cv2
module), not the old Python API (cv
module).
The documentation entitled OpenCV 2.1 Python Reference (http://opencv.willowgarage.com/documentation/python/) might show up in Google searches for OpenCV Python API
. Avoid this documentation, since it is out-of-date and covers only the old (C-like) Python API.
The documentation is also available as several downloadable PDF files:
API reference: http://docs.opencv.org/opencv2refman
Tutorials: http://docs.opencv.org/opencv_tutorials (These tutorials use C++ code. For a Python port of the tutorials' code, see Abid Rahman K.'s repository at http://goo.gl/EPsD1.)
User guide (incomplete): http://docs.opencv.org/opencv_user
If you write code on airplanes or other places without Internet access, you will definitely want to keep offline copies of the documentation.
If the documentation does not seem to answer your question, try talking to the OpenCV community. Here are some sites where you will find helpful people:
Official OpenCV forum: http://www.answers.opencv.org/questions/
Blog of David Millán Escrivá (one of this book's reviewers): http://blog.damiles.com/
Blog of Abid Rahman K. (one of this book's reviewers): http://www.opencvpython.blogspot.com/
My site for this book: http://nummist.com/opencv/
Last, if you are an advanced user who wants to try new features, bug-fixes, and sample scripts from the latest (unstable) OpenCV source code, have a look at the project's repository at https://github.com/Itseez/opencv/.
By now, we should have an OpenCV installation that can do everything we need for the project described in this book. Depending on which approach we took, we might also have a set of tools and scripts that are usable to reconfigure and rebuild OpenCV for our future needs.
We know where to find OpenCV's Python samples. These samples cover a different range of functionality than this book's project, but they are useful as additional learning aids.
Bob Savage <bobsavage@mac.com>
Python on a Macintosh running Mac OS X is in principle very similar to Python onany other Unix platform, but there are a number of additional features such asthe IDE and the Package Manager that are worth pointing out.
Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, youare invited to install the most recent version of Python 3 from the Pythonwebsite (https://www.python.org). A current “universal binary” build of Python,which runs natively on the Mac’s new Intel and legacy PPC CPU’s, is availablethere.
What you get after installing is a number of things:
A Python3.9
folder in your Applications
folder. In hereyou find IDLE, the development environment that is a standard part of officialPython distributions; and PythonLauncher, which handles double-clicking Pythonscripts from the Finder.
A framework /Library/Frameworks/Python.framework
, which includes thePython executable and libraries. The installer adds this location to your shellpath. To uninstall MacPython, you can simply remove these three things. Asymlink to the Python executable is placed in /usr/local/bin/.
The Apple-provided build of Python is installed in/System/Library/Frameworks/Python.framework
and /usr/bin/python
,respectively. You should never modify or delete these, as they areApple-controlled and are used by Apple- or third-party software. Remember thatif you choose to install a newer Python version from python.org, you will havetwo different but functional Python installations on your computer, so it willbe important that your paths and usages are consistent with what you want to do.
IDLE includes a help menu that allows you to access Python documentation. If youare completely new to Python you should start reading the tutorial introductionin that document.
If you are familiar with Python on other Unix platforms you should read thesection on running Python scripts from the Unix shell.
Your best way to get started with Python on Mac OS X is through the IDLEintegrated development environment, see section The IDE and use the Help menuwhen the IDE is running.
If you want to run Python scripts from the Terminal window command line or fromthe Finder you first need an editor to create your script. Mac OS X comes with anumber of standard Unix command line editors, vim andemacs among them. If you want a more Mac-like editor,BBEdit or TextWrangler from Bare Bones Software (seehttp://www.barebones.com/products/bbedit/index.html) are good choices, as isTextMate (see https://macromates.com/). Other editors includeGvim (http://macvim-dev.github.io/macvim/) and Aquamacs(http://aquamacs.org/).
To run your script from the Terminal window you must make sure that/usr/local/bin
is in your shell search path.
To run your script from the Finder you have two options:
Drag it to PythonLauncher
Select PythonLauncher as the default application to open yourscript (or any .py script) through the finder Info window and double-click it.PythonLauncher has various preferences to control how your script islaunched. Option-dragging allows you to change these for one invocation, or useits Preferences menu to change things globally.
With older versions of Python, there is one Mac OS X quirk that you need to beaware of: programs that talk to the Aqua window manager (in other words,anything that has a GUI) need to be run in a special way. Use pythonwinstead of python to start such scripts.
With Python 3.9, you can use either python or pythonw.
Python on OS X honors all standard Unix environment variables such asPYTHONPATH
, but setting these variables for programs started from theFinder is non-standard as the Finder does not read your .profile
or.cshrc
at startup. You need to create a file~/.MacOSX/environment.plist
. See Apple’s Technical Document QA1067 fordetails.
For more information on installation Python packages in MacPython, see sectionInstalling Additional Python Packages.
MacPython ships with the standard IDLE development environment. A goodintroduction to using IDLE can be found athttp://www.hashcollision.org/hkn/python/idle_intro/index.html.
There are several methods to install additional Python packages:
Packages can be installed via the standard Python distutils mode (pythonsetup.pyinstall
).
Many packages can also be installed via the setuptools extensionor pip wrapper, see https://pip.pypa.io/.
There are several options for building GUI applications on the Mac with Python.
PyObjC is a Python binding to Apple’s Objective-C/Cocoa framework, which isthe foundation of most modern Mac development. Information on PyObjC isavailable from https://pypi.org/project/pyobjc/.
The standard Python GUI toolkit is tkinter
, based on the cross-platformTk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OSX by Apple, and the latest version can be downloaded and installed fromhttps://www.activestate.com; it can also be built from source.
wxPython is another popular cross-platform GUI toolkit that runs natively onMac OS X. Packages and documentation are available from https://www.wxpython.org.
PyQt is another popular cross-platform GUI toolkit that runs natively on MacOS X. More information can be found athttps://riverbankcomputing.com/software/pyqt/intro.
The standard tool for deploying standalone Python applications on the Mac ispy2app. More information on installing and using py2app can be foundat http://undefined.org/python/#py2app.
The MacPython mailing list is an excellent support resource for Python users anddevelopers on the Mac:
Another useful resource is the MacPython wiki: