V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
cpluspython
V2EX  ›  Python

遇到 Python3 问题, import requests 库出现 AttributeError: '_SixMetaPathImporter' object...

  •  
  •   cpluspython · 2020-04-21 01:45:09 +08:00 · 2628 次点击
    这是一个创建于 1712 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码内容如下

    # -*- coding:utf-8 -*-
    #! /usr/bin/env python3
    import requests 
    #引入 requests 库
    
    

    运行代码后

    出现错误提示

    AttributeError: '_SixMetaPathImporter' object has no attribute 'find_spec',
    TypeError: 'int' object is not callable

    完整错误提示如下

    Traceback (most recent call last):
      File "<frozen importlib._bootstrap>", line 900, in _find_spec
    AttributeError: '_SixMetaPathImporter' object has no attribute 'find_spec'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/noogler/Documents/python_files_sublimetext3/practice/draft7.py", line 3, in <module>
        import requests 
      File "/usr/local/lib/python3.7/site-packages/requests/__init__.py", line 43, in <module>
        import urllib3
      File "/usr/local/lib/python3.7/site-packages/urllib3/__init__.py", line 7, in <module>
        from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
      File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 11, in <module>
        from .exceptions import (
      File "/usr/local/lib/python3.7/site-packages/urllib3/exceptions.py", line 2, in <module>
        from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
      File "<frozen importlib._bootstrap>", line 983, in _find_and_load
      File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 902, in _find_spec
      File "<frozen importlib._bootstrap>", line 879, in _find_spec_legacy
      File "<frozen importlib._bootstrap>", line 449, in spec_from_loader
      File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 212, in is_package
        return hasattr(self.__get_module(fullname), "__path__")
      File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 116, in __getattr__
        _module = self._resolve()
      File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 113, in _resolve
        return _import_module(self.mod)
      File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
        __import__(name)
      File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 71, in <module>
        import email.parser
      File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/parser.py", line 12, in <module>
        from email.feedparser import FeedParser, BytesFeedParser
      File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/feedparser.py", line 27, in <module>
        from email._policybase import compat32
      File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/_policybase.py", line 9, in <module>
        from email.utils import _has_surrogates
      File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/utils.py", line 33, in <module>
        from email._parseaddr import quote
      File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/_parseaddr.py", line 16, in <module>
        import time, calendar
      File "/Users/noogler/Documents/python_files_sublimetext3/practice/calendar.py", line 9, in <module>
        cal = calendar.month(year,month)
    TypeError: 'int' object is not callable
    [Finished in 0.1s with exit code 1]
    [cmd: ['/usr/local/bin/python3', '-u', '/Users/noogler/Documents/python_files_sublimetext3/practice/draft7.py']]
    [dir: /Users/noogler/Documents/python_files_sublimetext3/practice]
    [path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/aria2/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands]
    

    问题补充

    import pygame 库是正常的

    pygame 1.9.6
    Hello from the pygame community. https://www.pygame.org/contribute.html
    [Finished in 0.2s]
    

    初学 python 不久,不确定还需要补充哪些信息帮忙定位,我会继续补充 自己 Google 了 2 天了还是没解决,希望有好心大佬帮忙,感激不尽 😂

    6 条回复    2020-04-21 11:59:19 +08:00
    leishi1313
        1
    leishi1313  
       2020-04-21 05:59:23 +08:00
    把你的 ‘calendar.py’ 改个名吧
    EricCartmann
        2
    EricCartmann  
       2020-04-21 08:25:09 +08:00 via iPhone
    自己写的 Python 文件名出问题了,Python 导入的时候最先找的是当前工程下的文件,所以在导 calendar 这个自带模块的时候找到你自己写的这个文件了。。以后最好不要用这样的英文名字,建议用拼音(狗头)
    bany
        3
    bany  
       2020-04-21 09:22:53 +08:00
    从 log 上看```"/Users/noogler/Documents/python_files_sublimetext3/practice/calendar.py", line 9```第 9 行报错了,原因是你这个文件中有个 month 变量,刚好你的文件名与导入的包名是一样的(不建议这么取名),发生了冲突,在调用的时候,Python 查找的方式遵循`legb`: local->enclosing(闭包)->global->build-in 。最简单的改法就是把文件改了
    cpluspython
        4
    cpluspython  
    OP
       2020-04-21 11:57:23 +08:00
    @leishi1313 可以了 感谢!
    cpluspython
        5
    cpluspython  
    OP
       2020-04-21 11:58:04 +08:00
    @EricCartmann 明白了 😂 感谢!
    cpluspython
        6
    cpluspython  
    OP
       2020-04-21 11:59:19 +08:00
    @bany 学习了学习了 我再好好整理下 感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2573 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 15:26 · PVG 23:26 · LAX 07:26 · JFK 10:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.