8

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.

GeorgeC
  • 8,228
  • 7
  • 52
  • 136
  • What folders are in C:\OSGeo4W64\apps\qgis\python and C:\OSGeo4W64\apps\qgis\bin? Look for a processing folder. Mine was in python\plugins, and try append the path to the folder using sys.path.append. – Alex McVittie Dec 05 '15 at 02:18
  • @AlexMcVittie I used "sys.path.extend([r"C:\OSGeo4W64\apps\qgis\python",r"C:\OSGeo4W64\apps\qgis\bin", r"C:\OSGeo4W64\apps\qgis\python\plugins\processing"])" and then ran import qgis.core and now I get -- Traceback (most recent call last): File "", line 1, in File "C:\OSGeo4W64\apps\qgis\python\qgis__init__.py", line 36, in from qgis.core import QgsFeature, QgsGeometry File "C:\OSGeo4W64\apps\qgis\python\qgis\core__init__.py", line 3, in from qgis._core import * ImportError: DLL load failed: The specified module could not be found. – GeorgeC Dec 06 '15 at 03:33
  • Have you tried this solution? I know it has been mentioned in the solutions you mentioned but perhaps you can try it again, – Joseph Dec 08 '15 at 10:21
  • @Joseph I wish it did. I still get the same errors. – GeorgeC Dec 09 '15 at 06:39
  • 1
    Little offtopic, but maybe useful - Pycharm GUI offers easy setting of project python interpreter, where you just find it and it will show which packages are available (can be downloaded) for that python distribution... – dkocich Dec 09 '15 at 11:03
  • @DavidK , +1 I was literally typing a message along your comment, then I saw yours. – yanes Dec 13 '15 at 06:47
  • @yanes -I tried this but it still does not allow me to import pyQGIS or processing - I am having "memory" issues with a process in QGIS python and want to see how I can do this directly. See http://gis.stackexchange.com/questions/171114/memory-issue-qgis-python-processing-does-not-complete – GeorgeC Dec 13 '15 at 07:20
  • If using extend it complains about the DLL, then it can't find the qgis DLLs which lies in the qgis/bin directory, this should be included in the system PATH variable not the PYTHONPATH. You have to import os and then os.environ['PATH']='C:\\OSGeo4W64\\apps\\qgis\\bin;'+os.environ['PATH'] – caiohamamura Dec 14 '15 at 11:46

1 Answers1

5

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'])
caiohamamura
  • 1,031
  • 7
  • 19
  • thanks but I still get "ImportError: No module named qgis.core" – GeorgeC Dec 13 '15 at 02:40
  • I can see only two sources for this problem: the PYTHONPATH for C:\OSGeo4W64\apps\qgis\python is wrong (check drive letter and OSGeo4W64 or OSGeo4W) or you have a bad QGIS installation (try to change the curdir to where qgis\python is and import, if you keep having the same error it is probably a bad installation). – caiohamamura Dec 13 '15 at 14:48
  • If you have C:\OSGeo4W64\apps\qgis\python\qgis\__init__.py and C:\OSGeo4W64\apps\qgis\python\qgis\core\__init__.py there is no reason to get No module named qgis.core error. Assert you have these files and that PYTHONPATH is pointing to the right address of qgis\python. I could set up PyScripter with pyqgis successfully here. Don't forget to use Python 2.7, not Python 3. – caiohamamura Dec 14 '15 at 11:11
  • -yes the path are correct. I now get a sip import error (DLL load failed). I have sip in C:\Python27\Lib\sip-4.17 is this correct? I just extracted the zip to this directory and ran sipconfig.py – GeorgeC Dec 15 '15 at 02:23
  • Did you compile it? You have to compile it. Maybe you already have Visual C++ 2008 64-bit Tools. You have to use nmake and then nmake install to generate the "binaries", which is sip.pyd. Although I recommend you to use Python 27 from OSGeo4W, which already have an option to install sip with their installer, it is easier and less prone to error. – caiohamamura Dec 15 '15 at 11:18
  • Great...I can now import qgis.core but I get {{>>> layer = QgsVectorLayer("E:\Junk\pt000349_contour.tab", "contour", "mapinfo") Traceback (most recent call last): File "", line 1, in NameError: name 'QgsVectorLayer' is not defined}} What's happened? thank you. – GeorgeC Dec 15 '15 at 14:19
  • Have you imported QgsVectorLayer? from qgis.core import QgsVectorLayer or use QgsVectorLayer with prefixes qgis.core.QgsVectorLayer or if you want to import everything from qgis.core do from qgis.core import * – caiohamamura Dec 15 '15 at 14:28
  • https://gis.stackexchange.com/questions/296985/creating-standalone-python-script-using-pyqgis/296994?noredirect=1#comment477279_296994 hi please help needed in a similar question' – ps1 Sep 29 '18 at 09:32