我正在将PyGithub与来自Github的访问令牌一起使用,我得到了here来使用click创建命令行工具。我的代码如下:
from github import Github
def readEnvironment():
'This function reads in the environment variables form a file if needed'
if os.path.isfile('.env') is True:
with open('.env', 'r') as file:
for line in file:
if line.startswith('#'):
continue
# Remove leading `export `
# then, split name / value pair
key, value = line.split('=', 1)
os.environ[key] = value
@click.group()
def cli():
pass
@click.command()
@click.option('--token', default=None, help='The Github access token', prompt=False)
def github(token):
if None == token and (None != os.environ['GITHUB_ACCESS_TOKEN'] or '' != os.environ['GITHUB_ACCESS_TOKEN']):
token = os.environ['GITHUB_ACCESS_TOKEN']
client = Github(token)
print(client)
# Then play with your Github objects:
for repo in client.get_user().get_repos():
print(repo.name)
cli.add_command(github)
if __name__ == '__main__':
readEnvironment()
cli()
但出现以下错误
回溯(最近通话最近): 在文件“ farley.py”的第142行中 cli() 调用中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py”,第764行 返回self.main(* args,** kwargs) 主文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py”,第717行 rv = self.invoke(ctx) 调用中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py”,行1137 返回_process_result(sub_ctx.command.invoke(sub_ctx)) 调用中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py”,行956 返回ctx.invoke(self.callback,** ctx.params) 调用中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py”,行555 返回回调(* args,** kwargs) 在github上的文件“ farley.py”,第73行 用户= client.get_user(令牌) get_user中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/MainClass.py”,第264行 “ GET”,“ / users /” +登录 在requestJsonAndCheck中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py”,第322行 动词,URL,参数,标题,输入,自我.__ customConnection(URL) 在requestJson中,文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py”,第413行 返回self .__ requestEncode(cnx,动词,url,参数,标头,输入,编码) __requestEncode中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py”,第475行 cnx,动词,URL,requestHeaders,encoded_input __requestRaw中的第501行的“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py”文件 响应= cnx.getresponse() 在getresponse中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py”,行119 allow_redirects = False, 在get中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py”,行546 返回self.request('GET',url,** kwargs) 请求中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py”,行533 resp = self.send(准备,** send_kwargs) 发送文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py”,行646 r = adapter.send(request,** kwargs) 发送中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/adapters.py”,第449行 超时=超时 urlopen中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py”,第672行 分割=块 _make_request中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py”,行387 conn.request(方法,网址,** httplib_request_kw) 请求中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第1229行 self._send_request(方法,URL,正文,标头,encode_chunked) _send_request中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,行1270 self.putheader(hdr,value) 在putheader中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第1207行 引发ValueError('无效的标头值%r'%(values [i],)) ValueError:无效的标头值b'token
omitted
\ n'
其中omitted
是我从Github获得的访问令牌。正确的令牌出现在错误中。这是PyGithub文档中给出的基本示例,所以我不确定我缺少什么。当我在click方法之外运行代码时,它可以正常工作。我正在使用Python 3.7.2和PyGithub 1.45,然后单击7.0。
答案 0 :(得分:1)
事实证明,错误消息中的\n
是关键。我在读取环境变量时没有删除换行符。我将功能固定为
def readEnvironment():
'This function reads in the environment variables form a file if needed'
if os.path.isfile('.env') is True:
with open('.env', 'r') as file:
for line in file:
if line.startswith('#'):
continue
# Remove leading `export `
# then, split name / value pair
key, value = line.split('=', 1)
os.environ[key] = value[:-1]
然后一切正常。
答案 1 :(得分:0)
您可能想使用Click的内置envvar支持,并可能转换为字符串?
@click.command()
@click.option('--token', required=True, help='The Github access token', envvar='GITHUB_ACCESS_TOKEN')
def github(token):
client = Github(str(token))
print(client)
for repo in client.get_user().get_repos():
print(repo.name)