Using python dict imported from file in another python file
Asked Answered
M

5

11

I've the same issue as asked by the OP in How to import or include data structures (e.g. a dict) into a Python file from a separate file. However for some reason i'm unable to get it working.

My setup is as follows:

file1.py:

TMP_DATA_FILE = {'a':'val1', 'b':'val2'}

file2.py:

from file1 import TMP_DATA_FILE

var = 'a'
print(TMP_DATA_FILE[var])

When i do this and run the script from cmd line, it says string indices must be integers.

When i do type(TMP_DATA_FILE), i get class 'str'. I tried to convert this to dict to be able to use dict operations, but couldn't get it working.

If i do print(TMP_DATA_FILE.get(var)), since i'm developing using PyCharm, it lists dict operations like get(), keys(), items(), fromkeys() etc, however when i run the program from command line it says 'str' object has no attributes 'get'.

When i do print(TMP_DATA_FILE) it just lists 'val1'. It doesn't list 'a', 'b', 'val2'.

However the same script when run from PyCharm works without any issues. It's just when i run the script from command line as a separate interpreter process it gives those errors.

I'm not sure if it's PyCharm that's causing the errors or if i'm doing anything wrong.

Originally i had only one key:value in the dict variable and it worked, when i added new key:value pair that's when it started giving those errors.

I've also tried using ast.literal_eval & eval, neither of them worked. Not sure where i'm going wrong.

Any help would be highly appreciated.

Thanks.

Magic answered 17/6, 2015 at 14:0 Comment(4)
Forgot to mention - file1.py & file2.py are in different folders and i do have __init__.py in both folders. I also tried json options reading from some of the posts, those didn't work either.Magic
I've edited my question to change the import statement. This is the current style of import that i've which i forgot to mention while asking the question. Sorry for any confusion.Magic
check my answer once, may be it helpsYea
Thanks mescalinum for the tip. That was indeed the problem. I didn't had the right folders in PYTHONPATH. It works now. Thanks everyone for your suggestions.Magic
C
13

There are two ways you can access variable TMP_DATA_FILE in file file1.py:

import file1
var = 'a'
print(file1.TMP_DATA_FILE[var])

or:

from file1 import TMP_DATA_FILE
var = 'a'
print(TMP_DATA_FILE[var])

file1.py is in a directory contained in the python search path, or in the same directory as the file importing it.

Check this answer about the python search path.

Cribbs answered 17/6, 2015 at 14:9 Comment(1)
They could be in subdirectories not a same directory, I tested it.Enchondroma
Y
2

If you have the packages created in your project, then you have to link the file from the main project.

For example:
If you have a folder Z which contains 2 folders A and B and those 2 files file1.py and file2.py are present in A and B folders, then you have to import it this way

import Z.A.file1
print(file1.TMP_DATA_FILE)
Yea answered 17/6, 2015 at 14:14 Comment(0)
C
1

You calling it the wrong way. It should be like this :

print file1.TMP_DATA_FILE[var]
Convoluted answered 17/6, 2015 at 14:7 Comment(1)
I've tried this approach as well and this was my original code. It didn't work. I should've amended the question to reflect the latest code where i'm doing from file1 import dictVariable, and that's why i've dictVariable[var]Magic
D
1

Correct variant:

import file1    
var = 'a'
print(file1.TMP_DATA_FILE[var])

or

from file1 import TMP_DATA_FILE
var = 'a'
print(TMP_DATA_FILE[var])
Delano answered 17/6, 2015 at 14:7 Comment(0)
R
0

I use following to read constant definitions from other files when there's no conflicting symbols:

from <sub dir>.<other_file> import *
Reconstitute answered 9/6 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.