Python – How to Import qgis.core to Python App

pythonqgis

I'm new to Python. I want to create GIS app with Python using QGIS library on Window 7 64 bit.

The way I do:

install QGIS 1.7.0

  • I set PYTHONPATH=D:\Program Files (x86)\Quantum GIS Wroclaw\apps\qgis\python
  • I run python from cmd in D:\Program Files (x86)\Quantum GIS Wroclaw\bin
  • I type import qgis.core, a pop up window appear with text

The program can't start because qgis_core.dll is missing from your
computer. Try reinstalling the program to fix this problem

and text in cmd is

ImportError: DLL load failed: The specified module could not be found.

Can anyone help me?

Best Answer

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).