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 

This is my module_test_hello.py file:

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

All my files have the same path.

  • $\begingroup$ 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. $\endgroup$
    – Abhay
    Jan 17 '18 at 11:44

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

# we call function from the module now
  • $\begingroup$ 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. $\endgroup$
    – Fred
    Jan 17 '18 at 22:53
  • $\begingroup$ 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. $\endgroup$
    – Absulit
    Aug 18 '19 at 0:30

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.