2

I have a folder structure like this:

C: 
|_Blueprint
│   main.py
│
└───toolbox
        blueprint_tools.py

When I run this code in Blender's scripting text page:

from toolbox.blueprint_tools import dimension, array, modify

I get an error in the console

Traceback (most recent call last):
  File "\main.py", line 20, in <module>
ModuleNotFoundError: No module named 'toolbox'
Error: Python script failed, check the message in the system console

I swear, when I run this code (on other projects) outside of Blender this structure and code runs fine. But I get an error when running this script in Blender for testing out my Add-On.

If I compile the Blueprint folder into a .zip and install it everything works fine.... ?? I'm lost, could anyone please help me.

If I run the code like this: (added a . before toolbox for CWD)

from .toolbox.blueprint_tools import dimension, array, modify
Traceback (most recent call last):
  File "\main.py", line 20, in <module>
ImportError: attempted relative import with no known parent package
Error: Python script failed, check the message in the system console
MarianD
  • 13,096
  • 12
  • 42
  • 54
Davi Silveira
  • 87
  • 1
  • 12
  • [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – MarianD Dec 19 '20 at 14:17

1 Answers1

2

Both your error traces write File \main.py ... which means that Blender considers your main.py file to be in the root folder, knowing nothing about its real location in the file system hierarchy.

When you installed your structure as a zip file, you provided all necessary info to Blender.


Addendum:

Temporarily, during developing / debugging your add-on, you may add the full path (for finding your toolbox.blueprint_tools module) to the sys.path variable.

There are 2 possibilities how to do it:


  1. Insert into your main.py file these commands (use the path to your parent folder of your toolbox folder, of course):

    import sys
    sys.path += [r"C:\Users\Davi\Documents\Python\PARENT_of_toolbox"]
    

    before your statement

    from toolbox.blueprint_tools import dimension, array, modify 
    

    command, or


  1. Insert into your main.py file these commands (use the path to your toolbox folder, of course):

    import sys
    sys.path += [r"C:\Users\Davi\Documents\Python\PARENT_of_toolbox\toolbox"]
    

    before your modified statement

    from blueprint_tools import dimension, array, modify
    
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Thank you, your answer makes perfect sense. But I guess the only thing left is how do I give Blender all the info it needs to run my script in Blender for testing purposes? – Davi Silveira Dec 11 '20 at 17:26
  • Use the zip file, as you already did. You may find more info about Blender specific questions in [Blender StackExchange](https://blender.stackexchange.com/). Maybe [this answer](Use the zip file, as you already did. You may find more info about Blender specific questions in [Blender StackExchange](https://blender.stackexchange.com/). Maybe [this answer](https://blender.stackexchange.com/a/203926). – MarianD Dec 12 '20 at 05:36
  • Thanks again, it's just horrible to have to compile a zip and install it just to see a small change I made to the add-on only to make a new change and have to uninstall recompile and try again... you see what I mean. This crazy thing works as a zip install but doesn't as a python.py file running inside Blenders script panel. – Davi Silveira Dec 12 '20 at 17:49
  • MarianD thank you so much. I'll give this a try tomorrow morning and report back. I appreciate your help. – Davi Silveira Dec 14 '20 at 07:51