2
$\begingroup$

I have scripts loaded in the text editor. They are made internal, they are registered and saved under scripts/startup/myAddOns. They run OK when I run directly on the text editor. And the output goes to the System Console OK.

What I am asking is somewhat different. When I run my script from the Python Console, I do:


>>> from myAddOns import myModule      # myAddOns folder is under scripts/startup
>>>
>>> myModule
>>> <module 'myAddOns.myModule' from 'C:\\Program Files\\Blender Foundation\\Blender 3.3\\3.3\\scripts\\startup\\myAddOns\\myModule.py'>


in myModule there is a print statement:
...
...
print ("myvar", myvar)
...
...

The output of that print does not show up anywhere; neither at the Python console, nor the System Console.

Where does it go??

$\endgroup$
3
  • $\begingroup$ Are you sure the module even runs? Can you do with open("some_file_path", 'w') as f: f.write("test") and see if that creates the file? $\endgroup$ Commented Oct 4, 2022 at 14:58
  • $\begingroup$ @Markus von Broady Funny things happen there: I wrote a single line of code which prints a simple message. I imported the module. In the first run the output was printed at the Python Console. Then I added your create file, write file lines. Imported. No ouput. I thought it did not like file opening. Took them out back. Import. No output. Created a new test file with a different name. Again single line of print message. I got the output again, on the Python Console. Import again. None. Again... None... $\endgroup$ Commented Oct 4, 2022 at 15:16
  • $\begingroup$ Instead of standard python way import, if we use Blender way of import, i.e., use: my_module = bpy.data.texts["my_module"].as_module() at the python console, then everyting works fine. $\endgroup$ Commented Oct 4, 2022 at 17:15

1 Answer 1

1
$\begingroup$

Modules only get imported once by Python. Since the Python Console keeps all variables from one command to the next, the import stays imported. To reload a file you need to call:

>>> from myAddOns import myModule
 output gets printed, as the module is executed once to load its functions

>>> from myAddOns import myModule
 no output since the module does not need to be loaded as it is already loaded

>>> import importlib
>>> importlib.reload(myModule)
 output gets printed, since this will reload the library

You should not execute code on import, rather define functions which can then get called like

# In myAddOn/myModule
def run():
    print("Hello World")

# run the script if it is not executed from an import
if __name__ == '__main__':
    run()

# Then in the console do
>>> from myAddOns import myModule
>>> myModule.run()
$\endgroup$
2
  • $\begingroup$ Thanks Henrik. def run schema works. Now there is one problem left in my case. I want to call function run directly, i.e., not as myModule.run() but just run(). To do that I change the import command to: from myAddOns.myModule import *, and that simple example works fine. I can call function run directly both from the python console, and with a similar script from the text editor. But I have a problem in my real life case. I can not import my actual myModule with: " from myAddOns.myModule import * ". It gives " Attribute Error: module xxx.yyy has no attribute 'version'. will continue $\endgroup$ Commented Oct 5, 2022 at 1:44
  • $\begingroup$ ... continuing from above: as a workaround I can import each class by name like this: " from myAddOns.myModule import (classA, classB, classC) " and that works fine. But I am trying not to do that and use the wild-card *. Why do I get that version attribute error when I use * in my real life case (but not in that simple example)? $\endgroup$ Commented Oct 5, 2022 at 1:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .