Skip to main content
improving code formatting and redaction
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

I have installer QGISinstalled QGIS via osgeo4wosgeo4w 32 bit yesterday and I want, to take pyqgis api in standalone script, but I have some problems.

first I have python 2.7.13 32bit and I have install pyqt4 installed from Gohlke  . second After, I createcreated a file (qgis.pth) and I put it in python27/Lib/site-packagespython27/Lib/site-packages to get the path for QGIS api.

pathsPaths are :

C:\OSGeo4W\apps\Python27
C:\OSGeo4W\apps\Python27\libs
C:\OSGeo4W\apps\Python27\Lib
C:\OSGeo4W\apps\Python27\DLLs
C:\OSGeo4W\apps\qgis\bin
C:\OSGeo4W\apps\qgis\grass\bin
C:\OSGeo4W\apps\qgis\grass\modules
C:\OSGeo4W\apps\qgis\grass\scripts
C:\OSGeo4W\apps\qgis\i18n
C:\OSGeo4W\apps\qgis\python
C:\OSGeo4W\apps\qgis\python\console
C:\OSGeo4W\apps\qgis\resources
C:\OSGeo4W\bin
C:\OSGeo4W\bin\gdalplugins
C:\OSGeo4W\bin\osgPlugins-3.1.7
C:\OSGeo4W\etc\ini
C:\OSGeo4W\etc\postinstall
C:\OSGeo4W\etc\preremove
C:\OSGeo4W\include\geos
C:\OSGeo4W\lib

(and if I add the paths from QGIS pyqt4 and sip I take againhad some errors) now the

The problems are:

firstFirst test :

import qgis.core

errorError :

    import qgis.core
  File "C:\OSGeo4W\apps\qgis\python\qgis\__init__.py", line 26, in <module>
    from qgis.PyQt import QtCore
  File "C:\OSGeo4W\apps\qgis\python\qgis\PyQt\QtCore.py", line 38, in <module>
    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

secondSecond test :

from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from PyQt4.QtSvg import *
#from PyQt4.QtCore import pyqtSignal as Signal
#from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error :

    sip.setapi(api, 1)
ValueError: API 'QDate' has already been set to version 2

and finaly y tryfinally, I tried this code :

import sip
API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
API_VERSION = 2
for name in API_NAMES:
    sip.setapi(name, API_VERSION)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSvg import *
from PyQt4.QtCore import pyqtSignal as Signal
from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error if version=1version is 1:

    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

error if version is 2  :

from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

ImportError: cannot import name QPyNullVariant

anyAny idea  ?if If I remove the import for QGIS then I don't take some error with imports for sip ot pyqt. It's only if I try to import PYQGISPyQGIS.

I have installer QGIS via osgeo4w 32 bit yesterday and I want to take pyqgis api in standalone script but I have some problems.

first I have python 2.7.13 32bit and I have install pyqt4 from Gohlke  . second I create a file (qgis.pth) and I put in python27/Lib/site-packages to get the path for QGIS api.

paths :

C:\OSGeo4W\apps\Python27
C:\OSGeo4W\apps\Python27\libs
C:\OSGeo4W\apps\Python27\Lib
C:\OSGeo4W\apps\Python27\DLLs
C:\OSGeo4W\apps\qgis\bin
C:\OSGeo4W\apps\qgis\grass\bin
C:\OSGeo4W\apps\qgis\grass\modules
C:\OSGeo4W\apps\qgis\grass\scripts
C:\OSGeo4W\apps\qgis\i18n
C:\OSGeo4W\apps\qgis\python
C:\OSGeo4W\apps\qgis\python\console
C:\OSGeo4W\apps\qgis\resources
C:\OSGeo4W\bin
C:\OSGeo4W\bin\gdalplugins
C:\OSGeo4W\bin\osgPlugins-3.1.7
C:\OSGeo4W\etc\ini
C:\OSGeo4W\etc\postinstall
C:\OSGeo4W\etc\preremove
C:\OSGeo4W\include\geos
C:\OSGeo4W\lib

(and if I add the paths from QGIS pyqt4 and sip I take again some errors) now the problems :

first test :

import qgis.core

error :

    import qgis.core
  File "C:\OSGeo4W\apps\qgis\python\qgis\__init__.py", line 26, in <module>
    from qgis.PyQt import QtCore
  File "C:\OSGeo4W\apps\qgis\python\qgis\PyQt\QtCore.py", line 38, in <module>
    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

second test :

from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from PyQt4.QtSvg import *
#from PyQt4.QtCore import pyqtSignal as Signal
#from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error :

    sip.setapi(api, 1)
ValueError: API 'QDate' has already been set to version 2

and finaly y try this code :

import sip
API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
API_VERSION = 2
for name in API_NAMES:
    sip.setapi(name, API_VERSION)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSvg import *
from PyQt4.QtCore import pyqtSignal as Signal
from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error if version=1

    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

if version is 2  :

from PyQt4.QtCore import QPyNullVariant

ImportError: cannot import name QPyNullVariant

any idea  ?if remove the import for QGIS then I don't take some error with imports for sip ot pyqt only if I try to import PYQGIS

