<div dir="ltr"><div dir="ltr">> For example, a newly defined function on the class was <br>
> unknown in script_1. Might be a bug, but I'm not even sure.</div><div dir="ltr"><br></div><div>Well, for me it works without any issue.</div><div>But you need to re-execute the <span class="gmail-im">script_2.py in the Python console, it will refresh the class definition in the Python console context then you can execute script_1 again.</span></div><div><span class="gmail-im"><br></span></div><div><span class="gmail-im">But indeed, you are reaching the limit of using the Python console like this ...<br></span>
</div><div><br></div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le jeu. 4 mai 2023 à 16:02, Raymond Nijssen <<a href="mailto:r.nijssen@terglobo.nl">r.nijssen@terglobo.nl</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Etienne,<br>
<br>
Already tried what you are describing. Indeed it's inconvenient, and <br>
sometimes the changes in the class code did not end up in the class <br>
definition. For example, a newly defined function on the class was <br>
unknown in script_1. Might be a bug, but I'm not even sure.<br>
<br>
So the best way should be to tell QGIS to run another python script in <br>
the console. Or go the plugin way.<br>
<br>
Thanks and kind regards,<br>
<br>
Raymond<br>
<br>
<br>
On 04-05-2023 11:29, Etienne Trimaille wrote:<br>
> If you are talking about the simple Python console panel, and loading <br>
> Python files from there, then you can skip the " from .script_2 import <br>
> TestClass".<br>
> <br>
> If you notice, when you launch the script_2.py, you have a generated <br>
> line similar to :<br>
> exec(Path('/tmp/tmp5g244g4w.py').read_text())<br>
> <br>
> So it's reading and executing all the content of file 2 in the console <br>
> context already.<br>
> So then, when file 1 will be executed, there will already be the class <br>
> TestClass in the console context.<br>
> <br>
> But, then to manage multiple files, it's not convenient. You should <br>
> think about the smallest plugin, for instance <br>
> <a href="https://github.com/wonder-sk/qgis-minimal-plugin" rel="noreferrer" target="_blank">https://github.com/wonder-sk/qgis-minimal-plugin</a> <br>
> <<a href="https://github.com/wonder-sk/qgis-minimal-plugin" rel="noreferrer" target="_blank">https://github.com/wonder-sk/qgis-minimal-plugin</a>> Otherwise, it's <br>
> difficult to manage IMHO.<br>
> <br>
> Le jeu. 4 mai 2023 à 08:59, Raymond Nijssen via QGIS-Developer <br>
> <<a href="mailto:qgis-developer@lists.osgeo.org" target="_blank">qgis-developer@lists.osgeo.org</a> <mailto:<a href="mailto:qgis-developer@lists.osgeo.org" target="_blank">qgis-developer@lists.osgeo.org</a>>> <br>
> a écrit :<br>
> <br>
>     Hi developers,<br>
> <br>
>     While writing a python script in the QGIS python console editor I<br>
>     needed<br>
>     to write a new class and for convenience I put that in its own<br>
>     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" target="_blank">self.name</a> <<a href="http://self.name" rel="noreferrer" target="_blank">http://self.name</a>> = name<br>
> <br>
>           def __str__(self):<br>
>               return f'TestClass[{<a href="http://self.name" rel="noreferrer" target="_blank">self.name</a> <<a href="http://self.name" rel="noreferrer" target="_blank">http://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">QGIS-Developer@lists.osgeo.org</a> <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a>><br>
>     List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>     <<a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="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" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>     <<a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>><br>
> <br>
</blockquote></div></div>