0
$\begingroup$

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.

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

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()
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – batFINGER Jan 17 '18 at 20:36
  • $\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$ 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. $\endgroup$ – batFINGER Jan 18 '18 at 8:18
  • $\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 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.