gpt4 book ai didi

python - 奇怪的 Python 导入错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:10 24 4
gpt4 key购买 nike

我遇到了这个非常奇怪的错误,我在从 AWS EMR 的主目录启动的 Python shell 中遇到导入错误,而当我 cd 进入时相同的包导入正常另一个目录并在那里启动 Python shell。

EMR 安装了 Python 2.7。请让我知道我可以提供哪些额外信息。这个问题快把我逼疯了!

$ python
Python 2.7.16 (default, Mar 18 2019, 18:38:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import smtplib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/smtplib.py", line 46, in <module>
import email.utils
File "email.py", line 2, in <module>
from email.mime.multipart import MIMEMultipart
ImportError: No module named mime.multipart
>>> os.path.realpath('./')
'/home/abc123'
>>>
$ mkdir temp
$ cd temp
[temp]$ python
Python 2.7.16 (default, Mar 18 2019, 18:38:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import smtplib
>>> os.path.realpath('./')
'/home/abc12/temp'

最佳答案

第一个(主)目录中是否有名为 email.py 的文件?或者名为 email 的文件夹?如果是,那么由于 python 的查找机制(应用根目录优先于其他导入),这可能会影响导入行为。

来自docs : (强调我的)

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

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

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