1

I can use the following python statements/commands to get information about the version of Python that QGIS 3.14 supports. I use these in QGIS built-in python console

from platform import python_version
print(python_version())

import sys
print(sys.version)
print(sys.version_info) 

Is there a one-line command that will print the full path of the associated python interpreter to the python console?

2

Not really specific to QGIS, only Python stuff. Run the following to get what you want

import sys
print(sys.executable)
3
  • 1
    Thanks for this. From the python console in QGIS this returns C:\Program Files\QGIS 3.14\bin\qgis-bin.exe which is still useful information. In the meantime I found print(os.environ['PYTHONPATH']) which is a bit closer for my purposes as it returns the directory in which the python exe is located. However it places ~ in the output for some reason and seems to truncate some parts of the fullpath name
    – TrevP
    Sep 22 '20 at 9:12
  • @TrevP is it something like 'C:\PROGRA~1\QGIS3~1.4\' ? because that's normal these are old alias used for backwards compatibility. superuser.com/questions/529400/… Sep 22 '20 at 9:34
  • @ Louis Cottereau Yes exactly that!. Thanks for the explanation
    – TrevP
    Sep 22 '20 at 12:15
0

When using QGIS 3.20.3 on Ubuntu, sys.executable is the executable binary for the Python interpreter. According to the official Python documentation:

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

Working with QGIS 3.20.3 on Windows 10, sys.executable is the QGIS executable which is wrong. I opened an issue on Github: https://github.com/qgis/QGIS/issues/45646


As a workaround on Windows, you could search for the executable in sys.path.

The example below works for me in the QGIS Python console on Windows and Ubuntu.

import os
import sys

def find_python():

    if sys.platform != "win32":
        return sys.executable

    for path in sys.path:
        assumed_path = os.path.join(path, "python.exe")
        if os.path.isfile(assumed_path):
            return assumed_path

    return None

print(find_python())

Note: Not tested with virtual environments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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