<div dir="auto">Hi Raymond<div dir="auto"><br></div><div dir="auto">Note that this should work "out of the box" starting from QGIS 3.32 (cf <a href="https://github.com/qgis/QGIS/pull/51912">https://github.com/qgis/QGIS/pull/51912</a>)</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le ven. 5 mai 2023 à 14:50, Raymond Nijssen via QGIS-Developer <<a href="mailto:qgis-developer@lists.osgeo.org">qgis-developer@lists.osgeo.org</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Klaus,<br>
<br>
Thank you so much, it works!!<br>
<br>
Replying to you and the dev list, hoping this solution will end up in <br>
the same thread.<br>
<br>
Kind regards,<br>
<br>
Raymond<br>
<br>
<br>
On 05-05-2023 12:52, Klaus Affeldt wrote:<br>
> Hi Raymond,<br>
> <br>
> can't write to the list yet, so i answer you directly:<br>
> <br>
> The python console has a different working directory then the called script.<br>
> You have to find out the path of the script and append it to the python search path.<br>
> <br>
> <br>
> # script_1.py<br>
> <br>
> import os<br>
> from console.console import _console<br>
> <br>
> # name and path of this script<br>
> script_path = _console.console.tabEditorWidget.currentWidget().path<br>
> <br>
> # only the path<br>
> path=(os.path.dirname(script_path))+"/"<br>
> print (path)<br>
> <br>
> # append python search path<br>
> import sys<br>
> sys.path.append(path)<br>
> <br>
> from script_2 import TestClass<br>
> <br>
> obj1 = TestClass('Ray')<br>
> print(obj1)<br>
> <br>
> <br>
> # to dynamicly reload changes on script_2.py<br>
> import imp<br>
> <br>
> import script_2<br>
> <br>
> imp.reload(script_2)<br>
> obj1 = script_2.TestClass('Ray')<br>
> print(obj1)<br>
> <br>
> <br>
> <br>
> Mit freundlichen Grüßen<br>
> <br>
> Ihre PV ANSPERGER mbH<br>
> <br>
> Klaus Affeldt<br>
> <br>
> Zentrale Kamp-Lintfort<br>
> Südstraße 25<br>
> 47475 Kamp-Lintfort<br>
> Tel. +49 2842 9635-14<br>
> Fax: +49 2842 9635-99<br>
> <a href="https://www.ansperger.de" rel="noreferrer noreferrer" target="_blank">https://www.ansperger.de</a><br>
> <br>
> <br>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><br>
> <br>
> Hi developers,<br>
> <br>
> While writing a python script in the QGIS python console editor I needed<br>
> to write a new class and for convenience I put that in its own python file.<br>
> <br>
> Both files are in the same directory, but I cannot import my class in<br>
> the other script.<br>
> <br>
> Here's an example:<br>
> <br>
> <br>
> # script_1.py<br>
> <br>
> from .script_2 import TestClass<br>
> obj1 = TestClass('Ray')<br>
> print(obj1)<br>
> <br>
> <br>
> <br>
> <br>
> # script_2.py:<br>
> <br>
> class TestClass():<br>
> <br>
>       def __init__(self, name):<br>
>           <a href="http://self.name" rel="noreferrer noreferrer" target="_blank">self.name</a> = name<br>
> <br>
>       def __str__(self):<br>
>           return f'TestClass[{<a href="http://self.name" rel="noreferrer noreferrer" target="_blank">self.name</a>}]'<br>
> <br>
> <br>
> <br>
> It all works properly if I put my class definition in script_1 but as<br>
> the code is growing (and a customer of mine needs to understand this<br>
> too) it would really be nice to split the code over multiple files.<br>
> Is there a way? Or another approach?<br>
> <br>
> Kind regards,<br>
> Raymond<br>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>