4

I am trying to build a a library of scripts to call inside the QGis Python console/Python editior. I do not understand why they work independently, but not when being called from another script. Example:

file c:\scripts\A.py

def create_copy(input,output): 
    layer = QgsVectorLayer(input, "input", "ogr")
    error = QgsVectorFileWriter.writeAsVectorFormat(layer, output, "utf-8", None, "ESRI Shapefile")

I can call that function, from within the same script:

create_copy(r"z:\original.shp", r"z:\copy.shp")

works as intended.

Now if I call this from another script, like so:

file c:\scripts\B.py

import sys
sys.path.insert(0, r'c:\scripts')
import A

A.create_copy(r"z:\original.shp", r"z:\copy.shp")

I get

NameError: global name 'QgsVectorLayer' is not defined.

Even though the call is made from within the QGis Python Editor. What am I missing?

  • did you try to include the qgis imports with ´from qgis.core import * from qgis.gui import * from qgis.utils import *`? – LaughU Oct 12 '17 at 9:58
  • Yes, in both files. Problem remains. – Ben the bear Oct 12 '17 at 10:08
6

Try replacing sys.path.insert(0, r'c:\scripts') with sys.path.append(r'c:\scripts') instead and add the required imports in your A.py.


c:\scripts\A.py

from qgis.core import QgsVectorLayer, QgsVectorFileWriter

def create_copy(input,output): 
    layer = QgsVectorLayer(input, "input", "ogr")
    error = QgsVectorFileWriter.writeAsVectorFormat(layer, output, "utf-8", None, "ESRI Shapefile")

c:\scripts\B.py

import sys
sys.path.append(r'c:\scripts')
import A

A.create_copy(r"z:\original.shp", r"z:\copy.shp")
  • This was helpful, in that it pointed me in the right direction and actually works with the provided minimal working example. However it does not make it clear to me, why from qgis.core import QgsVectorLayer, QgsVectorFileWriter works and import qgis.core does not, neither does the suggest way by @LaughU. I think the fact that the scripts run on their own within QGis, might obfuscate some things that go on in the background. For example just restarting QGis sometimes changes whether the scripts complete or not. – Ben the bear Oct 12 '17 at 12:24
  • The supposed benefit of sys.path.insert(0...)is to make sure, that the added directory is searched first in case of ambiguity. Changing it did not make a difference. – Ben the bear Oct 12 '17 at 12:25
  • @Benthebear - Yes, I have noticed that I also had to restart QGIS before running the script before it 'reloaded' correctly. Not sure why import qgis.core... – Joseph Oct 12 '17 at 12:33
1

Another example of importing module into PyQgis:

Save in: '../ToolsFiles/File1.py

def HelloWorld():
  return "Welcome to PyQgis"

Save in: '../ToolsFiles/File2.py'

def HelloWorld2():
  return "Welcome to PyQgis 2"

Save in: '../Code.py' and execute next lines inside console Qgis.

import sys
tools = "..//ToolsFiles" #variable#
sys.path.append(tools) 
import File1 as FILE_1
import File2 as FILE_2

message1 = FILE_1.HelloWorld() #call function#
print(message1)

message2 = FILE_2.HelloWorld2() #call function#
print(message2)

Good luck ...

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.