我想在执行一些操作后启动一个 python 解释器,以方便用户使用。到目前为止,我已经有了我想要的骨架。
class ApiManager(object):
def __init__(self, username, password, version='v1'):
self.base_url = 'http://localhost:3000/api/' + version + '/{endpoint}'
login = requests.post(self.base_url.format(endpoint='login'), {
'user': username, 'password': password
}).json()['data']
self.headers = {
'X-Auth-Token': login['authToken'],
'X-User-Id': login['userId']
}
def get(self, endpoint):
" example method "
return requests.get(self.base_url.format(endpoint))
if __name__ == '__main__':
Api = ApiManager('user@user.org', 'password')
code.interact(banner="use Api.<Crud Command> to do a method")
但是,我想知道的是,如何将这个本地文件导入我的 code.interact
?
我试过 local=Api
但它不起作用,说 TypeError: exec() arg 2 must be a dict, not ApiInterpreter
。我试过使用 Api.__dict__()
但这也不起作用。我还认为可以将 code.InteractiveConsole
最佳答案
尝试:
code.interact(banner="use Api.<Crud Command> to do a method", local={"Api": Api})
关于python - 启动一个包含本地对象的 python 解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31326709/