Last active
August 4, 2022 10:44
-
-
Save moosetraveller/a5b2117db5a9207bcfedf2927d127ce2 to your computer and use it in GitHub Desktop.
Get Python Path to Executable within the QGIS Python Console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using sys.path | |
# see also https://github.com/qgis/QGIS/issues/45646#issuecomment-950339443 | |
import os | |
import sys | |
import subprocess | |
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 | |
raise Exception("Python executable not found") | |
python_exe = find_python() | |
subprocess.check_call([python_exe, "-m", "pip", "install", "geojson"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using qgis.core.QgsApplication.libraryPaths() | |
# not sure that this is the best way | |
import os | |
from sys import platform | |
import qgis.core as qc | |
def get_python_path(): | |
# QgsApplication > QApplication > QGuiApplication > QCoreApplication | |
# https://doc.qt.io/qt-5/qcoreapplication.html#libraryPaths | |
# https://doc.qt.io/qt-5/qt-conf.html | |
# https://github.com/qgis/QGIS/blob/f401ec2c221f2711e8042e750c0560debb48e34e/src/app/main.cpp#L1086 | |
for path in qc.QgsApplication.libraryPaths(): | |
assumed_path = os.path.join(path, "python3") # linux | |
if platform == "win32": | |
assumed_path = os.path.join(path, "python.exe") | |
if os.path.isfile(assumed_path): | |
return assumed_path | |
return None | |
print(get_python_path()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment