Python3.9 出现“TypeError: ‘module’ object is not callable”

时间:2021-02-14 01:00:37

标签: python python-requests python-3.9

当我从 Python 终端使用以下代码时,它按预期工作正常。

Jigneshs-MBP:Desktop jpolara$ python3.9
Python 3.9.1 (default, Feb  3 2021, 07:38:02)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> response = requests.get("http://api.open-notify.org/iss-now.json")
>>> print(response.json())
{'message': 'success', 'iss_position': {'longitude': '-54.8828', 'latitude': '-43.3500'}, 'timestamp': 1613263412}
>>>

但是如果我在 Python 脚本中添加相同的代码并执行它,我会得到如下错误:

Jigneshs-MBP:Desktop jpolara$ python3.9 python/get_method.py
Traceback (most recent call last):
  File "/Users/jpolara/Desktop/python/get_method.py", line 2, in <module>
    import requests
  File "/usr/local/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/local/lib/python3.9/site-packages/urllib3/__init__.py", line 11, in <module>
    from . import exceptions
  File "/usr/local/lib/python3.9/site-packages/urllib3/exceptions.py", line 3, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "/usr/local/lib/python3.9/site-packages/urllib3/packages/__init__.py", line 3, in <module>
    from . import ssl_match_hostname
  File "/usr/local/lib/python3.9/site-packages/urllib3/packages/ssl_match_hostname/__init__.py", line 9, in <module>
    from ssl import CertificateError, match_hostname
  File "/usr/local/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 255, in <module>
    from socket import socket, SOCK_STREAM, create_connection
  File "/Users/jpolara/Desktop/python/socket.py", line 5, in <module>
    s = socket.socket()         # Create a socket object
TypeError: 'module' object is not callable

这是我的脚本get_method.py

Jigneshs-MBP:Desktop jpolara$ cat python/get_method.py

import requests
response = requests.get("http://api.open-notify.org/iss-now.json")
print(response.json())

这就是我安装请求模块的方式。

python3.9 -m pip install requests

我是 Python 的初学者,对此知之甚少。我已经尝试了 Internet 上的多种解决方案来解决该问题,但没有任何帮助。您是否在 Python3.9 中看到过类似的问题或知道如何解决?

1 个答案:

答案 0 :(得分:3)

不要将您的文件命名为 socket.py,Python 正在导入它而不是 stdlib 套接字模块。