[QGIS-Developer] Multiple python scripts in python console?

Raymond Nijssen r.nijssen at terglobo.nl
Wed May 3 23:34:02 PDT 2023


Hi developers,

While writing a python script in the QGIS python console editor I needed 
to write a new class and for convenience I put that in its own python file.

Both files are in the same directory, but I cannot import my class in 
the other script.

Here's an example:


# script_1.py

from .script_2 import TestClass
obj1 = TestClass('Ray')
print(obj1)




# script_2.py:

class TestClass():

     def __init__(self, name):
         self.name = name

     def __str__(self):
         return f'TestClass[{self.name}]'



It all works properly if I put my class definition in script_1 but as 
the code is growing (and a customer of mine needs to understand this 
too) it would really be nice to split the code over multiple files.
Is there a way? Or another approach?

Kind regards,
Raymond


More information about the QGIS-Developer mailing list