1

我正处于学习阶段,我有一个问题import

我创建了一个名为 test 的模块,该文件夹包含我的 test.py、setup.py 和 python.exe,在运行 sdist 并安装后,我在 build 和 dist 文件夹中获得了 MANIFEST 文件、build、lib。

现在,我尝试在 IDLE 中使用我的模块并生成以下内容

>>> import test
>>> movies = ["1","2", ["3", "4", ["5", "6"]]]
>>> test.lol ()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    test.lol ()
AttributeError: 'module' object has no attribute 'lol'

这是我得到的错误。什么地方出了错?出了什么问题?由于我是新手,我自己找不到解决方案。

这是我的模块:

def lol():
    for each_item in movies:
       if isinstance(each_item, list):
           for nest in each_item:
               print(nest)
       else:
           print(each_item)

我使用 Windows 7 机器和 Python 3.2

4

1 回答 1

1

You are importing the test module from the standard library instead of the test module of your own.

For Python to be able to find the modules, they have to be located by the paths defined in sys.path list, e.g.:

import sys

# insert the path to the beginning of the list:
sys.path.insert(0, '/path/to/my/test/module/directory')

# now Python importing system will search the directory defined above 
# before scanning the standard library dirs. 
import test 

You can check the sys.path in IDLE via File -> Path Browser.

于 2012-09-18T10:48:40.980 回答