I am getting an error when I try to load up a function from a module I made:
module ‘my_module.module_functions’ has no attribute ‘example_function’
I want to preface everything by saying, for better or worse, it works fine with the consts/vars inside of blender. It seems to be everything else thats not working. Also, I tested just a print function in a python console separately and it works fine. It is JUST in blender that it doesnt work. I also want to say dont worry about other imports like bpy etc. I also am using a sys append to get the exact path of the module for the time being because my blender addon is not actually in the same place as the module right now but as mentioned, this is working fine for everything else. Just having issues with functions. The following is just for example.
I appreciate it. Yeah you can ignore the typo. I replaced the names of the actual functions in here. In VS Code, nothing flags it as being incorrect on the actual functions and I used autocomplete to ensure it is correct.
as far as relative imports go, wouldn’t it be a relative import into the init.py doing the from relative path and then import so I just need to call the package folder? Versus absolute where in the file I track all the way to the exact file? or maybe I am mistaken
I will loose the wildcard them but I will add I still have the problem importing the exact function instead of *
I’m not really sure that you’re asking here. We use relative paths for sub-directories because they’re not directly on the python path (sys.path). I guess you could do addon_name.subdirectory but that would break if the addon folder name changed and there’s no similar downside to using relative imports instead.
Remove the __init__.py from the subdirectory (it’s not needed in Python3 ) and then do your imports as:
from . import my_module
or from .my_module import module_functions.py
VS code may falsely flag the relative imports but they’ll work.
oh, I was just saying I was using the init.py to contract the total path so I could just call the directory instead of having to type out the whole path each time, so effectively it was a relative path to the init.py but based on your reply I think I have a better understanding now so Ill give that a try next too and report back soon on each of these.
ok, I am about to try what you suggested but I just made a discovery. Using a function that just prints something was a bad example because it turns out THAT works fine. In actual use case it looks like this:
and I think it’s the arguments, sorta. The strings being passed to the arguments come from another module so I am wondering if something in how I am doing that is actually the problem so I am going to tinker with that a bit too once I try restructuring the modules like you suggested.
Just going to edit this real fast to stress the importance of formatting for any other people new to python who see this down the road. I dont know what it was but I just ran a pep8 formatter on all the .py files and now it works. Something that wasn’t getting flagged by vscode must have been causing the problem. I dont know what though. Didn’t actually tell me what the problem was. I wish it did so I could post here for other people what the problem was specifically but I want to just throw out there that that seems to have fixed it.
Thank you for filling me in on the file structure. I will go back and fix up the packages and let you know if I have any problems. I appreciate it!