3
$\begingroup$

I'm trying to import data stored as a JSON. It works fine in stand-alone python, but I am having problems in Blender python console.

I have a folder Applications/blender.app/Contents/Resources/2.74/scripts/addons/uhoh and in it I have wowtest.py, a blank __init__.py, and test_data.txt which contains the JSON.

This is wowtest.py

def wowtest(fname):

    import json

    print('hmmm.... will this work?')

    with open(fname, 'r') as infile:
        xdic = json.load(infile)

    return xdic

And this is what happens in Blender's python console:

>> import uhoh.wowtest as wow
>>> wow.wowtest('test_data.txt')
hmmm.... will this work?

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/Applications/blender.app/Contents/Resources/2.74/scripts/addons/uhoh/wowtest.py", line 9, in wowtest
    with open(fname, 'r') as infile:
FileNotFoundError: [Errno 2] No such file or directory: 'test_data.txt'

I have put a second copy of test_data' up one level (in/addons`) just in case, but this doesn't help. What am I missing?

NOTE: I will run this from a script next. If there are differences, please let me know.

$\endgroup$
5
  • 2
    $\begingroup$ Your problem is path based, search about it here and you'll find many answers: blender.stackexchange.com/questions/639/… print out where your script is running and then correct path accordingly.. $\endgroup$ Jun 5 '15 at 5:31
  • $\begingroup$ I see! I typed import os and then os.path and I see I am not in Kansas. This helps @Jerryno! I can now get it to work if I append the entire correct path name to the file_name: Q = wow.wowtest('Applications/blender.app/Contents/Resources/2.74/scripts/addons/uhoh/test_data.txt'). Are there easier, or more standard ways to do this (e.g. store special data for scripts)? I feel like I'm making a mess in the /addons folder. $\endgroup$
    – uhoh
    Jun 5 '15 at 5:43
  • $\begingroup$ @uhoh read the link suggested by jerryno, __file__ is a special keyword in python that will stand for the current location of the python file. The base of this question has nothing to do with json. $\endgroup$
    – zeffii
    Jun 5 '15 at 5:52
  • $\begingroup$ Thanks for the help @zeffii. So it seems to me that what I needed to understand (weather I knew exactly how to phrase the question or not) is well beyond the other question "How do I get the path my script is running in?" Your answer below is very helpful! Is it still necessary for me to "please edit this question to explain how it is different..."? $\endgroup$
    – uhoh
    Jun 5 '15 at 10:01
  • 1
    $\begingroup$ my advice to you is to think of the shortest possible way to describe the problem and still have a problem, here the problem was not json, it was about finding the path of another file. Those kinds of questions have been asked on the mother site 'stackoverflow' many times and have good answers, it will never hurt to check stackoverflow first for python specific stuff.. it's a grey area but we try not to teach python here. $\endgroup$
    – zeffii
    Jun 5 '15 at 10:22
3
$\begingroup$

The current file's location is obtained using __file__

import os
current_file_path = __file__
current_file_dir = os.path.dirname(__file__)

to access a file in the same directory as this python file you do

other_file_path = os.path.join(current_file_dir, "other_file.ext")
with open(other_file_path) as f:
   ...

if there's a directory on the same path as the main python file you can get that by doing

other_file_path = os.path.join(current_file_dir, "directory", "other_file.ext")
with open(other_file_path) as f:
   ...
$\endgroup$
1
  • $\begingroup$ This is a concise and useful summary of how to manage paths in scripts. Thanks @zeffii for anticipating the rest of my question! $\endgroup$
    – uhoh
    Jun 5 '15 at 10:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.