7

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.

  • 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 5 '15 at 2: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 "<stdin>", line 1, in <module> File "C:\OSGeo4W64\apps\qgis\python\qgis_init_.py", line 36, in <module> from qgis.core import QgsFeature, QgsGeometry File "C:\OSGeo4W64\apps\qgis\python\qgis\core_init_.py", line 3, in <module> from qgis._core import * ImportError: DLL load failed: The specified module could not be found. – GeorgeC Dec 6 '15 at 3: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 8 '15 at 10:21
  • @Joseph I wish it did. I still get the same errors. – GeorgeC Dec 9 '15 at 6: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 9 '15 at 11:03
3
+50

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'])
  • thanks but I still get "ImportError: No module named qgis.core" – GeorgeC Dec 13 '15 at 2: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 2: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.