QGIS with Python 3 on Mac – How to Set Up and Use

python 3qgis

I need to start learning Python, and I want to learn with version 3.x. I understand that QGIS–at least some plug-ins–relies on Python and Tcl/Tk, and probably on specific versions. Assuming that QGIS is dependent on Python 2, can anyone tell me how to set up parallel installations of Python 3 and Tcl/Tk such that QGIS will have what it needs and I can use Python 3 for other purposes?

Best Answer

pyrogerg, you can have several versions of Python on your Mac (Mac OS X's, Macports, python.org framework, etc.) and it will not mess with QGIS, or any of the other versions of Python (older, but still relevant, similar question). You can also switch between them, or call a specific-version interpreter in Terminal (e.g. /usr/bin/python2.5), and some folks have made it easier on themselves. In fact, your Mac has several versions of Python already installed. Check /usr/bin. There is 2.5, 2.6, and 2.7 installed with Lion.

Concerning whether a Python 3 install will affect QGIS... basically no. If you are using the KyngChaos.com QGIS installs, they are compiled against the most current version of Python on the stock Mac OS X version they are built for (2.7 for Lion). A Python 3 (or 2.5) interpreter will not be used for the QGIS's internal Python environment, only the one it was compiled against.

Modules installed for the Python-specific version (here for stock Lion)...

/Library/Python/2.7/site-packages

will be available in QGIS, but plugin-local packages or modules of the same name will be loaded before them, as the plugin location paths (e.g. ~/.qgis/python/plugins/) precede site-packages on sys.path.

Things change if you want to compile QGIS yourself (to use latest master branch from github.com repository). Then you need to be specific when choosing the version of Python for which to build the app. Python 3 is not recommended at this time. See Mac section #5 of INSTALL.


Concerning the GUI toolkit used when building PyQGIS plugins, it is PyQt (not Tcl/Tk), which is a Python binding for Nokia's Qt C++ GUI toolkit, which is what QGIS is built with. The KyngChaos.com QGIS.apps are bundled with most of the Qt frameworks they were compiled against. See:

/Applications/QGIS.app/Contents/Frameworks  <-- 'Show Package Contents' of app

This means that you can also have different versions of Qt installed on your Mac as well, without affecting QGIS's build. Rarely there are issues with plugins 'seeing' two Qt installs and not knowing which to use, but it doesn't mean the plugin will crash.

If you are curious about building the current master branch of QGIS, and have a build environment setup as per INSTALL, try out this no frills script for automating the build.

Have fun learning Python. Certainly nothing wrong with learning 2.x and then move to 3.x. There are many, many apps, modules and plugins out there written with 2.x. If you are looking for a good IDE to learn Python, especially PyQt, try Eric4 or 5. It is actually built from PyQt as well.

Related Question