【问题标题】:Error: "python setup.py egg_info" failed with error code 1错误:“python setup.py egg_info”失败,错误代码为 1
【发布时间】:2018-11-25 17:21:08
【问题描述】:

尝试安装 unirest 时命令“python setup.py egg_info”失败,错误代码 1

运行命令pip install unirest 以在我的views.py 中使用unirest 后出现此错误。 我正在尝试在运行此命令的项目中使用汽油价格的 api

(geo) ABHISHEKs-MacBook-Air:map abksharma$ pip install unirest
Collecting unirest
 Using cached 
https://files.pythonhosted.org/packages/92/da/2149cbd7a8c78f8b76b377379c1bda64ec36cc13315d55f6f7de6d094ac5/Unirest-1.1.7.tar.gz
Collecting poster>=0.8.1 (from unirest)
Using cached
https://files.pythonhosted.org/packages/9f/dc/0683a458d21c3d561ab2f71b4fcdd812bf04e55c54e560b0854cea95610e/poster-0.8.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/var/folders/84/0bcm7b0s6kx3496xf5ply6wh0000gn/T/pip-install-vf9ffv_s/poster/setup.py", line 2, in <module>
    import poster
  File "/private/var/folders/84/0bcm7b0s6kx3496xf5ply6wh0000gn/T/pip-install-vf9ffv_s/poster/poster/__init__.py", line 29, in <module>
    import poster.streaminghttp
  File "/private/var/folders/84/0bcm7b0s6kx3496xf5ply6wh0000gn/T/pip-install-vf9ffv_s/poster/poster/streaminghttp.py", line 61
    print "send:", repr(value)
                ^
SyntaxError: invalid syntax

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in 
/private/var/folders/84/0bcm7b0s6kx3496xf5ply6wh0000gn/T/pip- 
install-vf9ffv_s/poster/

views.py

from django.shortcuts import render
import requests
import unirest


def petrol(request):

    response = unirest.post("https://fuelprice.p.mashape.com/",
    headers={
    "X-Mashape-Key": "eqPyWAd3Wrmsh52b6fvG3AQ5T2ygp1KZhDfjsng702Sd7DmWN7",
    "Content-Type": "application/json",
    "Accept": "application/json"
  },
    params=("{\"fuel\":\"p\",\"state\":\"dl\"}")
)

    price = response.json()
    return render(request, 'petrol/petrol.html', {'price':price})

url.py

from django.urls import path
from .import views


urlpatterns = [
      path('', views.petrol, name='petrol')
]

【问题讨论】:

    标签: python django django-models django-rest-framework


    【解决方案1】:

    在 python3 中不支持 Unirest(至少在 python 3.7 中不支持)。而不是这个,考虑使用requests。像这样使用它:

    response = requests.post("https://fuelprice.p.mashape.com/",
        headers={
        "X-Mashape-Key": "eqPyWAd3Wrmsh52b6fvG3AQ5T2ygp1KZhDfjsng702Sd7DmWN7",
        "Content-Type": "application/json",
        "Accept": "application/json"
      },
        data={'fuel': 'p', 'state': 'dl'}
    )
    
    price = response.json()
    

    【讨论】:

    • 运行服务器后,我在 /Expecting value: line 1 column 1 (char 0) 处收到了 JSONDecodeError 错误
    • 您可以尝试使用response.textresponse.status_code 来查看响应正文和状态码。您得到的响应可能不在 Json 序列化中。请参阅此处:docs.python-requests.org/en/master 了解更多使用 requests
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2016-12-18
    • 2017-02-20
    相关资源
    最近更新 更多