QGIS – Understanding Differences Between Python Versions Installed

pythonqgisqgis-3windows 10

There is QGIS 3.20.3 installed on the Windows 10 in the C:/Program Files/.

qgis_window

What is a core difference between all Pythons delivered together with QGIS? What is the purpose of each one and what are the responsibilities?

For instance, I could find Python in:

  • C:/Program Files/QGIS 3.20.3/apps/Python39
    path_1
  • C:/Program Files/QGIS 3.20.3/bin
    path_2
  • C:/Program Files/QGIS 3.20.3/apps/qgis/python
    path_3
  • C:/Users/.../AppData/Roaming/QGIS/QGIS3/profiles/default/python
    path_4

When I launched the Python Console and ran this code:

>>>import os
>>>import sys

>>>os.path.dirname(sys.executable)

I could get the following answer: C:\\Program Files\\QGIS 3.20.3\\bin this is a Python from /bin. Under this absolute path placed the executable binary for the Python interpreter.

However, when I typed this

>>>import os

>>>print(os.__file__)

I got this C:\\PROGRA~1\\QGIS32~1.3\\apps\\Python39\\lib\\os.py which is now a Python from /apps.

When I used this

>>>import qgis.core
   
>>>print(qgis.core.__file__)

I achieved his C:\PROGRA~1/QGIS32~1.3/apps/qgis/./python\qgis\core\__init__.py.

So, now I am wondering how to understand this Python labyrinth inside the QGIS.

Best Answer

Under Windows :

  • OSGeo_root_directory/apps/Python39

    This is where the Python program is installed, in this case the 3.9 version.

  • OSGeo_root_directory/bin

    This is just a directory with links, shortcuts for QGIS executables (with environment variable settings, etc.), in others, Python.

  • OSGeo_root_directory/apps/qgis/python

    This is where QGIS PyQGIS and Python core modules are, they work with Python and extend QGIS functions with Python.

  • %APPDATA%/QGIS/QGIS3/profiles/default/python

    This is where your custom Python expressions (extended QGIS expressions functionality) and installed plugins are stored.

In Python, you can have multiple directories (paths) where modules are installed and where Python search.

So when you import the module os, standard library, Python returns the path from the Python App, under the Lib directory where other standard libraries are installed.

os_lib

When you import qgis.core, you call the qgis module, that extents Python with QGIS Python API and it's located under the apps/qgis/python directory.

qgis.core