0
$\begingroup$

I want to run a python script in Blender via the console, but I split the script into multiple files. The folder consists of two files, "main.py" and "poser.py". I call the main script like this:

blender -b --python E:\Development\Python\BlenderPoser\main.py

But I get the following error when trying to import the "poser.py" file:

Error: Python: Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "E:\Development\Python\BlenderPoser\main.py", line 5, in <module>
    from . import poser
ImportError: attempted relative import with no known parent package

What I get from this is that Blender only imports the main.py file, and ignores all of the rest. How can I tell Blender to also import the rest of the files?

$\endgroup$
3
  • $\begingroup$ If I'm not mistaking, you should write something like from poser import <module name, or class> $\endgroup$
    – L0Lock
    Sep 15 at 9:12
  • $\begingroup$ Does this help? Having trouble creating an Addon with multiple modules $\endgroup$
    – L0Lock
    Sep 15 at 9:19
  • $\begingroup$ Using "from poser import Poser" results in the message "ModuleNotFoundError: No module named 'poser'". And I do know how to create a modular plugin, but I would like to create a script without making it a full plugin. $\endgroup$ Sep 15 at 9:31

1 Answer 1

0
$\begingroup$

I solved it by adding the additional files into a separate folder and adding that folder to the system path:

# Add the folder to the system path, so it can be loaded by the script
main_dir = pathlib.Path(os.path.dirname(__file__)).resolve()
core_dir = os.path.join(main_dir, "core")
sys.path.append(str(core_dir))

After this, I am able to import the poser.py file with import poser

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.