# If "bpy" in locals(): evals to True on first run

I've put together this multi-module script, which lives in a make_Shaders folder, which is also the name of the module that makes the shaders: make_Shaders.py. I'm using this code in the __init__.py to load (import) the modules:

# 'borrowed' from Antonioya - elegant solution!
path = sys.path
flag = False
for item in path:
flag = True
if flag is False:

if "bpy" in locals():
import imp

else:
import figure_Defs



When I zip and install this in a brand-new .blend (just opened Blender), I get the following error when I try to tick the 'Activate' tickmark:

addon_utils.disable: make_Shaders not disabled.
Traceback (most recent call last):
File "/home/robyn/blender276/2.76/scripts/modules/addon_utils.py", line 324, in enable
mod = __import__(module_name)
NameError: name 'figure_Defs' is not defined



Line 61 is the last statement in this segment:

if "bpy" in locals():
import imp



It seems "bpy" is in locals() straight away, when first starting Blender, which this says wouldn't happen:

This code works as follows.

If __init__.py() is run for the first time, i.e. on starting Blender with the addon enabled in your default.blend, "bpy" in locals() is False. The other files in the package are imported, and Imported multifiles is printed in the terminal. Thank you for considering my little dilemma.

I double-checked case and spelling of the modules: both correct.

• Is import bpy before or after the code shown above, secondly when importing use from . import make_Shaders and lastly why do you need to add to the sys.path ? The addon registration will do this for you. Once it is enabled you can from make_Shaders.make_Shaders import xxxx for example. Also can I suggest all lower case for make_shaders and camel case for class names class MakeShaders Nov 16 '15 at 2:35
• Is my face red! The import bpy is before... it should be after. I will change to from . import make_shaders (changing case to lower) as you suggest. The only bit I still don't get is this: "Once it is enabled you can from make_Shaders.make_Shaders import xxxx" - I thought I was limited as to where I can issue an 'import' statement, as in, not in a function. Nov 16 '15 at 11:01
• Oh, and as to the why am I adding to the syspath? got that from Antonioya, and it seemed like a good idea that the time, particularly since I've been having such dramas with paths. Finally resolved the path problem in that other thread by using a csv to store the path to the images. This works in both Linux and Windows: the users put in the path themselves. Probably not ideal, of course, and it's going to be a nightmare to error-trap... perhaps a try - except in the execute(). Nov 16 '15 at 11:13

Following batFINGER's sage advice, I've done the following. I think I get the from make_Shaders.make_Shaders import xxxx now. I haven't seen this syntax exactly anywhere, but it works an absolute treat. My actual statement is:
from make_shaders.make_shaders import buildShader