[Qgis-developer] python problem

侯伟 houw11 at lzu.edu.cn
Thu Jul 12 01:03:46 PDT 2012


I want to use qgis in pytnon environment. but got some problem. as 
http://www.qgis.org/pyqgis-cookbook/intro.html#python-applications says 
but i can set up qgis 1.8 environtment.

I find that grass has ability to use in python, and i succeeded.
use the same method i move to qgis, it seemly worked, but actually something is wrong.
can anyone have ideas for this.

below is my script to set up qgis envriontment in python...

import os,sys
qgisdir=r'C:\Program Files\Quantum GIS Lisboa'
def initqgisenv(qgisdir):
    os.environ['PYTHONPATH']=os.path.join(qgisdir,r'apps\qgis\python')
    os.environ['PYTHONPATH']=os.path.join(qgisdir,r'apps\Python27\Lib\site-packages')
    sys.path.insert(0,os.path.join(qgisdir,r'apps\qgis\python'))
    sys.path.insert(0,os.path.join(qgisdir,r'apps\Python27\Lib\site-packages'))
    os.environ['PATH']=os.path.join(qgisdir,r'apps\qgis\python;')+os.environ['PATH']
    sys.path.insert(1,os.path.join(qgisdir,r'apps\qgis\bin'))
    sys.path.insert(2,os.path.join(qgisdir,'bin'))
    os.environ['PATH']=os.path.join(qgisdir,'bin;')+os.environ['PATH']
    os.environ['PATH']=os.path.join(qgisdir,r'apps\qgis\bin;')+os.environ['PATH']
if __name__=='__main__':
    initqgisenv(qgisdir)
    from  qgis.core import *
    # supply path to where is your qgis installed
    QgsApplication.setPrefixPath(qgisdir, True)
    # load providers
    QgsApplication.initQgis()
    print 'set up qgis environment'
    #QgsApplication.exitQgis()
    print 'exit qgis properly'



More information about the Qgis-developer mailing list