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/utils.py", 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 ?

  • 1
    Try adding ~/Scripts/python to PATH that normally works for me. Check it with import os; os.path
    – Nathan W
    Commented Jun 14, 2012 at 14:26

1 Answer 1


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, 'console.py'. So, if you want to add your custom path to its sys.path in a more permanent fashion, edit the console.py file and add the sys.path.append('path/to/my/scripts') statement after the module imports section.

console.py is located here (for Mac, using KyngChaos.com QGIS package):


Of course, this is a hack that will be erased and have to be redone on a QGIS.app 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.)

  • 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/console.py
    – Renaud
    Commented Jun 14, 2012 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
    Commented Nov 5, 2014 at 18:29

Your Answer

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

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