# Can't get script to auto execute [duplicate]

I am an experienced python programmer, but just getting into the Blender API. I am stuck on trying to get a script to execute when the Blender file is loaded. I want to do all sorts of setup in this script, but to start with, I'm just trying to change the python path to add my working directory, so that .py files can be imported from there.

So, I opened a text editor window in a fresh project, then created a new text block, into which I typed the following 2 lines:

import sys
sys.path.append('/Path/To/My/Blender/Project')


When I use the "run script" button, and check sys.path in the console, my path has indeed been added.

So then, I checked the register toggle for the text block, and set the user preferences to auto run python scripts. I saved the .blend file, exited Blender, and re-started by double clicking the .blend file.

When I check sys.path, it is the normal blender path, but if I then run the script that should have auto run, sys.path changes as it should. So it would seem that my script is not running on load.

I am on Xubuntu 14.04 (Trusty) and my Blender version is 2.78. I must be making some really simple mistake, but I can't seem to find it. Thanks for any suggestions.

• Does this answer help? Mar 20 '17 at 13:40
• I think I'm already doing all the things mentioned in that answer, but I still get no autorun. Mar 21 '17 at 15:35

Note that this executes on ANY Blender File you open, but maybe it will suffice.

import bpy
import sys

@bpy.app.handlers.persistent
def setup_path(*args):
sys.path.append('/Path/To/My/Blender/Project')
setup_path()


Put this code into a Python file and place it into ~/.config/blender/2.78/scrips/startup/setup-path.py (replace ~/config/blender with %APPDATA%/Blender Foundation/Blender on Windows, and something along those lines on MacOS).

If you're on windows, use double backslashes instead:

sys.path.append('\\Path\\To\\My\\Blender\\Project\\')


in Python, backslashes have a special use,
so you need two to literally indicate that symbol:
https://docs.python.org/2.0/ref/strings.html

oh, and if memory serves correct, you need the trailing slash(es) as well.

...

I've run into situations where trailing slashes are needed:

cwd  = os .getcwd()

if cwd[0] is '/':   ##  linux
inpu  = '/png/'
outpu  = '/obj/'
else:                 ##  win
inpu  = '\\png\\'
outpu  = '\\obj\\'

Re  = png .Reader(cwd + inpu + img + filetype)
...
with open(cwd + outpu + img + '.mtl', 'w') as mtl:
...
''.join(cwd, filename)


None of the last 3 commands would function without it, so when I see a path written without it, alarm bells ring, because I know it can cause issues with scripts.

• They already stated their code works the first time. It is running the code automatically they have issue with. Mar 20 '17 at 12:28
• "Run" doesn't mean it gets past that point. I stand by my answer. Mar 20 '17 at 16:00
• Your reference link is pointing to python 2 instead of 3. Not the downvoter and honestly I don't care, leave it as is... Just wanted to point out that your answer has nothing to do with this question - OP's operating system is Linux (no ugly slash-hacks required) @Doyousketch2 Mar 20 '17 at 19:33
• The trailing slash is not needed. When you import a python module, the interpreter automatically inserts the separator appropriate to the OS between the path and the file name. In my case, there is no exception raised, the script simply fails to execute. Mar 21 '17 at 15:38
• The normal practice in python is to use os.path.join to construct file paths. No os specific logic required. Mar 21 '17 at 19:11