我在脚本中使用相同的python代码,并在cli中直接使用。虽然在cli中代码没有错误,但在脚本中它给了我一个错误。
AttributeError: module 'jsonpath' has no attribute 'jsonpath'代码:
import os
import click
import subprocess
import urllib.request
import json
import jsonpath
@cli.command()
@click.argument('search', required=False, nargs=-1)
def search(search):
for srch in search:
packs = urllib.request.urlopen("https://aur.archlinux.org//rpc/?v=5&type=search&arg="+srch).read()
somejson = json.loads(packs)
match = jsonpath.jsonpath(somejson, '$.results[*].Name,Version,Description')
print(match)对于cli和脚本,我使用相同的virtualenv。
代码cli:
(venv) [user@laptop pyapp]$ python
Python 3.6.1 (default, Mar 27 2017, 00:27:06)
[GCC 6.3.1 20170306] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jsonpath
>>> import json
>>> import urllib.request
>>> jsontext = urllib.request.urlopen("https://aur.archlinux.org//rpc/?v=5&type=search&arg=teamviewer").read()
>>> somejson = json.loads(jsontext)
>>> match = jsonpath.jsonpath(somejson, '$.results[*].Name,Version,Description')
>>> print(match)
['teamviewer-quicksupport-beta', '11.0.52520-1', 'Teamviewer Quicksupport - All-In-One Software for Remote Support and Online Meetings - beta version', 'teamviewer10', '10.0.46203-1.1', 'All-In-One Software for Remote Support and Online Meetings', 'remmina-plugin-teamviewer', '1.2.3.0-1', 'A protocol plugin for Remmina to launch a TeamViewer connection.', 'teamviewer8', '8.0.20931-1', 'All-In-One Software for Remote Support and Online Meetings', 'teamviewer-quicksupport', '11.0.57095-2', 'Teamviewer Quicksupport - All-In-One Software for Remote Support and Online Meetings', 'teamviewer-openrc', '1.0-2', 'OpenRC scripts for teamviewer.', 'teamviewer-beta', '12.0.69753-1', 'All-In-One Software for Remote Support and Online Meetings - beta version', 'teamviewer11', '11.0.67687-1', 'All-in-one software for remote support and online meetings', 'teamviewer9', '9.0.32150-1', 'All-In-One Software for Remote Support and Online Meetings', 'teamviewer', '12.0.76279-6', 'All-In-One Software for Remote Support and Online Meetings']我不明白代码出了什么问题。完整的代码是here
https://stackoverflow.com/questions/44366048
复制相似问题