Jennifer Jennifer - 5 months ago 12
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:

filename1.py

from filename import myDict


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

filename.py

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


I have also tried:

filename1.py

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

Something like this?

filename.py

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

filename1.py

from filename import X
print X().myDict
Comments