I've tried several previous answers to similar questions - no luck.

I run Win7. I installed QGIS 2.2.0 using the OSGeo4W 32-bit installer.

Problem 1) from qgis.core import * failed. Solution 1) After everything related to setting Path and PYTHONPATH variables failed, I had to copy C:\OSGeo4W\apps\qgis\python\qgis to C:\Python27\Lib\site-packages (and yes, including C:\OSGeo4W\apps\qgis\python\qgis in both Path and PYTHONPATH did NOT work).

Problem 2) The above approach got the qgis.core import started, but resulted in the following error message related to QgsFeature and QgsGeometry:

Traceback (most recent call last):
  File "D:\Research\inhaca\OBIA\model_script.py", line 1, in <module>
    from qgis.core import *
  File "C:\Python27\lib\site-packages\qgis\__init__.py", line 35, in <module>
    from qgis.core import QgsFeature, QgsGeometry
ImportError: DLL load failed: The specified module could not be found.

Now I'm lost. I tried copying all files from C:\OSGeo4W\apps\qgis\include and C:\OSGeo4W\apps\qgis\lib to the respective (include and Lib) folders in C:\Python27 - still didn't work. I'm fumbling in the dark here, any pointers?

  • Well for a start don't copy anything into your system Python. QGIS comes with it's own Python with everything that it needs.
    – Nathan W
    Mar 18, 2014 at 22:43
  • Are you trying to run this in a standalone script outside of QGIS?
    – Nathan W
    Mar 18, 2014 at 22:43
  • I copied what you listed and then run it on OSGeo4W shell then it worked Thanks a lot, Nahm!enter image description here
    – Nahm
    May 29, 2014 at 0:39

1 Answer 1


You need to create a batch file to bootstrap your Python file that setups the correct environment. This is the one I use for all of my standalone QGIS apps:

REM Change OSGEO4W_ROOT to point to the base install folder

REM Python Setup
set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%

python yourscript.py

The QGISNAME variable is only so I can swap between release qgis and dev QGIS for testing, remove if not needed.

  • Thanks @Nathan, I've followed your suggestion and it got me to the next error message... when I run my bat file I now get: ImportError: No module named site. So although site.py and site.pyc exist in C:\Python27\Lib (which is in my PYTHONPATH) apparently they are not found. (If I copy those two files to C:\OSGeo4W\apps\Python27\Lib the error changes to ImportError: No module named os).
    – Anders
    Mar 19, 2014 at 0:02
  • Let me stress this because it will cause you all end of problems. Don't copy or move anything from C:\Python27 into C:\OSGeo4W python. If it can't find site then it's not picking up the right Python version.
    – Nathan W
    Mar 19, 2014 at 0:34
  • Right, no copying! But I still have a problem, so how do I get it to find the right Python version? And - is there no way to use QGIS tools through Python without running it through a bat file? That seems odd to me, right?
    – Anders
    Mar 19, 2014 at 5:04
  • 1
    Ok @Nathan, finally got it to work using the bat file, so good so far. I can even import arcpy numpy as well, although various libraries installed through Pythonxy fail their imports (tested both scikits and gdal, both fail, while they import ok using my normal Python setup). Even disregarding that, it's still not very satisfactory since my code now ONLY runs through the bat file, which isn't exactly ideal for debugging! Any hints on how you create and debug code using the setup you suggested? Apologies for asking newbie questions, but figuring these things out from scratch is rather daunting.
    – Anders
    Mar 19, 2014 at 6:36

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.