Python 导入问题,找不到模块

标签 python

来 self 在 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

主.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'

最佳答案

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

关于Python 导入问题,找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25031055/

相关文章:

python - 转换元组列表,使 [(a,b,c)] 转换为 [(a,b),(a,c)]

python - "from __future__ import annotations"在 VSCode 中产生 "annotations is not defined"

python - SQLAlchemy、Alembic 和新实例

python - 您最喜欢在 django 中管理数据库迁移的解决方案是什么?

python - Python 中基于分隔符分割和组合文本

php - Python 无法打开文件

python - 尝试使用验证器在子类中设置父类(super class)字段

python - 使用 Python 获取目录(包括子目录)的最后修改日期?

python - 将单个面部位置四元组二转换为该四元组定义的矩形内包含的图像区域

python - 以有效的方式过滤2 numpy.ndarray中的相似图像