I want to run PyQGIS in the command prompt. But every time I get an error because python can not be found by the command prompt. I installed QGIS Desktop 3.0 64 bit via the OSGeo4W installer.

 C:\Users\johwi>SET OSGEO4W_ROOT=C:\OSGeo4W64

 C:\Users\user>SET QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis

 C:\Users\user>SET PATH=%PATH%;%QGIS_PREFIX%\bin


The command "python" is written wrong or could not be found.

After adding the system variable "C:\OSGeo4W64\bin\python3.exe" I get this error:

Error processing line 1 of C:\Users\johwi\Anaconda3\lib\site-packages\matplotlib-2.1.2-py3.6-nspkg.pth:

Failed to import the site module
Traceback (most recent call last):
  File "C:\Users\johwi\Anaconda3\Lib\site.py", line 168, in addpackage
  File "<string>", line 1, in <module>
  File "C:\Users\johwi\Anaconda3\Lib\types.py", line 171, in <module>
    import functools as _functools
  File "C:\Users\johwi\Anaconda3\Lib\functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "C:\Users\johwi\Anaconda3\Lib\weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\johwi\Anaconda3\Lib\site.py", line 541, in <module>
  File "C:\Users\johwi\Anaconda3\Lib\site.py", line 523, in main
    known_paths = addsitepackages(known_paths)
  File "C:\Users\johwi\Anaconda3\Lib\site.py", line 320, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "C:\Users\johwi\Anaconda3\Lib\site.py", line 207, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "C:\Users\johwi\Anaconda3\Lib\site.py", line 178, in addpackage
    import traceback
  File "C:\Users\johwi\Anaconda3\Lib\traceback.py", line 5, in <module>
    import linecache
  File "C:\Users\johwi\Anaconda3\Lib\linecache.py", line 8, in <module>
    import functools
  File "C:\Users\johwi\Anaconda3\Lib\functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "C:\Users\johwi\Anaconda3\Lib\weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

Another try that didn't work: enter image description here

enter image description here

I added a user variable called "PYTHONPATH" and added three environment variables to the system variable "PATH" (last three in second picture). But I get the same error when I type in "python" in the command prompt.

It worked on my other computer where QGIS 2.18 and Python 2.7 is installed via OSGeo4w installer with this commands:

    REM Change OSGEO4W_ROOT to point to the base install folder
    REM Gdal Setup
    set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal\
    REM Python Setup
    set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%

But on my computer with QGIS 3.0 and Python 3.6 I tried everthing and nothing works.

  • What if you set the path through system->advanced system settings->advanced->environment variables and then manually add the python path to "Path" in system variables?
    – B.Quaink
    Commented Mar 15, 2018 at 13:28
  • Hmm the first error i see is it cannot import weakref, and that should be standard. Does python work in QGIS 3.0? And if so, can you try to import weakref? Do you have any other instances of python installed? Lastly, can you try /python.exe instead of /python3.exe?
    – B.Quaink
    Commented Mar 15, 2018 at 14:53
  • I have also installed Anaconda and ArcGIS on my computer. After changing the path to \python.exe I get the error "ImportError: No module named site". Commented Mar 15, 2018 at 15:19
  • What if you add the location at user variables instead of system variables?
    – B.Quaink
    Commented Mar 16, 2018 at 9:11
  • I get the same error when I add it to user variables. Commented Mar 26, 2018 at 18:50

1 Answer 1


What I suggest doing if you have multiple python interpreters installed, to add a new system variable with a description to eacht of the specific interpreters.

Add a new System Variable in Environment Variables. You can find these options via: system->advanced system settings->advanced->environment variables

Then add a new variable named PyQGIS with the system path C:\Program Files\QGIS 3.0\bin\python3.exe

Save everything and then start up the command prompt. Type "%PyQGIS%" to start up python that is in a specific directory, like the python interpreter in QGIS 3.0.

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.