Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i am working with the qgis python console. I usually store all my python scripts into ~/Scripts/python I do have set the PYTHONPATH to ~/Scripts/python in my .bashrc However, I can not manage to import my sripts from the console inside QGIS.

Here is my error when I try to import a script called Create_Figures_2D

import Create_Figures_2D
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/qgis/", line 309, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: No module named Create_Figures_2D

How do I set up the PYTHONPATH for pygis ?

share|improve this question
Try adding ~/Scripts/python to PATH that normally works for me. Check it with import os; os.path – Nathan W Jun 14 '12 at 14:26
up vote 7 down vote accepted

In the Python console run the following:

import sys

See if your PYTHONPATH entry is listed. I'm guessing not, since that environment variable is probably not available to QGIS's running Python.

To add your scripts folder to console's sys.path do a regular append():


Then you should be able to import your modules from there for that console session. I recommend expanding the ~ to your user's full home path (not sure if Python does ~ expansion).

EDIT: ~ expansion doesn't seem to be supported by sys.path (on Mac, at least).

QGIS's Python console is created by a Python module, ''. So, if you want to add your custom path to its sys.path in a more permanent fashion, edit the file and add the sys.path.append('path/to/my/scripts') statement after the module imports section. is located here (for Mac, using QGIS package):


Of course, this is a hack that will be erased and have to be redone on a update.

Any adjustment to the running QGIS Python interpreter's sys.path is available to other plugins as well.

A safer alternative might be to make a simple, development PyQGIS plugin that loads and merely does the sys.path.append(), whenever you want it. It could also remove that path from sys.path on plugin unload(). I have not tested whether the sys.path adjustment of such a plugin would be available in an already-loaded console (I'm guessing, it would be.)

share|improve this answer
Your Hack is working fine for me so far. Thank you very much.On ubuntu 12.04, the file is located at /usr/share/pyshared/qgis/ – Renaud Jun 14 '12 at 16:18
Excellent! By the way, I needed to restart my QGIS on Windows 7 to make the Python console aware of the appended path. – Dirk Nov 5 '14 at 18:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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