Почему это не работает, и как это исправить?
Код: Выделить всё
import requests
res = requests.get('http://www.google.ca')
print(res)
< /code>
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in
import requests
File "/Users/me/dev/rough/requests.py", line 3, in
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
Using from-import of a specific name results in an ImportError:
Код: Выделить всё
from requests import get
res = get('http://www.google.ca')
print(res)
< /code>
Traceback (most recent call last):
File "requests.py", line 1, in
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in
from requests import get
ImportError: cannot import name 'get'
br/> Importerror :
Код: Выделить всё
from requests.auth import AuthBase
< /code>
Traceback (most recent call last):
File "requests.py", line 1, in
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package
from requests import *
res = get('http://www.google.ca')
print(res)
< /code>
Traceback (most recent call last):
File "requests.py", line 1, in
from requests import *
File "/Users/me/dev/rough/requests.py", line 3, in
res = get('http://www.google.ca')
NameError: name 'get' is not defined
< /code>
Из Python 3.13 Когда это приводит к ошибкам, теперь мы отображаем более полезное сообщение об ошибке
: < /p>
$ python random.py Traceback (most recent call last): File
"/home/me/random.py", line 1, in
import random File "/home/me/random.py", line 3, in
print(random.randint(5))
^^^^^^^^^^^^^^ AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/random.py' since it
has the same name as the standard library module named 'random' and
prevents importing that standard library module)
< /code>
Аналогично, если сценарий
имеет то же имя, что и сторонний модуль, который он пытается
import, и это приводит к ошибкам, мы также отображаем более полезное сообщение об ошибке
: < /p>
$ python numpy.py Traceback (most recent call last): File
"/home/me/numpy.py", line 1, in
import numpy as np File "/home/me/numpy.py", line 3, in
np.array([1, 2, 3])
^^^^^^^^ AttributeError: module 'numpy' has no attribute 'array' (consider renaming '/home/me/numpy.py' if it has the same name as a
library you intended to import)
< /code>
< /blockquote>
Для тех случаев, когда вы называете свой модуль, так же, как существующий на назначение < /strong> < /em> и хотите справиться с этой ситуацией, посмотреть, как я могу импортировать из стандартной библиотеки, когда у моего проекта есть модуль с тем же именем? (Как я могу контролировать, где Python ищет модули?)
Подробнее здесь: https://stackoverflow.com/questions/362 ... -attribute