Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python calling a module

Tags:

python

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

like image 639
user1050619 Avatar asked Aug 25 '26 19:08

user1050619


1 Answers

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
like image 177
Jakob Bowyer Avatar answered Aug 28 '26 09:08

Jakob Bowyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!