Why does this python code give "TypeError: the JSON object must be str, not 'bytes'" on one machine but not another
Why does this python code give "TypeError: the JSON object must be str, not 'bytes'" on one machine but not another
我有一个 small public repo 具有以下 python 代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.request, urllib.parse, urllib.error
import json
from urllib.request import urlopen, Request
import os
def get_json_from_url(url):
config = json.loads(open(os.path.dirname(os.path.abspath(__file__))+'/config.json').read())
token = config['token']
request = Request(url)
request.add_header('Authorization', 'token %s' % token)
request.add_header('Accept', "application/vnd.github.inertia-preview+json" )
response = urlopen(request)
return json.loads(response.read())
def process_cards(pri,url, tag=""):
cards= get_json_from_url(url)
for card in cards:
payload=""
if card['note']:
payload="map project:"+ card['note']
else:
payload="Work on: "+ card['content_url']
print("({}) {} {}".format(pri,payload,tag))
def process_project_board(url,tag=""):
board= get_json_from_url(url)
columns_url= board["columns_url"]
columns= get_json_from_url(columns_url)
priorities=['*', 'A', 'B', 'C', 'D','E']
for x in columns:
process_cards(priorities.pop(0),x['cards_url'],tag)
if __name__ == "__main__":
process_project_board("https://api.github.com/projects/1613733","+EQT")
process_project_board("https://api.github.com/projects/1659667","+PersonalProjects")
当我 运行 使用
时,它在我的桌面上运行完美
python3 vision.py
但是当我在服务器上克隆它时,出现以下错误:
Traceback (most recent call last):
File "vision.py", line 37, in <module>
process_project_board("https://api.github.com/projects/1613733","+EQT")
File "vision.py", line 29, in process_project_board
board= get_json_from_url(url)
File "vision.py", line 15, in get_json_from_url
return json.loads(response.read())
File "/usr/lib/python3.4/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
导致行为差异的原因是什么?没有丢失库错误 - config.json 文件(我的 Github 令牌所在的位置)是相同的,而且我从未遇到类似的问题将东西移到服务器......
您必须在您的服务器上使用低于 3.6 的 Python 版本,并且 JSON 未按预期运行。升级您的 Python 版本或使用 virualenv,以便您可以在桌面和服务器上使用相同的代码
我有一个 small public repo 具有以下 python 代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.request, urllib.parse, urllib.error
import json
from urllib.request import urlopen, Request
import os
def get_json_from_url(url):
config = json.loads(open(os.path.dirname(os.path.abspath(__file__))+'/config.json').read())
token = config['token']
request = Request(url)
request.add_header('Authorization', 'token %s' % token)
request.add_header('Accept', "application/vnd.github.inertia-preview+json" )
response = urlopen(request)
return json.loads(response.read())
def process_cards(pri,url, tag=""):
cards= get_json_from_url(url)
for card in cards:
payload=""
if card['note']:
payload="map project:"+ card['note']
else:
payload="Work on: "+ card['content_url']
print("({}) {} {}".format(pri,payload,tag))
def process_project_board(url,tag=""):
board= get_json_from_url(url)
columns_url= board["columns_url"]
columns= get_json_from_url(columns_url)
priorities=['*', 'A', 'B', 'C', 'D','E']
for x in columns:
process_cards(priorities.pop(0),x['cards_url'],tag)
if __name__ == "__main__":
process_project_board("https://api.github.com/projects/1613733","+EQT")
process_project_board("https://api.github.com/projects/1659667","+PersonalProjects")
当我 运行 使用
时,它在我的桌面上运行完美python3 vision.py
但是当我在服务器上克隆它时,出现以下错误:
Traceback (most recent call last):
File "vision.py", line 37, in <module>
process_project_board("https://api.github.com/projects/1613733","+EQT")
File "vision.py", line 29, in process_project_board
board= get_json_from_url(url)
File "vision.py", line 15, in get_json_from_url
return json.loads(response.read())
File "/usr/lib/python3.4/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
导致行为差异的原因是什么?没有丢失库错误 - config.json 文件(我的 Github 令牌所在的位置)是相同的,而且我从未遇到类似的问题将东西移到服务器......
您必须在您的服务器上使用低于 3.6 的 Python 版本,并且 JSON 未按预期运行。升级您的 Python 版本或使用 virualenv,以便您可以在桌面和服务器上使用相同的代码