ImportError: cannot import name Session, when using Azure SDK
ImportError: cannot import name Session, when using Azure SDK
我正在 运行使用 Azure SDK 编写代码。
首先,我通过pip install azure
下载了sdk。然后我向 运行 写入以下 python 代码。
import requests
import os
# make sure you configure these three variables correctly before you try to run the code
AZURE_ENDPOINT_URL='https://login.microsoftonline.com/xxxxxx-xx-155715822801/oauth2/token'
AZURE_APP_ID='6dxxxxx8-c4af-4522xxx6-5a8f8155a616'
AZURE_APP_SECRET='password'
def get_token_from_client_credentials(endpoint, client_id, client_secret):
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'resource': 'https://management.core.windows.net/',
}
response = requests.post(endpoint, data=payload).json()
return response['access_token']
# test
if __name__ == '__main__':
auth_token = get_token_from_client_credentials(endpoint=AZURE_ENDPOINT_URL,
client_id=AZURE_APP_ID,
client_secret=AZURE_APP_SECRET)
print auth_token
运行编码后,得到如下信息。
File "D:\Python27\lib\site-packages\requests\__init__.py", line 58, in <module>
from . import utils
File "D:\Python27\lib\site-packages\requests\utils.py", line 26, in <module>
from .compat import parse_http_list as _parse_list_header
File "D:\Python27\lib\site-packages\requests\compat.py", line 39, in <module>
import cookielib
File "D:\Python27\lib\cookielib.py", line 32, in <module>
import re, urlparse, copy, time, urllib
File "C:\Users\yulei\Desktop\copy.py", line 8, in <module>
import azure.mgmt.compute
File "D:\Python27\lib\site-packages\azure\mgmt\compute\__init__.py", line 19, in <module>
from .computemanagement import *
File "D:\Python27\lib\site-packages\azure\mgmt\compute\computemanagement.py", line 23, in <module>
from requests import Session, Request
ImportError: cannot import name Session
问题似乎是由与 python 文件同名的软件包 requests
引起的,或者是当前目录中包含文件 __init__.py
的目录。
因为Python从路径sys.path
开始依次导入包,第一个路径是''
(当前目录)。
因此,如果您创建 python 文件 requests.py
或创建目录 requests
包含文件 __init__.py
,Python 首先将其作为包导入requests
。然后,运行 代码 from requests import Session
将导致错误 ImportError: cannot import name Session
。
请依次检查当前目录或路径sys.path
中的目录,并删除名为requests.py
的文件或名为requests
的目录。
我最近使用 sudo (sudo pip install --upgrade requests
) 升级了我的 requests
库,然后令我惊讶的是我不能在没有 sudo 的情况下调用代码。
由于权限问题,它找不到刚刚升级的文件!
我正在 运行使用 Azure SDK 编写代码。
首先,我通过pip install azure
下载了sdk。然后我向 运行 写入以下 python 代码。
import requests
import os
# make sure you configure these three variables correctly before you try to run the code
AZURE_ENDPOINT_URL='https://login.microsoftonline.com/xxxxxx-xx-155715822801/oauth2/token'
AZURE_APP_ID='6dxxxxx8-c4af-4522xxx6-5a8f8155a616'
AZURE_APP_SECRET='password'
def get_token_from_client_credentials(endpoint, client_id, client_secret):
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'resource': 'https://management.core.windows.net/',
}
response = requests.post(endpoint, data=payload).json()
return response['access_token']
# test
if __name__ == '__main__':
auth_token = get_token_from_client_credentials(endpoint=AZURE_ENDPOINT_URL,
client_id=AZURE_APP_ID,
client_secret=AZURE_APP_SECRET)
print auth_token
运行编码后,得到如下信息。
File "D:\Python27\lib\site-packages\requests\__init__.py", line 58, in <module>
from . import utils
File "D:\Python27\lib\site-packages\requests\utils.py", line 26, in <module>
from .compat import parse_http_list as _parse_list_header
File "D:\Python27\lib\site-packages\requests\compat.py", line 39, in <module>
import cookielib
File "D:\Python27\lib\cookielib.py", line 32, in <module>
import re, urlparse, copy, time, urllib
File "C:\Users\yulei\Desktop\copy.py", line 8, in <module>
import azure.mgmt.compute
File "D:\Python27\lib\site-packages\azure\mgmt\compute\__init__.py", line 19, in <module>
from .computemanagement import *
File "D:\Python27\lib\site-packages\azure\mgmt\compute\computemanagement.py", line 23, in <module>
from requests import Session, Request
ImportError: cannot import name Session
问题似乎是由与 python 文件同名的软件包 requests
引起的,或者是当前目录中包含文件 __init__.py
的目录。
因为Python从路径sys.path
开始依次导入包,第一个路径是''
(当前目录)。
因此,如果您创建 python 文件 requests.py
或创建目录 requests
包含文件 __init__.py
,Python 首先将其作为包导入requests
。然后,运行 代码 from requests import Session
将导致错误 ImportError: cannot import name Session
。
请依次检查当前目录或路径sys.path
中的目录,并删除名为requests.py
的文件或名为requests
的目录。
我最近使用 sudo (sudo pip install --upgrade requests
) 升级了我的 requests
库,然后令我惊讶的是我不能在没有 sudo 的情况下调用代码。
由于权限问题,它找不到刚刚升级的文件!