# Error: No module named ' … '

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.

• If it's a script in the same folder as blend file use from . import module_test_hello If it's a text file in blender with py extension, or in scripts path (user settings), or in addon folder(s), use import module_test_hello Oh and btw need to use from module_test_hello import hello or module_test_hello.hello() since that is where hello() is. stackoverflow is a great source of answers on python coding. – batFINGER Jan 17 '18 at 11:23
• 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

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

• As I understand the question the module_test_hello.py is a py file in same folder as blend, not a text block, in which case see comment above and feel free to use in answer Only need to register text block if you want to "autorun" at start up (loading), otherwise import will work fine just with py extension. – batFINGER Jan 17 '18 at 20:36
• from . import module_test_hello will do exactly same, without need for importing os or sys. Could also place in .../2.79/scripts/modules/ folder. – batFINGER Jan 18 '18 at 8:18