0

I have file with only bare data like: foo.py

TL = {
'aa': {'L1104205608': {'ada': {'bb': 'APC', 'ip_addr': '10.44.184.12', 'port': 2}},
       'L1104205616': {'ada': {'bb': 'APC', 'ip_addr': '10.44.184.13', 'port': 3}}}}
aaa = 'bbb'

I need to import files like this, then unpack values, that are inside it only:

I do use:

imp.load_source('foo', r'<path_to_foo>\foo.py')

...and I am stuck here :( How to get an output with 'TL' and 'aaa' with their vlues?

2 Answers 2

1

You realy needed dynamicly load module? I think you just need

import foo
print(foo.TL)
print(foo.aaa)

If <path_to_foo> isn't package. You can do before import

import sys
sys.path.append(<path_to_foo>)

If you need dynamic import. Probably you can make

foo = imp.load_source('foo', r'<path_to_foo>\foo.py')
print(foo.TL)
print(foo.aaa)
Sign up to request clarification or add additional context in comments.

1 Comment

quite close, 1) but i do not know the actual name of 'foo' (it will be for all files in dir, unknown number and names) 2) 'TL' and 'aaa' are example values too. At the execution time, script doesn't know number and names of those values Also, the dynamic import is what i did.
0

What I was looking for was vars():

print(var) for var in vars(module)

What is yet unknown to me i s how to reduce depth to 1, (if imported module also imports modules, I don't want to see their vars)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.