Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 ?

share|improve this question
1  
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
add comment

1 Answer

up vote 5 down vote accepted

In the Python console run the following:

import sys
sys.path

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():

sys.path.append('~/Scripts/python')

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):

/Applications/QGIS.app/Contents/Resources/python/qgis/console.py

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.)

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/console.py –  Renaud Jun 14 '12 at 16:18
add comment

Your Answer

 
discard

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.