Python ImportError:没有模块名称解析

时间:2018-02-02 15:28:57

标签: python python-2.7 error-handling module importerror

我一直得到同样的错误,我已经尝试了所有我知道如何修复的错误。我得到的错误是:ImportError: No module name parse有谁知道如何解决这个问题?我目前正在运行python 2.7.12。这是我的代码:

 # -*- coding: utf-8 -*-

import http.client, urllib.parse, json


# **********************************************
# *** Update or verify the following values. ***
# **********************************************

# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey =  "My_Access_Key"

# Verify the endpoint URI.  At this writing, only one endpoint is used for Bing
# search APIs.  In the future, regional endpoints may be available.  If you
# encounter unexpected authorization errors, double-check this value against
# the endpoint for your Bing Web search instance in your Azure dashboard.
host = "api.cognitive.microsoft.com"
path = "/bing/v7.0/search"

term = "Microsoft Cognitive Services"

def BingWebSearch(search):
    "Performs a Bing Web search and returns the results."

    headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
    conn = http.client.HTTPSConnection(host)
    query = urllib.parse.quote(search)
    conn.request("GET", path + "?q=" + query, headers=headers)
    response = conn.getresponse()
    headers = [k + ": " + v for (k, v) in response.getheaders()
                   if k.startswith("BingAPIs-") or k.startswith("X-MSEdge-")]
    return headers, response.read().decode("utf8")

if len(subscriptionKey) == 32:

    print('Searching the Web for: ', term)

    headers, result = BingWebSearch(term)
    print("\nRelevant HTTP Headers:\n")
    print("\n".join(headers))
    print("\nJSON Response:\n")
    print(json.dumps(json.loads(result), indent=4))

else:

    print("Invalid Bing Search API subscription key!")
    print("Please paste yours into the source code.")

1 个答案:

答案 0 :(得分:0)

就像snakecharmerb告诉你的那样,urllib.parse来自Python3。

我认为您应该使用urlparse作为urllib.parsehttplib作为http模块。

如果你已经在你的机器中安装了Python3(来自apt或类似),也许它没有将你的/usr/bin/python链接更改为更新的版本,但我很确定你应该有{已安装{1}}或python3命令。

如果要将python3.x命令替换为python,则必须创建指向python3.x文件夹的符号链接。你可以用:

/usr/bin

也许rm没有必要,但它并没有受到伤害。