gpt4 book ai didi

python - Python 导入时出错

转载 作者:太空宇宙 更新时间:2023-11-03 19:12:39 25 4
gpt4 key购买 nike

我正处于学习阶段,导入有问题

我创建了一个名为 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

最佳答案

您正在从标准库导入 test 模块,而不是您自己的 test 模块。

为了使 Python 能够找到模块,必须通过 sys.path 列表中定义的路径来定位它们,例如:

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

您可以在 IDLE 中通过文件 -> 路径浏览器查看sys.path

关于python - Python 导入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com