I am trying to write a standalone Python script in windows 7 that uses qgis.core and various other qgis libraries.

I have found an old question on this subject at Writing standalone Python scripts using PyQGIS? and was following its advice, by using the guide at 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 idea 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? Commented Jan 12, 2015 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" Commented Jan 12, 2015 at 20:37
  • Title amended, thanks, I will try this and report back in the main body of the question
    – user24956
    Commented Jan 13, 2015 at 9:19
  • 1
    Still no luck, I have tried many permutations of PATH and PYTHONPATH to no avail.
    – user24956
    Commented Jan 13, 2015 at 15:35

3 Answers 3


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). Commented Jan 16, 2015 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. Commented Jan 16, 2015 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
    Commented Mar 6, 2015 at 3:32
  • I also tried to import qgis.core in pyscripter, and I get another import error: no module named sip
    – kflaw
    Commented Mar 6, 2015 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
    Commented Jan 29, 2016 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 Commented Jan 31, 2018 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.
    Commented Sep 10, 2019 at 21:56
  • Reason I got that error is because I wasn't running the python that QGIS is using. Works now!
    – Donny V.
    Commented Sep 10, 2019 at 22:01

If you want to have a clear project to perform PyQGIS development, Anaconda is the best way to construct it. Once anaconda is installed you can create a project for PyQGIS by following those steps : 1. Activate the base environment if it's not ( '(base)' prefix at the left)

conda activate

2. Create a conda environment for PyQGIS development :

conda create -n pyqgis-dev

3. Activate the new environment

conda activate pyqgis-dev

4. Install QGIS through anaconda in the new environment, for example the 3.4 version :

