【问题标题】:Python import issues, can't find modulePython导入问题,找不到模块
【发布时间】:2014-07-30 07:28:38
【问题描述】:

根据我在https://docs.python.org/2/tutorial/modules.html 阅读的所有内容,main.py 代码应该可以工作。我一辈子都想不通为什么它不起作用。

版本等

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

目录结构

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

ma​​in.py

import abc
import bcd


abc.bar()
bcd.foo()

错误

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'

【问题讨论】:

  • 您遇到的错误是什么?
  • @JustinUnwin 添加了错误消息。感谢您指出。

标签: python


【解决方案1】:

abc is a builtin Python module。将您的 abc.py 重命名为其他名称。

【讨论】:

  • 我的错...非常感谢。
【解决方案2】:

您的代码没有问题。但是 abc 是一个内置模块。尝试执行locate abc.py,您应该会看到如下行:

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

要么尝试重命名你的模块,要么搜索诸如Import Local module over global python之类的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多