# Importing python modules and text files

I am new to blender, but not python, and have been asked to create a user-input dependent, stylized animation of a protein network I am working with:

text document >> information processed by python-script >> blender animates

So, if certain parameters in the network are changed, the differences in behavior can be observed visually as well.

Instead of coding in Blender's built-in Text Editor, I have been working in "pycharm". However, when I attempt to run the program through blender, it will throw an error and inform me that all my custom modules cannot be found and imported, though they are all in the same directory. Same goes for all the text documents. The text files in particular are a problem as we are talking about thousands of data-points that can't be manually entered.

All modules and text files are saved in the same directory as the program's master runner.

I am working on a windows 7, 64 bit windows machine.

the errors:

 # import error:
no module named 'x'
# filenotfounderror:
[errno2] No such file or directory: 'y'


blender_file_directory: C:\Users\Henry\Documents\Blender\file.blend

python_files_directory: C:\Users\Henry\PycharmProjects\project_name\runner.py

# import of modules:
from module import *

# import of txt file:


The script is called through blender's own SDK like so:

filename = "C:/Users/Henry/PycharmProjects/project_name/runner.py"


Text files are stored in the same directory as python scripts, so importing them when running the script through python is no problem. Only when it's run through blender does it throw an error.

• What error is thrown? Could you add the imports and directory structure you use? – stacker Jul 8 '15 at 13:04
• @stacker I hope the information provided in the edit suffices? – Henry Ehlers Jul 8 '15 at 13:18

When you execute a python script inside Blender, the current working directory is not the base directory of your .blend file. You can easily confirm this by importing os and printing os.getcwd(), and that will explain why blender isn't finding these modules you're trying to import.

you'll get the current .blend filepath using bpy.data.filepath, from which you should be able to construct relative paths, or switch pythons working directory. if that's more convenient (remember to switch it back )

• my bpy.data.filepath is empty, as I haven't created any file. os.getcwd() returns the path of my modules. Yet, I get the error. – McLawrence Sep 4 '17 at 13:06
• feel free to ask a proper new question, that will give you room to describe your scenario and you might get a more pointed answer. – zeffii Sep 5 '17 at 11:38

A more detailed example of the technique zeffi outlined is illustrated by the following example from http://web.purplefrog.com/~thoth/blender/python-cookbook/import-python.html

import bpy
import sys
import os

dir = os.path.dirname(bpy.data.filepath)
if not dir in sys.path:
sys.path.append(dir )
#print(sys.path)

import cityFunctions

# this next part forces a reload in case you edit the source after you first start the blender session
import imp

# this is optional and allows you to call the functions without specifying the package name
from cityFunctions import *

• This solution works for me. However, it is not clear to me why: printing dir gives me an empty string (len(dir)=0) and dir is not where my modules are, which are at the current working directory. – McLawrence Sep 4 '17 at 13:16
• maybe having an empty string in your path is equivalent to having . in your path. – Mutant Bob Sep 5 '17 at 14:25

I have been using this snippet at the beginning of main python script to be able to load other text blocks from the same blend file (without having to save them first as file) because other answers did not do it for me:

import sys
import bpy
for name, text in bpy.data.texts.items():
if name.endswith('.py') and name[:-3] not in sys.modules:
sys.modules[name[:-3]] = text.as_module()