I am trying to write a stand alone python script in windows 7 that uses qgis.core and various other qgis libraries.

I have found an old question on this subject here: Writing standalone Python scripts using PyQGIS? and was following its advice, by using the guide here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html#python-applications

The code suggested there is:

set PYTHONPATH=c:\qgispath\python

followed by:

set PATH=C:\qgispath;%PATH%

I found that IDLE did not like the "set at the beggining, so my code looks like this:

import qgis.core

and I get:

Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    import qgis.core
ImportError: No module named qgis.core

I have tried every variant of the paths I can think of including removing the ;%PATH%" at the end (that looks a bit superfluous) but have not been able to find any more information. Does anyone have any ideas where I might be going wrong?


After seeing Luigi Pirelli's comment I have now tried:

>>> PYTHONPATH="C:/OSGEO4~1/apps/qgis/./python\qgis"
>>> import qgis.core

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import qgis.core
ImportError: No module named qgis.core
>>> PATH="C:\OSGeo4W64\apps\qgis\python\qgis\core;%PATH%"
>>> import qgis.core

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import qgis.core
ImportError: No module named qgis.core
>>> PATH="C:/OSGEO4~1/apps/qgis/./python\qgis\core;%PATH%"
>>> import qgis.core

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    import qgis.core
ImportError: No module named qgis.core
  • Can you please modify the title of your question the way it would outline the exact issue you are facing? – SS_Rebelious Jan 12 '15 at 17:22
  • set PYTHONPATH as the value that you can read typing "qgis" in the QGIS Python Console... what is the value? In my case: <module 'qgis' from 'C:/OSGEO4~1/apps/qgis-dev/./python\qgis_init_.pyc'> => I've to add to PYTHONPATH the path: "C:/OSGEO4~1/apps/qgis-dev/./python\qgis" – Luigi Pirelli Jan 12 '15 at 20:37
  • Title amended, thanks, I will try this and report back in the main body of the question – user24956 Jan 13 '15 at 9:19
  • 1
    Still no luck, I have tried many permutations of PATH and PYTHONPATH to no avail. – user24956 Jan 13 '15 at 15:35

If you have installed QGIS via OSGeo4W, I suggest you to follow this work flow (I've just tested it on Windows 7):

  1. Open the OSGeo4W Shell. Start->All programs->OSGeo4W->OSGeo4W Shell.

  2. Set environment variables. My preferred way is to execute this bat file (updated 2016.11.03: this bat file) from the OSGeo4W Shell. As you can see, the file sets environment variables in this way:

    set PYTHONPATH=%OSGEO4W_ROOT%\\apps\\qgis\\python
    set PATH=%OSGEO4W_ROOT%\\apps\\qgis\\bin;%PATH%

    Of course, you need to check by yourself that those paths exist in your OS and adjust them if necessary.

    So, download the .bat file and execute it by accessing it from the OSGeo4W Shell and pressing ENTER.

    enter image description here

  3. Import PyQGIS libraries by one of the following methods:

    a. Enter the Python interpreter. Run python in the OSGeo4W Shell and import the libraries manually.

    enter image description here

    b. Execute a Python script. Run python my_pyqgis_script.py in the OSGeo4W Shell. I'd start with a very simple one.

    enter image description here

The work flow was posted (in Spanish) several years ago in GeoTux and still works. As you can see, I do not include environment variables in the Python script, but set them in the OSGeo4W Shell.

  • Hi! I have a doubt about that. Why var_entorno_osgeo4w.bat? I did all this lines but in this parte gives me an error. Also I read in these help foruns and I still get the import qgis.core error (run in osgeo4w shell). – Bárbara Duarte Jan 16 '15 at 16:47
  • Because that's the name of the file that I'm providing you with to set environment variables. So, you go to the folder where that .bat file is located (in the images you see it is located in C:\dllo ) and run the .bat file by writing the name of the file and pressing Enter. – Germán Carrillo Jan 16 '15 at 18:09
  • I tried to use the solution above - I tried to run the my_pygis_script.py and get an import error: No module named qgis.core. How can I fix this? – kflaw Mar 6 '15 at 3:32
  • I also tried to import qgis.core in pyscripter, and I get another import error: no module named sip – kflaw Mar 6 '15 at 3:51
  • 1
    I get 'No module named qgis.core' if I try the method suggested here. If I use the python.exe within qgis/bin as an interpreter I get the 'no module named site' error when I try to import qgis.core. I've wasted many hours trying to use pyqgis. – Alexander Jan 29 '16 at 14:19

I've just been wrestling with a similar problem, and noticed a batch file in the OSGeo4W\bin folder called python-qgis.bat. This is part of an OSGeo4W package based install of QGIS 2.18.15 x64 on Windows 10.

When executed, this batch file opens the Python interpreter. import qgis successfully runs from within.

This batch file contains the line (as noted by Germán Carrillo in his original answer):


just before the python interpreter is launched.

  • this takes off the pain of manually configuring the environment variables, thanks – Hicham Zouarhi Jan 31 '18 at 14:04
  • This seems to start working but than throws an error "SyntaxError: invalid syntax" when running C:\OSGEO4~1\apps\Python37\lib\site.py – Donny V. Sep 10 '19 at 21:56
  • Reason I got that error is because I wasn't running the python that QGIS is using. Works now! – Donny V. Sep 10 '19 at 22:01

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.