Skip to content

Commit df679a2

Browse files
committed
Set paths in qgis python module based on osgeo4w env file
1 parent b5ae888 commit df679a2

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

python/__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,53 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
from builtins import zip
2019

2120
__author__ = 'Martin Dobias'
2221
__date__ = 'January 2007'
2322
__copyright__ = '(C) 2007, Martin Dobias'
2423
# This will get replaced with a git SHA1 when you do a git archive
2524
__revision__ = '$Format:%H$'
2625

26+
from builtins import zip
27+
import os
28+
29+
def setupenv():
30+
"""
31+
Set the environment for Windows based on the .vars files from the
32+
OSGeo4W package format.
33+
"""
34+
# If the prefix path is already set the we don't do any more path setup.
35+
if os.getenv('QGIS_PREFIX_PATH'):
36+
return
37+
38+
# Setup the paths based on the .vars file.
39+
from pathlib import PurePath
40+
41+
path_split = PurePath(os.path.dirname(os.path.realpath(__file__))).parts
42+
43+
try:
44+
appname = os.environ['QGIS_ENVNAME']
45+
except KeyError:
46+
appname = path_split[-3]
47+
48+
envfile= list(path_split[:-4])
49+
envfile.append("bin")
50+
envfile.append("{0}-bin.env".format(appname))
51+
envfile = os.path.join(*envfile)
52+
with open(envfile) as f:
53+
for line in f:
54+
linedata = line.split("=")
55+
name = linedata[0]
56+
data = linedata[1]
57+
os.environ[name] = data
58+
59+
60+
if os.name == 'nt':
61+
# On windows we need to setup the paths before we can import
62+
# any of the QGIS modules or else it will error.
63+
setupenv()
64+
65+
2766
from qgis.PyQt import QtCore
2867
from qgis.core import QgsFeature, QgsGeometry
2968

0 commit comments

Comments
 (0)