3

The system env variable 'PATH' seems to be overwritten in Qgis, replacing it with their own values. Not sure if it was intended or not.

Anyway in version 2.2, I could solve it by go into settings and appending the PATH variable with the values copied from system properties => environment variables.

Apparently for version 2.4, the above solution does not work anymore and Qgis will always overwrite the PATH variable with their own values.

I needed the PATH variable as my project requires the execution of external programs from Qgis by making use of python subprocess module. Now in v2.4, external programs cannot be executed. I do not wish to define absolute path for all the external programs as the project will frequently be ported to different systems.

  • I'm the thread starter. I'm sorry that I can't seem to upvote your answer nor comment on it, probably because I'm new. Anyway, when you mentioned about the ../QGIS Valmiera/bin/qgis.bat, I went on to investigate further. Seems like the culprit lies in ../QGIS Valmiera/bin/o4w_env.bat that was being invoked earlier instead, on the following line: > set path=%OSGEO4W_ROOT%\bin;%WINDIR%\system32;%WINDIR%;%WINDIR%\WBem As you can see, the path variable has been set without appending it with the rest. With the following change: > set path=%OSGEO4W_ROOT%\bin;%WINDIR%\system32;%WINDIR%;%WINDIR%\WBem; – user34545 Jul 15 '14 at 8:53
  • The o4w_env.bat file remarks start with clean path, so I assume it was done intentionally. – AndreJ Jul 15 '14 at 15:08
  • You need to log in with your account to be able to comment and vote. Since you commented as @user34545, that tells me you were not logged in at that time. – underdark Jul 15 '14 at 16:19
  • Yes it's done intentionally to avoid dll conflicts with system dlls etc. Everything QGIS needs is inside the install folder so we use that as the base. – Nathan W Jul 15 '14 at 22:50
2

A little late, but beter than never :)

I ran into this problem recently. I was writing a QGIS Python plugin that had to call a matlab script. But the plugin had to run on different machines and I wouldn't always know where Matlab would be installed. The trick was to read the system Path variable from the registry instead of using os.

The which function below was adapted from https://stackoverflow.com/questions/377017/test-if-executable-exists-in-python. The getenv_system function was taken from http://win32com.goermezer.de/content/view/210/190/.

# This function is like the linux which command
def which(program):
    import os
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        #for path in os.environ["PATH"].split(os.pathsep):
        for path in getenv_system("PATH").split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file
    return None

# This function gets a system variable
# it was necessary to use this instead of os.environ["PATH"] because QGIS overwrites the path variable
# the win32 libraries appear not to be part of the standard python install, but they are included in the 
# python version that comes with QGIS
def getenv_system(varname, default=''):
    import os
    import win32api
    import win32con
    v = default
    try:
        rkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
        try:
            v = str(win32api.RegQueryValueEx(rkey, varname)[0])
            v = win32api.ExpandEnvironmentStrings(v)
        except:
            pass
    finally:
        win32api.RegCloseKey(rkey)
    return v

# find the path to the matlab executable. This is necessary because QGIS overwrites the path variable
matlab = which('matlab.exe') 
errCode = subprocess.call([matlab, "-r", myMatlabScript, "-nosplash", "-nodesktop", "-wait"])

The cool thing about this solution too is that you don't actually change the environment variables, so you get the path to your external application while still preventing clashes.

1

QGIS does not change the PATH variable permanently, it just adds its own folders to a default Windows path while it is running. This is done in ../QGIS Valmiera/bin/qgis.bat:

path %OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%

For QGIS 2.4, the line reads:

path %OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.4\lib;%PATH%

So no great changes.

With that extended path, QGIS.exe will be started. After exiting the programme, the command window session opened by qgis.bat gets closed, and everything is back to normal.

1

I am adding a solution for Unix like systems (tested on MacOS X): If the system $PATH variable is overwritten and that you want to add some paths:

  • Go under Preferences / System / Environment
  • "Append" to "Path" the path to the directory (ensure to start the path with ':')
  • Restart QGis

Append a directory to the PATH variable

0

I just wanted to point out that Bruno von Paris answer actually works for Windows with a little tweaking in QGIS 3.2.1 (and probably others), simply replace the : in his picture with ;

E.g.

Under Settings -> Options -> System -> Environment, tick 'use custom variables' and enter the following.

Apply = Append

Variable = PATH

Value = ;C:\PutPathHere

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.