ZQH
ZQH

Reputation: 13

Python import issues, can't find module

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'

Upvotes: 1

Views: 174

Answers (2)

fredtantini
fredtantini

Reputation: 16556

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

Upvotes: 1

user1907906
user1907906

Reputation:

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

Upvotes: 6

Related Questions