6

First of all, I'm using Python 2.7.3, QGIS 1.8 (Lisboa) on Windows 7 pro 64 bits. Now let's go to the actual question:

I have written some code (Python) that works well within the QGIS built in Python console, but now I need to run it from outside QGIS (it is too much to ask for the final user to open QGIS, then the python console and to copy and paste the code).

I have been trying to access the QGIS API, but it is missing from my PYTHONPATH. I've tried whatever is on the instructions, plus a bunch of paths like: C:\Program Files(x86)\QGIS\apps\qgis\python\qgis and C:\Program Files(x86)\QGIS\apps\qgis\python. My result so far is always the same:

import qgis.core
ImportError: No module named qgis.core

Any ideas?

  • 3
    I don't know if this is just a typo, but there should be a space between "Files" and "(x86)". Like so: C:\Program Files (x86) – blah238 Nov 6 '12 at 0:10
4

It is possible that you have a separate Python installation on your system in addition to Python being bundled with QGIS and PyQGIS is installed only for the bundled Python.

Check if you have two separate Python installations and if you do, try running the script by specifying the full path to the Python executable under the QGIS installation. If that works you'd need to make sure that the PYTHONPATH and/or PATH variables first find the Python under QGIS, because (as far as I know) PATH is only checked until it encounters the first instance of whatever is being looked for...

2

For new users reading this post, you can find a way of solving errors about importing qgis modules on Windows 7 at Problem with import qgis.core when writing a stand-alone PyQGIS script.

2

Add this to PATH variable (replace "C:\Program Files\QGIS Valmiera" with QGIS install location):

C:\Program Files\QGIS Valmiera\bin;C:\Program Files\QGIS Valmiera\apps\qgis\bin

and this to PYTHONPATH variable:

C:\Program Files\QGIS Valmiera\apps\qgis\python;
C:\Program Files\QGIS Valmiera\apps\qgis\python\plugins;
C:\Program Files\QGIS Valmiera\apps\Python27\lib\site-packages

1

This worked for me from A note on configuring your IDE on Windows and tere is available information for Eclipse and Pydev, I just like using Pyscripter

If you used the OSGeo4W Installer, you can find this under the bin folder of your OSGeo4W install. Look for something like C:\OSGeo4W\bin\qgis-unstable.bat.

  • For using Pyscripter IDE, here’s what you have to do:

  • Make a copy of qgis-unstable.bat and rename it pyscripter.bat.

  • Open it in an editor. And remove the last line, the one that starts QGIS.

  • Add a line that points to your Pyscripter executable and add the commandline argument that sets the version of Python to be used (2.7 in the case of QGIS >= 2.0)

  • Also add the argument that points to the folder where Pyscripter can find the Python dll used by QGIS, you can find this under the bin folder of your OSGeoW install

    @echo off

    SET OSGEO4W_ROOT=C:\OSGeo4W

    call "%OSGEO4W_ROOT%"\bin\o4w_env.bat

    call "%OSGEO4W_ROOT%"\bin\gdal16.bat

    @echo off

    path %PATH%;%GISBASE%\bin

Start C:\pyscripter\pyscripter.exe --python25 --pythondllpath=C:\OSGeo4W\bin Start C:\pyscripter\pyscripter.exe --python25 --pythondllpath=C:\OSGeo4W\bin Now when you double click this batch file it will start Pyscripter, with the correct path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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