0
$\begingroup$

I have the following structure:

enter image description here

The repo folder is located over ~/Desktop/Github/ and when I run from the command line blender --background --python ./Desktop/Github/repo_folder/main.py I get the following error:

enter image description here

I want to import all the classes/functions in utils.py to main.py to do that I tried the following things:

1) from scripts.utils import * got an error that scripts module wasn't recognized.

2) moved main under scripts and edited the line from .utils import *

3) tried to create __init__.py file under scripts

I use python 3.6.8 on Ubuntu 18.04 when trying (1) no errors marks shown over the IDE (Pycharm) Blender version - 2.8 installed using snap (if that helps)

Thank you for helping!

$\endgroup$
2
  • $\begingroup$ In python the dots refer to submodules, not folders. Check the init.py if it says anything about the utils. Also are you sure that you need the utils? If that's an addon there's a good chance all you need is accessible from the main. $\endgroup$ Oct 8, 2019 at 7:18
  • $\begingroup$ it is not an add on ant the init.py file is empty file $\endgroup$
    – AvivSham
    Oct 8, 2019 at 7:27

2 Answers 2

1
$\begingroup$

Found the solution! since using third party (Blender)

import sys
sys.path.insert(0, <path_to_repo>)

is needed to set the environmental variables

$\endgroup$
1
$\begingroup$

An example that should work with Blender 2.80:
The directory:

folder
  | -> main.py
  | -> scripts
         -> __init__.py
         -> hello.py

scripts/__init__.py:

from scripts.hello import *

scripts/hello.py:

def hello():
    print('Hi')

main.py

import bpy
import sys

if __name__ == '__main__':
    sys.path.append('')
    import scripts
    scripts.hello()

And you launch it with: blender -b -P main.py.


Adding Avivsham's answer below. The script directory must be in path. Lazy dynamic loading (put at the beginning of your file):

import sys
from pathlib import Path
scriptDir = Path(__file__).parent.absolute()
if scriptDir not in sys.path:
    sys.path.insert(0, str(scriptDir))
$\endgroup$
3
  • $\begingroup$ It doesn't work for me. $\endgroup$
    – AvivSham
    Oct 8, 2019 at 9:06
  • $\begingroup$ What is the error? $\endgroup$ Oct 8, 2019 at 9:45
  • $\begingroup$ Found the solution! since using third party (Blender) ``` import sys sys.path.insert(0, "/home/aviv/Desktop/Github/VideoReasoning")``` is needed to set the environmental variables $\endgroup$
    – AvivSham
    Oct 8, 2019 at 10:13

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.