繁体   English   中英

Python导入问题,找不到模块

[英]Python import issues, can't find module

从我在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

main.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是内置的Python模块 将您的abc.py重命名为其他名称。

您的代码没有错。 但是abc是一个内置模块。 尝试执行一次locate abc.py ,您应该看到类似以下的行:

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

要么尝试重命名模块,要么在SO中搜索诸如通过全局python导入本地模块之类的东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM