1

From all I've read at https://docs.python.org/2/tutorial/modules.html, the main.py code should work. I can't for the life of me figure out why it doesn't work.

Versions etc.

vagrant@vagrant-ubuntu-precise-64:/vagrant/monkey$ python --version
Python 2.7.3

Directory Structure

vagrant@vagrant-ubuntu-precise-64:/vagrant/monkey$ ll
total 6
drwxrwxrwx 1 vagrant vagrant 0 Jul 30 07:18 ./
drwxrwxrwx 1 vagrant vagrant 4096 Jul 30 07:04 ../
-rwxrwxrwx 1 vagrant vagrant 76 Jul 30 07:09 abc.py*
-rwxrwxrwx 1 vagrant vagrant 76 Jul 30 07:10 bcd.py*
-rwxrwxrwx 1 vagrant vagrant 43 Jul 30 06:56 main.py*

abc.py

# abc module
def bar():
 print 'abc.bar called.'
 return None

bcd.py

# bcd module
def foo():
 print 'bcd.foo called.'
 return None

main.py

import abc
import bcd
abc.bar()
bcd.foo()

Error

vagrant@vagrant-ubuntu-precise-64:/vagrant/monkey$ python main.py
Traceback (most recent call last):
 File "main.py", line 5, in <module>
 abc.bar()
AttributeError: 'module' object has no attribute 'bar'
asked Jul 30, 2014 at 7:28
1
  • 2
    What is the error that you are getting? Commented Jul 30, 2014 at 7:32

2 Answers 2

6

abc is a builtin Python module. Rename your abc.py to something else.

answered Jul 30, 2014 at 7:37
Sign up to request clarification or add additional context in comments.

Comments

1

nothing wrong with your code. But abc is a builtin module. Try to do a locate abc.py, and you should see lines like:

/usr/lib/python2.6/abc.py
/usr/lib/python2.6/abc.pyc

Either try to rename your module, or search SO for things like Import Local module over global python

answered Jul 30, 2014 at 7:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.