I have installed QGIS via osgeo4w 32 bit yesterday, to take pyqgis api in standalone script, but I have some problems.

I have python 2.7.13 32bit and pyqt4 installed from Gohlke. After, I created a file (qgis.pth) and I put it in python27/Lib/site-packages to get the path for QGIS api.

Paths are :

C:\OSGeo4W\apps\Python27
C:\OSGeo4W\apps\Python27\libs
C:\OSGeo4W\apps\Python27\Lib
C:\OSGeo4W\apps\Python27\DLLs
C:\OSGeo4W\apps\qgis\bin
C:\OSGeo4W\apps\qgis\grass\bin
C:\OSGeo4W\apps\qgis\grass\modules
C:\OSGeo4W\apps\qgis\grass\scripts
C:\OSGeo4W\apps\qgis\i18n
C:\OSGeo4W\apps\qgis\python
C:\OSGeo4W\apps\qgis\python\console
C:\OSGeo4W\apps\qgis\resources
C:\OSGeo4W\bin
C:\OSGeo4W\bin\gdalplugins
C:\OSGeo4W\bin\osgPlugins-3.1.7
C:\OSGeo4W\etc\ini
C:\OSGeo4W\etc\postinstall
C:\OSGeo4W\etc\preremove
C:\OSGeo4W\include\geos
C:\OSGeo4W\lib

(and if I add the paths from QGIS pyqt4 and sip I had some errors)

The problems are:

First test :

import qgis.core

Error :

import qgis.core
  File "C:\OSGeo4W\apps\qgis\python\qgis\__init__.py", line 26, in <module>
    from qgis.PyQt import QtCore
  File "C:\OSGeo4W\apps\qgis\python\qgis\PyQt\QtCore.py", line 38, in <module>
    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

Second test :

from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from PyQt4.QtSvg import *
#from PyQt4.QtCore import pyqtSignal as Signal
#from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error :

sip.setapi(api, 1)
ValueError: API 'QDate' has already been set to version 2

and finally, I tried this code :

import sip
API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
API_VERSION = 2
for name in API_NAMES:
    sip.setapi(name, API_VERSION)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSvg import *
from PyQt4.QtCore import pyqtSignal as Signal
from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error if version is 1:

from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

error if version is 2:

from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

Any idea? If I remove the import for QGIS then I don't take some error with imports for sip ot pyqt. It's only if I try to import PyQGIS.

Source Link
jessie jes
  • 1.1k
  • 8
  • 21

error sip.setapi(api, 1) in pyQGIS

I have installer QGIS via osgeo4w 32 bit yesterday and I want to take pyqgis api in standalone script but I have some problems.

first I have python 2.7.13 32bit and I have install pyqt4 from Gohlke . second I create a file (qgis.pth) and I put in python27/Lib/site-packages to get the path for QGIS api.

paths :

C:\OSGeo4W\apps\Python27
C:\OSGeo4W\apps\Python27\libs
C:\OSGeo4W\apps\Python27\Lib
C:\OSGeo4W\apps\Python27\DLLs
C:\OSGeo4W\apps\qgis\bin
C:\OSGeo4W\apps\qgis\grass\bin
C:\OSGeo4W\apps\qgis\grass\modules
C:\OSGeo4W\apps\qgis\grass\scripts
C:\OSGeo4W\apps\qgis\i18n
C:\OSGeo4W\apps\qgis\python
C:\OSGeo4W\apps\qgis\python\console
C:\OSGeo4W\apps\qgis\resources
C:\OSGeo4W\bin
C:\OSGeo4W\bin\gdalplugins
C:\OSGeo4W\bin\osgPlugins-3.1.7
C:\OSGeo4W\etc\ini
C:\OSGeo4W\etc\postinstall
C:\OSGeo4W\etc\preremove
C:\OSGeo4W\include\geos
C:\OSGeo4W\lib

(and if I add the paths from QGIS pyqt4 and sip I take again some errors) now the problems :

first test :

import qgis.core

error :

    import qgis.core
  File "C:\OSGeo4W\apps\qgis\python\qgis\__init__.py", line 26, in <module>
    from qgis.PyQt import QtCore
  File "C:\OSGeo4W\apps\qgis\python\qgis\PyQt\QtCore.py", line 38, in <module>
    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

second test :

from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from PyQt4.QtSvg import *
#from PyQt4.QtCore import pyqtSignal as Signal
#from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error :

    sip.setapi(api, 1)
ValueError: API 'QDate' has already been set to version 2

and finaly y try this code :

import sip
API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
API_VERSION = 2
for name in API_NAMES:
    sip.setapi(name, API_VERSION)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSvg import *
from PyQt4.QtCore import pyqtSignal as Signal
from PyQt4.QtCore import pyqtSlot as Slot
import qgis.core

error if version=1

    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

if version is 2 :

from PyQt4.QtCore import QPyNullVariant

ImportError: cannot import name QPyNullVariant

any idea ?if remove the import for QGIS then I don't take some error with imports for sip ot pyqt only if I try to import PYQGIS