4

I installed QGIS through OSGeo4W, and I am trying to write a standalone PyQGIS script, however, I am unable to import the qgis, qgis.core, processing ect… modules, as shown in the picture, I get the ‘Import Error: No Module named qgis...

enter image description here

I sought help from the many examples that exist that require you to change your command line path to your python libraries, change your python path to your c:\OSGeo4W\apps\qgis\python ect…

Here is a list of the answers I tried, but, it seems myself and many others are still unable to fix this issue, as I could not get it to acknowledge the existence of those libraries:

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

Cannot run standalone QGIS script

Running Custom Applications:

0

I am running a batch file first.

call "C:\OSGeo4W64\bin\o4w_env.bat"
set PATH=%PATH%;C:\OSGeo4W64\apps\qgis\bin
  • Fantastic you found your own solution, am sure this will help others – user32496 Jun 30 '16 at 7:56
  • I tried to use this method as it is much easier than mine, but, I still get Import error: No Module named QGIS @okorkut – user32496 Jun 30 '16 at 10:19
  • 1
    Have you verified the path of the "o4w_env.bat" in your computer. I set this according to my path. I assumed that since you installed through QSGeo4W file paths should be similar. May be your file installed in a different directory. – okorkut Jun 30 '16 at 12:29
4

Here is how I solved the issue, it’s a bit of work, but you can now import the modules without hassle, I created a Python Script, that uses the module subprocess, to write an osgeo4w command called ‘python-qgis’ Its nothing complicated, here it is
enter image description here

I called it NotePadPlus.py (No real reason for calling it that) I then call this script from the osgeo4w command shell

enter image description here

And now I am able to call the qgis module without any issue! And just to show its not just pretending to accept it I misspelled the Tkinter module and it says it does not exist enter image description here

I hope this helps, and you can adapt it to suit your needs!

  • 2
    Couldn't you just call python-qgis from the osgeo4w shell (whithout the python subprocess wrapper script)? – Matthias Kuhn Jun 30 '16 at 7:16
  • 1
    yes you can! what I have done is created an entire python script that does various tasks, and at the start the subprocess calls the python-qgis, and this then allows the qgis and other modules to be called in the rest of that same script – user32496 Jun 30 '16 at 7:41
1

Here is probably the best solution I have found, it has made very easy thanks to http://www.qgistutorials.com/en/docs/running_qgis_jobs.html.

First thing you need to do is create a batch file, call it "launch", and enter the following enter image description here

Then you create your GIS Standalone Python script, hopefully, this is more complex than mine, but this is just to show you that the modules import correctly.

enter image description here

Then all you need to do is trigger the launch batch file( I just double clicked mine) and it will import the modules without issue!

enter image description here

Your Answer

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