【问题标题】:Having acess to my environment variables PYTHON, LINUX, click command, which uses a curl command inside [duplicate]可以访问我的环境变量 PYTHON、LINUX、click 命令,它在 [重复] 中使用 curl 命令
【发布时间】: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


【解决方案1】:

这解决了我的问题:

当您在 ~/.bashrc 中定义变量时,该变量将在 ~/.bashrc 被“获取”(读取)后立即出现。这只发生在你启动一个新的 shell 时(例如,当你打开一个新的终端时)。

因此,如果您将新行添加到您的 .bashrc 文件中,您将需要打开一个新终端并在那里运行您的 python 脚本。或者,您可以运行 source ~/.bashrc 将其源到当前 shell。

【讨论】:

    猜你喜欢
    • 2015-11-04
    • 2017-08-04
    • 1970-01-01
    • 2019-08-27
    • 2016-12-24
    • 2015-04-09
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多