4

I would like to use a startup.py that is not stored in the default QGIS-user-folder like ~/.qgis2 or a folder used with the configpath option.

The startup.py I want to use should be located in a folder that is write protected for the users and should only be edited by sysadmins.

Boundless has implemented a interesting functionality called "QGIS Initialization scripts" ( https://connect.boundlessgeo.com/docs/desktop/latest/system_admins/init_scripts.html ).

As a workaround I check in the QGIS startup-file (e.g. qgis.bat) if "my" startup.py is located in the users .qgis2\python-folder. If there is no startup.py in the users folder I copy it into this folder. Otherwise I do a md5 check if the startup.py is identical with my startup.py and overwrite the users startup.py if it is not identical:

:start_qgis


set "file=%QGIS_UDIR%\python\startup.py"
set "referenzfile=%OSGEO4W_ROOT%\bwtacheck\startup.py"


if exist "%file%" goto start_bwta_check
copy /Y %referenzfile% %QGIS_UDIR%\python\startup.py
goto start_qgis2


:start_bwta_check
call %OSGEO4W_ROOT%\bwtacheck\md5.bat %file% file_md5
call %OSGEO4W_ROOT%\bwtacheck\md5.bat %referenzfile% referenz_md5

if "%file_md5%" equ "%referenz_md5%" (
echo MD5 identisch
) else (
echo MD5 nicht identisch
copy /Y %QGIS_UDIR%\python\startup.py %QGIS_UDIR%\python\startup_overwritten.py
copy /Y %referenzfile% %QGIS_UDIR%\python\startup.py
)

:start_qgis2
REM Run the central update commmandfile if the variable is set to a location and the file exist.
if *%QGIS_NETCMD%==* exit /b
if exist %QGIS_NETCMD% call %QGIS_NETCMD%
exit /b

Does the plain QGIS also support the use of a startup-script that is stored outside the typical userprofile-folder (the one used by qgis to store the plugins etc.)?

I tried to use a startup.py in the program-folder (like C:\OSGeo4W64\apps\qgis-ltr\python) but this did not work. Is this a bug or meant to be?

PS: It is important that the startup-script gets executed after QGIS has initialised.

2
  • 1
    Depending on what your script involves, you can set a PYQGIS_STARTUP environment variable. The issue with this is that it runs before QGIS has initialised, meaning that the script will not get full access to all the QGIS modules.
    – Joseph
    May 18, 2018 at 12:44
  • 1
    @Joseph: good to know the PYQGIS_STARTUP environmental variable but I need something that is executed after QGIS has initialised
    – Thomas B
    Jun 4, 2018 at 5:27

1 Answer 1

3
+50

You can automatically run a python script after QGIS is loaded with the --code option via command line.

E.g. to run the script /home/mario/mystartup.py you can launch QGIS like this from the command line:

qgis --code /home/mario/mystartup.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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