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

Etienne Trimaille etienne.trimaille at gmail.com
Thu May 4 02:29:17 PDT 2023


If you are talking about the simple Python console panel, and loading
Python files from there, then you can skip the " from .script_2 import
TestClass".

If you notice, when you launch the script_2.py, you have a generated line
similar to :
exec(Path('/tmp/tmp5g244g4w.py').read_text())

So it's reading and executing all the content of file 2 in the console
context already.
So then, when file 1 will be executed, there will already be the class
TestClass in the console context.

But, then to manage multiple files, it's not convenient. You should think
about the smallest plugin, for instance
https://github.com/wonder-sk/qgis-minimal-plugin Otherwise, it's difficult
to manage IMHO.

Le jeu. 4 mai 2023 à 08:59, Raymond Nijssen via QGIS-Developer <
qgis-developer at lists.osgeo.org> a écrit :

> 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
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230504/0cf83382/attachment.htm>


More information about the QGIS-Developer mailing list