ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用Azure SDK时,无法导入名称会话

2019-10-09 16:56:19  阅读:154  来源: 互联网

标签:importerror python azure python-2-7


我正在使用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文件或目录包含文件__init__.py相同名称的软件包请求引起的.

因为Python从路径sys.path导入包,第一个路径是”(当前目录).

因此,如果您创建python文件request.py或创建dir请求包括文件__init__.py,则Python首先会将其作为软件包请求导入.然后,从请求导入会话运行代码将导致错误ImportError:无法导入名称Session.

请依次检查路径sys.path中的当前目录或目录,并删除名为request.py的文件或名为request的目录.

标签:importerror,python,azure,python-2-7
来源: https://codeday.me/bug/20191009/1880581.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有