【发布时间】:2021-03-12 10:24:34
【问题描述】:
我编写了自己的 cmd 工具,并创建了一个 click cmd 来触发我的上传功能。 在这个上传函数中,我做了一个人工部署调用。如果我用 pytest 测试这个函数可以工作,如果我通过 cmd 行测试它,它就找不到我的环境变量。
我已将这些添加到我的 .bashrc 和 pycharm 中作为 env var。
知道为什么它不能在命令行上运行。它说我的环境变量是无
我的主线:
def configure_click(entrypoint: click.Group = cli):
entrypoint.add_command(deploy_log)
return entrypoint
def main():
entrypoint = configure_click(entrypoint=cli)
entrypoint()
@click.command(context_settings=CLICK_CONTEXT_SETTINGS)
@click.option(
'-u', '--storage-url',
required=True,
help='url of the artifactory repository',
@click.option(
'-i', '--input_directory',
required=True,
help='Directory of the updated log',
)
def deploy_log(storage_url,input_directory):
test_log = Log()
test_log.upload_log(storage_url, input_directory)
日志类:
def upload_log(self, artifactory_path, input_dir):
TARGET_PATH = ('{}/test/log.zip'.format(artifactory_path))
PATH_TO_FILE = 'log.zip'
ARTIFACTORY_USER = os.getenv('ARTIFACTORY_USER')
ARTIFACTORY_TOKEN= os.getenv('ARTIFACTORY_TOKEN')
deploy_cmd = "curl --fail -u"+ARTIFACTORY_USER+":"+ARTIFACTORY_TOKEN+ " -T "+PATH_TO_FILE+" "+ TARGET_PATH
os.system(deploy_cmd)
例外:
TypeError: must be str, not NoneType
【问题讨论】:
-
如果您忘记设置或
export这些变量,就会发生这种情况。我们看不到您从中调用此脚本的环境,但这似乎是显而易见的结论。 -
如果这不能解决问题,请减少到 minimal reproducible example 以便我们可以准确地看到您在做什么,理想情况下使用仅演示问题并且不尝试做任何其他事情的代码. (在这里笨拙地使用
os.system而不是subprocess.run是一种明显的干扰。)
标签: python linux environment-variables