Jennifer Jennifer - 1 year ago 85
Python Question

How to import dict initialized in init constructor from one python file to another?

I am trying to figure out how to import my dictionary in a python file to another dictionary. When I try:

from filename import myDict

I get "ImportError: cannot import name myDict" and I imagine this is because my dictionary is initialized as follows:

def __init__(self):
self.myDict = {}

I have also tried:

from filename import self.myDict

And then I get a syntax error that says the "." in self.myDict is invalid. If anyone can provide any suggestions on how to resolve this issue it would be greatly appreciated! Thanks.

Answer Source

Something like this?

class X:
    def __init__(self):
        self.myDict = {}

from filename import X
print X().myDict
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download