I have a few python scripts I would like to call from the QGIS Python console. It was working well until I attempted to separate some of the functions to another file -- the qgis.core modules are not available to the separate file.

Here's some of the code to explain what I mean. All of the files are in a folder called pyQgis:

from qgis.core import *
import sys
scripts = "..//pyQgis"
sys.path.append (scripts)

from other_file import get_elem

test = QgsProject.instance()

test_run = get_elem("cable_ht")


from qgis.core import *

def get_elem(layer_name):
    proj = QgsProject.instance()

The variable 'test' prints out okay. But the instance from the other file gives:

NameError: name 'QgsProject' is not defined

1 Answer 1


You are not importing the other_file module (which include its from qgis.core import * line), but only its get_elem name definition, in a separate namespace in which QgsProject name is just not defined.

You can solve it in some ways. Depending on your package structure they can work or not. For instance:

# Import the module from a relative path
from . import other_file

# Call its function
test_run = other_file.get_elem("cable_ht")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.