8

I want to install some third-party Python modules for QGIS 3 on Mac, however, I have no clue how to do it, since I am not so much familiar with different Python environments.

Is there any similar thing to the OSGeo4W Shell on Mac, or is it possible to do via the terminal?

2
  • I ended up building the packages in another python and copying them into /Applications/QGIS3.14.app/Contents/Resources/python. Nasty. Commented Jun 24, 2020 at 9:28
  • I tried all the suggestions here without effort...
    – otmezger
    Commented May 23, 2022 at 12:28

3 Answers 3

6

from Download for Macos

These packages use the python.org Python 3, version 3.6, the "macosx10.9" build - other distributions are not supported. Install Python before installing QGIS.

1) To know the python interpreter path used by QGIS

In the Python console of QGIS

import sys
print(sys.executable)
/usr/local/bin/python3.6

2) then in the terminal

$ /usr/local/bin/python3.6
Python 3.6.8 
...
>>>

3) and you can install third-party Python modules from the terminal

/usr/local/python/bin/python3.6 -m pip install shapely

or

/usr/local/bin/python3.6 setup.py install

New

I cannot install the 3.10 version in my Mac, but if I examine the application file:

The content of /Applications/QGIS3.10.app/Contents/MacOS/

The QGIS executable is here

enter image description here

In the /Applications/QGIS3.10.app/Contents/MacOS/bin folder

enter image description here

There is a symbolic link file python3 that points to /Applications/QGIS3.10.app/QGIS3.10.app/Contents/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python

enter image description here

7
  • 2
    Hi and thanks for the answer. When I run the command sys.executable from my python console I get the following link: /Applications/QGIS3.10.app/Contents/MacOS/QGIS, and when I type it in the terminal then QGIS3 just opens. Is there an alternative python command to find the path to the python environment?
    – Pawel
    Commented Feb 20, 2020 at 16:00
  • 1
    In the Python console of QGIS ? (because /Applications/QGIS3.10.app/Contents/MacOS/QGIS is the executable of QGIS, not of Python) -> see New above
    – gene
    Commented Feb 20, 2020 at 17:50
  • It still doesn't work for me when I enter these directories and type python -m pip install 'package'. It's like the python that is being called is the default python installed on Mac and not the python installed in the QGIS3 environment... Is there a terminal command to make sure that the python and pip called is the one running in QGIS3, and not the other one (though the versions are the same)?
    – Pawel
    Commented Feb 21, 2020 at 15:45
  • The one running in QGIS3 is the sys.executable result in the QGIS3 Python console (the default python installed on Mac is version 2.x)
    – gene
    Commented Feb 21, 2020 at 16:08
  • I remember that I had once changed the main python of the mac from 2 to 3, but how do I call the one in the python console from the terminal?
    – Pawel
    Commented Feb 21, 2020 at 18:17
4

Tested on QGIS 3.14 under MacOSX 10.15.

In QGIS, go to menu Settings > System and search for Environment options:

enter image description here

Check the Use custom variables checkbox, and :

  • In the Apply column, choose Append ;
  • In the Variable column, write PYTHONPATH ;
  • In the Value column, write the path to the directory with python modules as :/Users/me/Library/Python/3.8/lib/python/site-packages

Beware, to correctly separate environment variables, don't forget the colon.

Restart QGIS and in the QGIS Python console, try to import your module.

1
  • I did as you suggested and pointed to a conda env's site-packages. Still get an ImportError Library not loaded. Any help?
    – nospec
    Commented Feb 6, 2023 at 18:01
2

For Windows users, the answers above are probably still fine, but for MacOS users who don't want to re-point their PYTHONPATH to an external Python, this answer has changed slightly for the bundled Python distribution in recent QGIS packages for MacOS.

The pip3 command is now found here after a default install (adjacent to the bundled python):

/Applications/QGIS-LTR.app/Contents/MacOS/bin/pip3

If you open a terminal and type (for example to install ptvsd):

/Applications/QGIS-LTR.app/Contents/MacOS/bin/pip3 install ptvsd

you should get the result you're hoping for.

You can verify the library was installed in your Python library repository here:

/Applications/QGIS-LTR.app/Contents/Resources/python/site-packages

(I have not added this a comment because I don't really use this platform and don't have the minimum rep.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.