conda install -c conda-forge qgis=3.4
  1. Type 'y' and press enter :

    Collecting package metadata (current_repodata.json): done
    Solving environment: failed with initial frozen solve. Retrying with flexible solve.
    Collecting package metadata (repodata.json): done
    Solving environment: done
    ==> WARNING: A newer version of conda exists. <==
      current version: 4.7.12
      latest version: 4.8.2
    Please update conda by running
    $ conda update -n base -c defaults conda
    ## Package Plan ##
    environment location: C:\Users\e.conte\AppData\Local\Continuum\anaconda3\envs\pyqgis-dev
    added / updated specs:
      - qgis=3.4
    The following packages will be downloaded:
       package                    |            build
    pycparser-2.19             |             py_2          88 KB  conda-forge
                                           Total:          88 KB
    The following NEW packages will be INSTALLED:
      boost-cpp          conda-forge/win-64::boost-cpp-1.70.0-h6a4c333_2
      bzip2              conda-forge/win-64::bzip2-1.0.8-hfa6e2cd_2
      ca-certificates    conda-forge/win-64::ca-certificates-2019.11.28-hecc5488_0
      certifi            conda-forge/win-64::certifi-2019.11.28-py37_0
      cffi               conda-forge/win-64::cffi-1.13.2-py37hb32ad35_0
      cfitsio            conda-forge/win-64::cfitsio-3.470-hfa6e2cd_2
      chardet            conda-forge/win-64::chardet-3.0.4-py37_1003
      coverage           conda-forge/win-64::coverage-5.0.3-py37hfa6e2cd_0
      cryptography       conda-forge/win-64::cryptography-2.8-py37hb32ad35_1
      curl               conda-forge/win-64::curl-7.68.0-h4496350_0
      expat              conda-forge/win-64::expat-2.2.9-he025d50_2
      freetype           conda-forge/win-64::freetype-2.10.0-h563cfd7_1
      freexl             conda-forge/win-64::freexl-1.0.5-hd288d7e_1002
      future             conda-forge/win-64::future-0.18.2-py37_0
      gdal               conda-forge/win-64::gdal-2.4.2-py37he6b6c38_4
      geos               conda-forge/win-64::geos-3.7.2-he025d50_2
      geotiff            conda-forge/win-64::geotiff-1.5.1-h8f84788_2
      gettext            conda-forge/win-64::gettext-
      glib               conda-forge/win-64::glib-2.58.3-py37hc0c2ac7_1002
      gsl                conda-forge/win-64::gsl-2.5-h631dd0c_1
      hdf4               conda-forge/win-64::hdf4-4.2.13-hf8e6fe8_1003
      hdf5               conda-forge/win-64::hdf5-1.10.5-nompi_ha405e13_1104
      httplib2           conda-forge/win-64::httplib2-0.17.0-py37_0
      icu                conda-forge/win-64::icu-64.2-he025d50_1
      idna               conda-forge/win-64::idna-2.8-py37_1000
      intel-openmp       pkgs/main/win-64::intel-openmp-2019.4-245
      jinja2             conda-forge/noarch::jinja2-2.11.1-py_0
      jpeg               conda-forge/win-64::jpeg-9c-hfa6e2cd_1001
      kealib             conda-forge/win-64::kealib-1.4.10-hf7dc31f_1005
      krb5               conda-forge/win-64::krb5-1.16.4-hdd46e55_0
      libblas            conda-forge/win-64::libblas-3.8.0-14_mkl
      libcblas           conda-forge/win-64::libcblas-3.8.0-14_mkl
      libcurl            conda-forge/win-64::libcurl-7.68.0-h4496350_0
      libffi             conda-forge/win-64::libffi-3.2.1-h6538335_1006
      libgdal            conda-forge/win-64::libgdal-2.4.2-h2bdb96c_4
      libiconv           conda-forge/win-64::libiconv-1.15-hfa6e2cd_1005
      libkml             conda-forge/win-64::libkml-1.3.0-h4ece8bf_1010
      liblapack          conda-forge/win-64::liblapack-3.8.0-14_mkl
      libnetcdf          conda-forge/win-64::libnetcdf-4.6.2-h376842a_1003
      libpng             conda-forge/win-64::libpng-1.6.37-h7602738_0
      libpq              conda-forge/win-64::libpq-11.5-hb0bdaea_2
      libspatialindex    conda-forge/win-64::libspatialindex-1.9.3-he025d50_3
      libspatialite      conda-forge/win-64::libspatialite-4.3.0a-h832c139_1029
      libssh2            conda-forge/win-64::libssh2-1.8.2-h642c060_2
      libtiff            conda-forge/win-64::libtiff-4.1.0-h21b02b4_3
      libxml2            conda-forge/win-64::libxml2-2.9.10-h9ce36c8_0
      libzip             conda-forge/win-64::libzip-1.5.1-h8f0db0c_1005
      lz4-c              conda-forge/win-64::lz4-c-1.8.3-he025d50_1001
      m2w64-expat        pkgs/msys2/win-64::m2w64-expat-2.1.1-2
      m2w64-gcc-libgfor~ pkgs/msys2/win-64::m2w64-gcc-libgfortran-5.3.0-6
      m2w64-gcc-libs     pkgs/msys2/win-64::m2w64-gcc-libs-5.3.0-7
      m2w64-gcc-libs-co~ pkgs/msys2/win-64::m2w64-gcc-libs-core-5.3.0-7
      m2w64-gettext      pkgs/msys2/win-64::m2w64-gettext-0.19.7-2
      m2w64-gmp          pkgs/msys2/win-64::m2w64-gmp-6.1.0-2
      m2w64-libiconv     pkgs/msys2/win-64::m2w64-libiconv-1.14-6
      m2w64-libwinpthre~ pkgs/msys2/win-64::m2w64-libwinpthread-git-
      m2w64-xz           pkgs/msys2/win-64::m2w64-xz-5.2.2-2
      markupsafe         conda-forge/win-64::markupsafe-1.1.1-py37hfa6e2cd_0
      mkl                pkgs/main/win-64::mkl-2019.4-245
      mock               conda-forge/win-64::mock-3.0.5-py37_0
      msys2-conda-epoch  pkgs/msys2/win-64::msys2-conda-epoch-20160418-1
      nose2              conda-forge/noarch::nose2-0.9.2-py_0
      numpy              conda-forge/win-64::numpy-1.18.1-py37hc71023c_0
      openjpeg           conda-forge/win-64::openjpeg-2.3.1-h57dd2e7_3
      openssl            conda-forge/win-64::openssl-1.1.1d-hfa6e2cd_0
      owslib             conda-forge/noarch::owslib-0.19.1-py_0
      pcre               conda-forge/win-64::pcre-8.44-h6538335_0
      pip                conda-forge/noarch::pip-20.0.2-py_2
      plotly             conda-forge/noarch::plotly-4.5.0-py_0
      poppler            conda-forge/win-64::poppler-0.67.0-h1707e21_8
      poppler-data       conda-forge/noarch::poppler-data-0.4.9-1
      postgresql         conda-forge/win-64::postgresql-11.5-h06f7779_2
      proj4              conda-forge/win-64::proj4-6.1.0-hc2d0af5_2
      psycopg2           conda-forge/win-64::psycopg2-2.8.4-py37hb32ad35_0
      pycparser          conda-forge/noarch::pycparser-2.19-py_2
      pygments           conda-forge/noarch::pygments-2.5.2-py_0
      pyopenssl          conda-forge/win-64::pyopenssl-19.1.0-py37_0
      pyproj             conda-forge/win-64::pyproj-2.2.1-py37h3f740ce_0
      pyqt               conda-forge/win-64::pyqt-5.9.2-py37h6538335_4
      pysocks            conda-forge/win-64::pysocks-1.7.1-py37_0
      python             conda-forge/win-64::python-3.7.6-h5b45d93_2
      python-dateutil    conda-forge/noarch::python-dateutil-2.8.1-py_0
      pytz               conda-forge/noarch::pytz-2019.3-py_0
      pyyaml             conda-forge/win-64::pyyaml-5.3-py37hfa6e2cd_0
      qca                conda-forge/win-64::qca-2.2.1-h1a227db_2
      qgis               conda-forge/win-64::qgis-3.4.14-py37h64d9c6e_0
      qjson              conda-forge/win-64::qjson-0.9.0-h1a227db_1005
      qscintilla2        conda-forge/win-64::qscintilla2-2.11.2-py37h6acbd6c_0
      qt                 conda-forge/win-64::qt-5.9.7-h506e8af_3
      qtkeychain         conda-forge/win-64::qtkeychain-0.9.1-h1a227db_1001
      qtlocation         conda-forge/win-64::qtlocation-5.9.7-he025d50_1
      qwt                conda-forge/win-64::qwt-6.1.4-h1a227db_1003
      qwtpolar           conda-forge/win-64::qwtpolar-1.1.1-h1a227db_6
      requests           conda-forge/win-64::requests-2.22.0-py37_1
      retrying           conda-forge/noarch::retrying-1.3.3-py_2
      setuptools         conda-forge/win-64::setuptools-45.2.0-py37_0
      sip                conda-forge/win-64::sip-4.19.8-py37h6538335_1000
      six                conda-forge/win-64::six-1.14.0-py37_0
      sqlite             conda-forge/win-64::sqlite-3.30.1-hfa6e2cd_0
      tk                 conda-forge/win-64::tk-8.6.10-hfa6e2cd_0
      urllib3            conda-forge/win-64::urllib3-1.25.7-py37_0
      vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
      vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
      wheel              conda-forge/noarch::wheel-0.34.2-py_1
      win_inet_pton      conda-forge/win-64::win_inet_pton-1.1.0-py37_0
      wincertstore       conda-forge/win-64::wincertstore-0.2-py37_1003
      xerces-c           conda-forge/win-64::xerces-c-3.2.2-h6538335_1004
      xz                 conda-forge/win-64::xz-5.2.4-h2fa13f4_1001
      yaml               conda-forge/win-64::yaml-0.2.2-hfa6e2cd_1
      zlib               conda-forge/win-64::zlib-1.2.11-h2fa13f4_1006
      zstd               conda-forge/win-64::zstd-1.4.4-hd8a0e53_1
    Proceed ([y]/n)? y

    This step basically installed the matching python version with the QGIS version you asked, QGIS in your python environment, all the depedencies that it needs (gdal, geos, psycopg2, sqlite, proj, numpy...), setted up the PYTHONPATH to dll and pyd files. So in just a few seconds you have all you need to develop in PyQGIS.

In the top of all the scripts that you'll write, I recommend you to put this before anything (the processing part is just if you want to use QGIS algorithm such as clip or reprojectlayer :

import processing
from qgis.core import QgsApplication
from processing.core.Processing import Processing
from qgis.analysis import QgsNativeAlgorithms

qgs = QgsApplication([], False)
  • I second this, it worked for me. Commented Nov 17, 2021 at 20:44

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.