# Running Python outside of QGIS - (ImportError: No module named qgis.core)

I'm trying to run simple scripts on QGIS from outside the QGIS environment - as here

How to run a simple python script for QGIS from outside (e.g. Sublime Text)?

My code:

import sys
sys.path.append("C:\\Program Files (x86)\\QGISBrighton\\apps\\Python27")
import qgis.core


My output:

ImportError: No module named qgis.core


My qgis_core.dll is at

C:\Program Files (x86)\QGISBrighton\apps\qgis\bin

QGIS install is here:

C:\Program Files (x86)\QGISBrighton

In Control Panel > System > Advanced

PYTHONPATH set to

C:\Program Files (x86)\QGISBrighton\apps\Python27

Path set to

C:\Python27; C:\Program Files (x86)\QGISBrighton\apps\Python27; C:\Program Files (x86)\QGISBrighton\apps\qgis\bin

I've tried multiple permutations of setting my Path, PYTHONPATH and appending to sys.path on the fly (as detailed in many answers here).

I'm hoping by providing the above information someone can give me explicit instructions on how to get past

ImportError: No module names qgis.core

• Try following some of the answers provided by @gcarrillo who has helped a great deal in solving similar issues. – Joseph Apr 9 '15 at 11:30
• Thanks @Joseph! @ben-mayo: Specifically, I suggest you to follow the steps given at gis.stackexchange.com/questions/129959/…. – Germán Carrillo Apr 10 '15 at 16:45
• I had this problem. The solution for me was to re-install QGIS following the 'Advanced Install' instructions here: spatialgalaxy.net/2014/10/09/… Specifically: From the Select packages screen, select the following for installation: Desktop -> qgis: QGIS Desktop Libs -> qt4-devel (needed for lrelease/translations) Libs -> setuptools (needed for installing pip) – Alexander Feb 19 '16 at 15:43

This solution worked for me in Windows 7x64 SP1 with Python 2.7.13-64bit and QGIS 2.14.9x64 standalone install (important: Python and QGIS architectures must match). This solution involves virtualenv (install instructions) which means you won't need to play with Python environment variables which can be tricky if you're doing other Python development with different versions of Python.

## Editing Path

Edit the PATH variable by going to Control Panel>System>Advanced system settings then click on Environment Variables in the below window.

Edit the PATH User Variable and add ;C:\Program Files\QGIS Essen\bin\;C:\Program Files\QGIS Essen\apps\qgis-ltr\bin, note the version Essen as well as the use of qgis-ltr\bin here. The latter was necessary in order for Python to be able to find the qgis_core.dll.

## Create a Virtual Environment

Create a new virtual environment in the command line: virtualenv qgisenv -p "C:\path\to\Python27.exe" specifying the Python 2.7 location. Note this will create a folder for your virtual environment, you should put any projects you develop in here.

Next add paths to the installed QGIS python libraries to the virtual environment's site-packages folder using .pth files. For example in QGIS 2.14.9 python packages can be found at the following locations (why?), so create a file qgisenv\Lib\site-packages\qgis.pth with:

C:\Program Files\QGIS Essen\apps\qgis-ltr\python
C:\Program Files\QGIS Essen\apps\Python27\
C:\Program Files\QGIS Essen\apps\Python27\Lib\
C:\Program Files\QGIS Essen\apps\Python27\Lib\site-packages
C:\Program Files\QGIS Essen\bin
C:\Program Files\QGIS Essen\include
C:\Program Files\QGIS Essen\apps\qgis-ltr\bin


this list can probably be shortened

## Activate Environment and Test

Back in the Command Prompt cd into the qgisenv directory and activate the virtual environment with Scripts\activate

Then open a python console and type

>>> from qgis.core import *


And you should be good to go!

## How to use an IDE?

If you use anaconda and Spyder, you could use conda to set up the environment and then call spyder after activating the environment. Other IDEs might have the option of setting a virtual environment from the settings.

• If you are using the virtualenv to set up qgis why then do you have to manually edit the system PATH variable. Shouldn't virtualenv take care of that for you? – user32882 Sep 7 '18 at 8:36
• @user32882 I'm not setting up QGIS in a virtualenv, I'm instead setting up a separate virtualenv to work on a PyQGIS project. – raphael Sep 7 '18 at 14:58