Monday, 8 May 2017

pyqgis - Getting QGIS python modules to work from PyScripter in Windows 10?


Been trying lots of "solutions" from net research but nothing seems to work.


I have the current versions of QGIS from OSGEO and PyScripter 2.5


Based on one of the solutions I tried...


import sys
sys.path.extend([r"C:\OSGeo4W64\apps\qgis\python",r"C:\OSGeo4W64\apps\qgis\bin"])
sys.path.reverse()
import qgis.core


gives me



Message File Name Line Position Traceback
4
C:\OSGeo4W64\apps\qgis\python\qgis__init__.py 26 ImportError: No module named sip



import processing

gives me




ImportError: No module named processing



and path is


PATH=C:\ProgramData\Oracle\Java\javapath;C:\Python27\;C:\Python27\Scripts;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Skype\Phone\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\; C:\OSGeo4W64\bin 

pythonpath includes


C:\OSGeo4W64\apps\qgis\bin
C:\OSGeo4W64\apps\qgis\python

Tried "solutions"




  1. https://mapoholic.wordpress.com/2012/06/28/configure-pyscripter-qgis/

  2. How can I Setup Python to recognize QGIS (and links from it)

  3. https://stackoverflow.com/questions/15297391/problems-while-importing-pyqgis-modules-in-python

  4. Import error for qgis.core when running OSGeo4w shell script


and


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


after running the .bat in above I get "ImportError: no module name qgis.core" in python within osgeo shell as well.



Answer




Your PYTHONHOME should be:


C:\OSGeo4W64\apps\Python27

Your PATH should include (in the beggining):


C:\OSGeo4W64\bin; C:\OSGeo4W64\apps\qgis\bin 

PYTHONPATH should have:


C:\OSGeo4W64\apps\qgis\python
C:\OSGeo4W64\apps\qgis\python\plugins
C:\OSGeo4W64\bin\python27.zip

C:\OSGeo4W64\apps\Python27\DLLs
C:\OSGeo4W64\apps\Python27
C:\OSGeo4W64\apps\Python27\lib
C:\OSGeo4W64\apps\Python27\lib\site-packages

In code you have to:


import sys
from qgis.core import QgsApplication

QgsApplication.setPrefixPath('C:\OSGeo4W64\apps\qgis', True)

qgs = QgsApplication(sys.argv, False)
qgs.initQgis()

If you don't have sip in your Python installation you should install it.


EDIT


Provided you are running the correct python version for pyqgis you can setup everything in python code:


import sys, os

os.environ['PATH'] = 'C:\\OSGeo4W64\\apps\\qgis\\bin;C:\\OSGeo4W64\\bin;' + os.environ['PATH']
sys.path.extend(['C:\\OSGeo4W64\\apps\\qgis\\python', 'C:\\OSGeo4W64\\apps\\qgis\\python\\plugins', 'C:\\OSGeo4W64\\bin\\python27.zip', 'C:\\OSGeo4W64\\apps\\Python27\\DLLs', 'C:\\OSGeo4W64\\apps\\Python27', 'C:\\OSGeo4W64\\apps\\Python27\\lib', 'C:\\OSGeo4W64\\apps\\Python27\\lib\\site-packages'])

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