ModuleNotFoundError: No module named 'folium.plugins'; 'folium' is not a package

ModuleNotFoundError: No module named 'folium.plugins'; 'folium' is not a package

我知道这个问题已经被问过好几次了,但到目前为止 none 的解决方案帮助了我。

每当我尝试 运行 我的 python 脚本时,我都会收到以下错误:

M-MBP:folder m$ python3.7 folium.py
Traceback (most recent call last):
  File "folium.py", line 3, in <module>
    import folium
  File "/Users/m/folder/folium.py", line 4, in <module>
    from folium.plugins import MarkerCluster
ModuleNotFoundError: No module named 'folium.plugins'; 'folium' is not a package

备注:

None 成功了。有什么建议吗?

谢谢!

您的脚本与您想要的包同名。它正在尝试导入自身,但其中没有 plugins。将您的脚本命名为 folium.py 以外的名称,我相信您的问题将会消失。

展示:

arts@support:~ 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.DEBUG
10
>>>
arts@support:~ 0$ cd tmp
arts@support:~/tmp 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/arts/tmp/logging.py", line 5, in <module>
    print(logging.DEBUG)
AttributeError: 'module' object has no attribute 'DEBUG'
>>>
arts@support:~/tmp 0$ cat logging.py
import sys
import logging    # Imports itself
import os

print(logging.DEBUG)

原因是,你需要看看sys.path

arts@support:~/tmp 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
>>> del sys.path[0]
>>> sys.path
['/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
>>> import logging
>>> logging.DEBUG
10
>>>
arts@support:~/tmp 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
>>> import logging
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/arts/tmp/logging.py", line 5, in <module>
    print(logging.DEBUG)
AttributeError: 'module' object has no attribute 'DEBUG'
>>>

在第二块中,您会看到我删除了列表的第一块。这将从 python 的导入路径中删除您的当前目录,因此它现在忽略了我的 logging.py 文件并成功导入了真正的 logging 模块。