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.