Sunday, 21 October 2018

python - Problem with import qgis.core when writing a stand-alone PyQGIS script


I am trying to write a stand alone python script in windows 7 that uses qgis.core and various other qgis libraries.


I have found an old question on this subject here: Writing standalone Python scripts using PyQGIS? and was following its advice, by using the guide here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html#python-applications


The code suggested there is:


set PYTHONPATH=c:\qgispath\python

followed by:


set PATH=C:\qgispath;%PATH%


I found that IDLE did not like the "set at the beggining, so my code looks like this:


PYTHONPATH="C:\OSGeo4W64\apps\qgis\python"
PATH="C:\OSGeo4W64\apps\qgis\python\qgis\core;%PATH%"
import qgis.core

and I get:


Traceback (most recent call last):
File "", line 1, in
import qgis.core

ImportError: No module named qgis.core

I have tried every variant of the paths I can think of including removing the ;%PATH%" at the end (that looks a bit superfluous) but have not been able to find any more information. Does anyone have any ideas where I might be going wrong?


EDIT:


After seeing Luigi Pirelli's comment I have now tried:


>>> PYTHONPATH="C:/OSGEO4~1/apps/qgis/./python\qgis"
>>> import qgis.core

Traceback (most recent call last):
File "", line 1, in

import qgis.core
ImportError: No module named qgis.core
>>> PATH="C:\OSGeo4W64\apps\qgis\python\qgis\core;%PATH%"
>>> import qgis.core

Traceback (most recent call last):
File "", line 1, in
import qgis.core
ImportError: No module named qgis.core
>>> PATH="C:/OSGEO4~1/apps/qgis/./python\qgis\core;%PATH%"

>>> import qgis.core

Traceback (most recent call last):
File "", line 1, in
import qgis.core
ImportError: No module named qgis.core
>>>

Answer



If you have installed QGIS via OSGeo4W, I suggest you to follow this work flow (I've just tested it on Windows 7):





  1. Open the OSGeo4W Shell. Start->All programs->OSGeo4W->OSGeo4W Shell.




  2. Set environment variables. My preferred way is to execute this bat file (updated 2016.11.03: this bat file) from the OSGeo4W Shell. As you can see, the file sets environment variables in this way:


    set PYTHONPATH=%OSGEO4W_ROOT%\\apps\\qgis\\python
    set PATH=%OSGEO4W_ROOT%\\apps\\qgis\\bin;%PATH%

    Of course, you need to check by yourself that those paths exist in your OS and adjust them if necessary.


    So, download the .bat file and execute it by accessing it from the OSGeo4W Shell and pressing ENTER.



    enter image description here




  3. Import PyQGIS libraries by one of the following methods:


    a. Enter the Python interpreter. Run python in the OSGeo4W Shell and import the libraries manually.


    enter image description here


    b. Execute a Python script. Run python my_pyqgis_script.py in the OSGeo4W Shell. I'd start with a very simple one.


    enter image description here





The work flow was posted (in Spanish) several years ago in GeoTux and still works. As you can see, I do not include environment variables in the Python script, but set them in the OSGeo4W Shell.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...