pythonubuntuenvironment-variablesqgispyqgis

Ubuntu22.04LTS QGIS add environmental variables


working with a book on QGIS in python, i came across the following example:

For computers that run a version of Linux, you can use the following:

export PYTHONPATH="/path/to/qgis/build/output/python/"
export LD_LIBRARY_PATH="/path/to/qgis/build/output/lib/"
export QGIS_PREFIX="/path/to/qgis/build/output/"

The author wants my python interpreter (PyCharm) to be able to find qgis and its python packages. Problem is I never added environmental variables on ubuntu, here's what I tried:

I cd'd to root and searched for python3 and qgis with which, and found them both in'/usr/bin/'. I then typed in the following command export /usr/bin/python3=/usr/bin/qgis/build/output/python only to get the following error bash: export: `/usr/bin/python3=/usr/bin/qgis/build/output/python': not a valid identifier

My understanding is that I'm doing something very wrong, could someone nudge me in the right direction? I need to know the following:

are the subjects of my export supposed to be directories or files? (which only returned files)

is QGIS really installed in '/usr/bin', same place as python? If so, why the need to add environmental variables?

Even though google says that QGIS shouldn't have tkinter, i'm able to import it into QGIS's python console and use it no problem. This works for all of my python packages (tested with flask, tkinter and bs4) , yet my pycharm doesn't see QGIS as a valid import. Is that indicative of anything?

Thanks for your patience


Solution

  • Using PyCharm

    If you're running this with PyCharm, you can specify the environment variables within the Run Configuration: enter image description here

    enter image description here

    In this dialog, you can fill in the data according to your needs, here's a quick run through the different fields:

    How to do this normally on Ubuntu?

    Normally, you would want environment variables to be independent of the method of running (in this case PyCharm), so that you could equally run the Python scripts directly from the terminal and everything would still work.

    There are several ways to go about this:

    1. Inline environment variables

    Whenever you want to run your script, you prepend the definition of environment variables to it: PYTHONPATH=/path/to/qgis/build/output LD_LIBRARY_PATH=/path/to/qgis/build/output/lib QGIS_PREFIX=/path/to/qgis/build/output python my_script.py

    2. Custom .rc file

    You can store the environment variable definitions into a file. Usually this file is hidden, so its name starts with a ., and the name usually ends with rc, for example .qgisrc. This file would look like this:

    export PYTHONPATH="/path/to/qgis/build/output/python/"
    export LD_LIBRARY_PATH="/path/to/qgis/build/output/lib/"
    export QGIS_PREFIX="/path/to/qgis/build/output/"
    

    Now, before running your scripts, you would first need to populate the environment with these variables by sourcing this .qgisrc file: source .qgisrc && python my_script.py

    3. More permanent and automatic environment population

    Finally, there already exist dedicated files where you can define more permanent environment variables which will be automatically loaded whenever you open your terminal: .bashrc and .profile. The process is the same as in the previous method, but you'd just add the export ... lines into the .bashrc or .profile files. In this case, the source command is not necessary - all environment variables defined in .bashrc or .profile files are loaded automatically whenever you start a new shell.

    Note that if you want to use this method and want it to work in PyCharm, you will need to start PyCharm from the terminal, and not using its launcher in the desktop GUI. This is because the environment variables are automatically loaded only into the shell, and any process started via the shell inherits the environment variables of the shell.

    What you did wrong

    The export /usr/bin/python3=/usr/bin/qgis/build/output/python is something you really shouldn't do. Environment variables are a mechanism to give a NAME to something, and have it be visible to every process. When the QGIS tutorial stated that you should write export PYTHONPATH=/path/.../, the left part of the command is meant literally - you should write export PYTHONPATH= exactly like this. The part after the equals sign depends on the paths on your particular computer, and you did good to find the path where QGIS is installed.

    I am not sure what you mean by "subjects of exports", but in this context the value (the right side) of all environment variables you listed should be a folder. For example, the PYTHONPATH environment variable is used internally by many tools to know where to look for the python interpreter, as well as where to look for any packages. You could have hundreds of Python packages, and they could theoretically all be installed in different places. Specifying PYTHONPATH to point to each of those places means Python will be able to find the packages.

    QGIS is almost certianly not installed in /usr/bin - that's just where the executable binary for QGIS is located. And if you run ls /usr/bin, you will see that just about everything is installed there. You are not trying to tell Python where the QGIS executable is, nor vice versa. You are trying to tell Python where to look for QGIS packages, so that they can be imported in your python scripts.

    Final disclaimer

    I am not entirely sure the PYTHONPATH variable should be exported as export PYTHONPATH="/path/to/qgis/build/output/python/". This will overwrite the current python path, so you will lose access to all libraries you normally have access to. If this happens, I advise you to try and write the export line like this: PYTHONPATH=$PYTHONPATH:/path/to/qgis/build/output/python/ (this takes the existing PYTHONPATH, and appends to it the QGIS related part).