# How to import qgis.core to Python app?

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?

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

Reinstall QGIS with OSGeo4W installer, it has everything you need to develop already set up. http://trac.osgeo.org/osgeo4w/

If you want to create a standalone application, you should use the copy qgis.bat in %OSGEO4W_ROOT%\bin as a template and replace the call to qgis with a call to python (or pythonw with your application script). You'll then have a python running in an environment similar to QGIS.

In other words: copy qgis.bat in D:\Program Files (x86)\Quantum GIS Wroclaw\bin to myqgisapp.bat and replace the last line in the copy

start "Quantum GIS" /B "%OSGEO4W_ROOT%"\apps\qgis\bin\qgis.exe %*


with

start "My Quantum GIS App" /B "%OSGEO4W_ROOT%"\apps\qgis\bin\pythonw.exe L:\path\to\your\python\app.py


Note: pythonw.exe will not open a separate text window, but print won't work (if you need that, use python.exe).

• Thanks. But can you show me some step ? I'm so confused Apr 5, 2012 at 17:23
• I dont have 15 reputation to vote your answer. Thank you anyway. :D Apr 6, 2012 at 0:45

Apart from Vincent's answer (which I found correct), it is important to note that the path to qgis_core.dll in the PATH environment variable should come first in the list.

Prior to finding this out, I have been stuck with the described problem in a QGIS 2.0.1 OSGeo4W installation.

If you use OSGeo4W package (download here), installed or portable (my favorite):

1. from installation directory (for example f:\OSGeo4W) start OSGeo4W.bat,
2. In the shell type "py3_env" (that is batch file defining environment for python3),
3. Type "python" to start python interpreter,
4. Type >>>import qgis.core to work with qgis completely from python

ERROR rise: "ModuleNotFoundError: No module named 'qgis'"

To correct this open py3_env.bat (f:\OSGeo4W\bin) and ad to python environmental variable %OSGEO4W_ROOT%\apps\qgis\python.

py3_env.bat should look like this:

SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
SET PYTHONPATH=%PYTHONHOME%;%PYTHONHOME%\Scripts;%OSGEO4W_ROOT%\apps\qgis\python
PATH %PYTHONPATH%;%PATH%


Thats it. Just type in python console:

>>>import qgis.core


and code-on!