0

I want to import .py files to my script. But I have this error message. I´m not sure but I think I got it any time before. I don´t understand why now I can´t get it.

I have a file called "Hello_Test.blend" Inside I have a script called script_test_hello.py:

import module_test_hello 
hello()

This is my module_test_hello.py file:

def hello():
    print("Hello World!")

All my files have the same path.

Mr. Baldan
  • 547
  • 1
  • 5
  • 11
  • Please check if you have that module placed in the relative folder from where all the modules are to be imported or just specify your own path. – Abhay Jan 17 '18 at 11:44

1 Answers1

2

Blender cannot find your module because blender is searching for modules in the base path and your module is not there, so you need to append your modules path to a path that blender can find it, so you need to execute some functions to do this.

import bpy
import sys
import os

# we get blend file path
filepath = bpy.data.filepath

# we get the directory relative to the blend file path
dir = os.path.dirname(filepath)

# we append our path to blender modules path
# we use if not statement to do this one time only
if not dir in sys.path:
   sys.path.append(dir )

# now blender is able to find our modules
import module_test_hello

# in case we edited our script file after blender had started 
# we reload the module via imp module
import imp
imp.reload(module_test_hello)

# we call function from the module now
module_test_hello.hello()
Fred
  • 126
  • 7
  • hmm I didn't understand his question because my english is poor, any way i will update my answer, from your explain I understand what he need. – Fred Jan 17 '18 at 22:53
  • This is a bit odd, I was watching a guy making a tutorial about addons and he did not need this, and he was on Windows as I am. – Absulit Aug 18 '19 at 00:30