Reputation: 20856
Im trying to call module but some reason its giving me error. The data.py contains a list of items and in the main.py Im trying to iterate and print over the items.but I get the below error.
Error
Import error: No module named Basics
Both data.py & main.py are located in c:/python27/basics/
data.py
bob={'name':'bobs mith','age':42,'salary':5000,'job':'software'}
sue={'name':'sue more','age':30,'salary':3000,'job':'hardware'}
people=[bob,sue]
main.py
from Basics import data
if __name__ == '__main__':
for key in people:
print(key, '=>\n ', people[key])
If I just give import data, then I get the below error
Name error:name 'people' is not defined.
Update:
New code:
from Basics import data
if __name__ == '__main__':
for key in data.people:
print(key, '=>\n ', data.people[key])
TypeError:list indices must be integers, not dict
Upvotes: 3
Views: 1580
Reputation: 10389
For the second part, the "people" object is a list containing two dictionaries. So you want to do this:
for person in people:
for key in person:
print(key, '=>\n ', person[key])
Upvotes: 1
Reputation: 54079
Did you make an __init__.py
in c:/python27/basics/
?
Also it is probably good practice to make the case of the import Basics
match the case of the directory basics
. It doesn't matter on windows I think, but it certainly will under unix.
Upvotes: 1
Reputation: 34688
You will need __init__.py
in your Basics directory
And
you will need to have that directory in your PYTHON_PATH
or sys.path
To use people you need to do either of these.
from Basics.data import people
Or
from Basics import data
print data.people
Upvotes: 5