# Relative path to script folder using text view

I'm completely new to scripting, just started watching my first tutorial on this matter and the tutor in the video uses the built-in python scripting view, but has to copy and paste lines of code into the console to add the folder, that contains both the script and all associated files, and another line of code every time he updates the script outside of blender and also every time he restarts blender. I've found it more convenient to load the script into Text View, where I can even edit and update it more easily, but I can't seem to get Blender to load and save files, that are in the same folder as the script itself just by specifying their name, it only seems to work, when I specify the whole path. The instructor uses the command bpy.data.filepath to specify the filepath, but every time I try this, I get an error both in Intellij and in blender's text view, telling me, that bpy couldn't be found, in blender's python console, the command works just fine.

Any help or pointers in the right direction will be much appreciated.

If you are editing a script in the text editor and clicking the Run Script button then you need to have import bpy at the top of your script to use anything in the bpy module.

Blender's python console automatically imports the bpy module for you, it also gives you some shortcuts like D = bpy.data so you can simply use D.filepath in the python console. The console also imports math and mathutils items so you can simply use radians() in the console but will need to import it if you use it in a script.

Convenience Imports: from mathutils import *; from math import *
Convenience Variables: C = bpy.context, D = bpy.data

It is also possible to run text from the text editor in the console. I use the addon mentioned in the previous link to run scripts in the console to make debugging easier.

• Thanks, your answer got mefurther, 'import bpy' was giving me errors yesterday, if I remember correctly, but seems to work today, at least inside blender, intellij is still giving me errors though. In intellij, autocorrect and autocomplete work nicely with 'import bpy', but when I run the script in intellij, I get a 'module not found' error Also, I was hoping the piece of code: ' import bpy import os, sys sys.path.append(os.path.dirname(bpy.data.filepath)) ' would solve my relative path problem, but while it doesn't generate any errors, I still need to use absolute paths.
– ivan
May 23, 2017 at 8:42
• The bpy module is only available within the python interpreter in blender. It is possible to build blender as a module so that it can be used in any python interpreter but you will have to compile blender yourself. May 23, 2017 at 10:47
• Cool, thanks! I've found out some more from the console. os.path.dirname(bpy.data.filepath) returns the correct path,but when I specify an external file only by it's name, the script adds the .blend file name as another folder between the path and the file, like this: D:\\filePath\\filename.blend\\external file while it should be: D:\\filePath\\external file. os.path.join(os.path.dirname(bpy.data.filepath), str(external_file_name)) does work though. Any tips on how to get around this?
– ivan
May 23, 2017 at 11:14
• I don't know what code you use that joins the path with the blend file name in it but your later example of os.path.join looks right. If you are trying to make the path to an external file open in blender's text editor you can get it from bpy.data.texts['Text'].filepath May 24, 2017 at 0:33
• well... joining the code is just a workaround, since I'm unable to get Blender to look in the correct folder by default. Blender's text editor seems to append the .blend file name to the end of the path as another folder, that doesn't really exist and then ends up not finding the specified file. So specifying the absolute path is currently the only way I seem to be able to get external files to work.
– ivan
May 25, 2017 at 9:16