You have to verify two things :
PYTHONPATH
The PYTHONPATH is set for the cmd you launch. This means that you have to set it globally on your system
Go to Control Panel -> System-> Advanced.
Click on the 'Environment Variables' button.
In the 'System Variables' panel that appears, click New and enter PYTHONPATH in the 'Variable Name' field. In the 'Variable Value' field enter the path to QGIS.
Or you can set it directly on the cmd command line just before launching the python interpreter with :
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
PATH
The DLL which will be used by the qgis python module must be in your path.
Just as you set the PYTHONPATH above, set the PATH environment variable so that it contains the directory where qgis_core.dll is located.
The directory to add is probably
D:\Program Files (x86)\Quantum GIS Wroclaw\apps\qgis
But check that qgis_core.dll is really there.
OSGEO4W
As was suggested in another answer, OSGEO4W install and sets everything up for you. It will install another Python though, which can complicate things when you want to install third party Python modules not available in OSGEO4W.
You would as well get the latest QGIS version too, which is 1.7.4 (1.8 coming soon).
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.
Best Answer
This is the correct path setup:
qgis.core
andqgis.gui
are located in theqgis\python\qgis
folder but you only setPYTHONPATH
to theqgis\python
folder becauseqgis
is a package.