简体   繁体   中英

Importing values from python module

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?

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)

What I was looking for was vars():

print(var) for var in vars(module)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM