Skip to main content
edited tags
Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180
deleted 101 characters in body; edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I've been trying, along with this post, to run a script in OSGeo4w Shell, outside of QGIS. But I get the following error:

ImportError: No module named qgis.core

I have also read the following posts and tried to import various modules but to no avail:

Here is a simple script which creates a grid and clips a polygon shapefile onto it. Any help in getting this script to work outside QGIS would be greatly appreciated.  

Note: This script has been tested and works successfully when running in QGIS.

##Test=name

import os
import glob
import sys

sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):
            outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
            outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)
run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

EDIT:

Following the answer and the script posted by @gcarrillo, I can finally import the qgis.core. modules successfully. The script provided by @gcarrillo (many, many thanks!) runs but I receive a Traceback error:

Traceback (most recent call last):
  File "Test.py", line 55, in <module>
    run()
  File "Test.py", line 53, in run
    algClip.processAlgorithm(progress)
  File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
    layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'

I've been trying, along with this post, to run a script in OSGeo4w Shell, outside of QGIS. But I get the following error:

ImportError: No module named qgis.core

I have also read the following posts and tried to import various modules but to no avail:

Here is a simple script which creates a grid and clips a polygon shapefile onto it. Any help in getting this script to work outside QGIS would be greatly appreciated. Note: This script has been tested and works successfully when running in QGIS.

##Test=name

import os
import glob
import sys

sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):
            outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
            outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)
run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

EDIT:

Following the answer and the script posted by @gcarrillo, I can finally import the qgis.core. modules successfully. The script provided by @gcarrillo (many, many thanks!) runs but I receive a Traceback error:

Traceback (most recent call last):
  File "Test.py", line 55, in <module>
    run()
  File "Test.py", line 53, in run
    algClip.processAlgorithm(progress)
  File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
    layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'

I've been trying, along with this post, to run a script in OSGeo4w Shell, outside of QGIS. But I get the following error:

ImportError: No module named qgis.core

I have also read the following posts and tried to import various modules but to no avail:

Here is a simple script which creates a grid and clips a polygon shapefile onto it. 

Note: This script has been tested and works successfully when running in QGIS.

##Test=name

import os
import glob
import sys

sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):
            outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
            outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)
run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Following the answer and the script posted by @gcarrillo, I can finally import the qgis.core. modules successfully. The script provided by @gcarrillo runs but I receive a Traceback error:

Traceback (most recent call last):
  File "Test.py", line 55, in <module>
    run()
  File "Test.py", line 53, in run
    algClip.processAlgorithm(progress)
  File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
    layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'
replaced http://gis.stackexchange.com/ with https://gis.stackexchange.com/
Source Link

I've been trying, along with this postpost, to run a script in OSGeo4w Shell, outside of QGIS. But I get the following error:

ImportError: No module named qgis.core

I have also read the following posts and tried to import various modules but to no avail:

Here is a simple script which creates a grid and clips a polygon shapefile onto it. Any help in getting this script to work outside QGIS would be greatly appreciated. Note: This script has been tested and works successfully when running in QGIS.

##Test=name

import os
import glob
import sys

sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):
            outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
            outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)
run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

EDIT:

Following the answer and the script posted by @gcarrillo, I can finally import the qgis.core. modules successfully. The script provided by @gcarrillo (many, many thanks!) runs but I receive a Traceback error:

Traceback (most recent call last):
  File "Test.py", line 55, in <module>
    run()
  File "Test.py", line 53, in run
    algClip.processAlgorithm(progress)
  File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
    layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'

I've been trying, along with this post, to run a script in OSGeo4w Shell, outside of QGIS. But I get the following error:

ImportError: No module named qgis.core

I have also read the following posts and tried to import various modules but to no avail:

Here is a simple script which creates a grid and clips a polygon shapefile onto it. Any help in getting this script to work outside QGIS would be greatly appreciated. Note: This script has been tested and works successfully when running in QGIS.

##Test=name

import os
import glob
import sys

sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):
            outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
            outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)
run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

EDIT:

Following the answer and the script posted by @gcarrillo, I can finally import the qgis.core. modules successfully. The script provided by @gcarrillo (many, many thanks!) runs but I receive a Traceback error:

Traceback (most recent call last):
  File "Test.py", line 55, in <module>
    run()
  File "Test.py", line 53, in run
    algClip.processAlgorithm(progress)
  File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
    layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'

I've been trying, along with this post, to run a script in OSGeo4w Shell, outside of QGIS. But I get the following error:

ImportError: No module named qgis.core

I have also read the following posts and tried to import various modules but to no avail:

Here is a simple script which creates a grid and clips a polygon shapefile onto it. Any help in getting this script to work outside QGIS would be greatly appreciated. Note: This script has been tested and works successfully when running in QGIS.

##Test=name

import os
import glob
import sys

sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):
            outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
            outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)
run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

EDIT:

Following the answer and the script posted by @gcarrillo, I can finally import the qgis.core. modules successfully. The script provided by @gcarrillo (many, many thanks!) runs but I receive a Traceback error:

Traceback (most recent call last):
  File "Test.py", line 55, in <module>
    run()
  File "Test.py", line 53, in run
    algClip.processAlgorithm(progress)
  File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
    layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'
Improving title
Link
MaryBeth
  • 3.7k
  • 24
  • 42
Loading
Removed "Update" which included final script and made it into an answer
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading
Notice removed Reward existing answer by Joseph
Bounty Ended with Germán Carrillo's answer chosen by Joseph
Changed final script to match that of updated answer
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading
Notice added Reward existing answer by Joseph
Bounty Started worth 100 reputation by Joseph
Added final script
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading
Added additional information regarding error
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading
Tweeted twitter.com/#!/StackGIS/status/555114761990705152
Error still persists so added additional information
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading
deleted 12 characters in body
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading