# Different ways to call an imported function from a separate py file, returns error

Not sure if this is a normal python behaviour or a Blender thing

I have a file named operators.py and a file named functions.py. also a __innit__.py that calls the operators.

Method 1 :

in functions.py:

def someFunction(self, context):
print("I did it!")


in operators.py:

from functions import someFunction


and then later I call the function with:

someFunction(self, context)


The above resulted in:

ImportError: cannot import name 'someFunction'


Method 2 :

in functions.py:

def someFunction(self, context):
print("I did it!")


in operators.py:

import functions


and then later I call the function with:

functions.someFunction(self, context)


The above resulted in:

AttributeError: module 'functions' has no attribute 'someFunction'


So blender did get and import the module but it can't get the functions working.

• How are you running the script? are you opening the files in blender's text editor and running them? or telling blender to run the script from the cli? is the script in the current dir when you start blender? – sambler May 30 '18 at 4:39
• opening the files in blender's text editor and running them. Everytime I hv to press run script to on each file to get it up and running. Am I doing it wrong? – adrian li May 30 '18 at 11:13

Your function seems to be in a class (guessed from the self parameter).
Method using self are meant to be used on instances.
Also the package init file should be named __init__.py, not __innit__.py.
• thanks i think that's the reason. But i though if i call the function from some other classes, as long i called it with (self, context) parameter it will run the same. for example i have to call a self.report() inside the function, how do I achieve that without having the (self) parameter? – adrian li May 30 '18 at 11:04
• I have changed the file name to __init__.py. On another matter, when I have multiple .py files in the blender text editor, I must pressrun script on every single one of them in order to get everything up and running, is this normal or am I doing something wrong? – adrian li May 30 '18 at 11:06
• I am trying to call the def from another class that also have the (self, context) parameter, the def itself is not written inside a class. is it not ok? – adrian li May 30 '18 at 11:15