-1

I write my own cmd tool and I have created a click cmd where I trigger my upload function. Inside this upload function, I do a artifactory deploy call. If I test this whith pytest the function works, If I test it over the cmd line, it cant find my enviromental variables.

I have added these into my .bashrc and also in pycharm as env var.

Do have any idea why it does not work over the command line. It says that my env vars are None

MY MAIN:

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)

Log Class:

 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)

EXCEPTION:

TypeError: must be str, not NoneType
Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
  • 2
    If you forgot to set or `export` those variables, that's what happens. We can't see the environment you are calling this script from but that would seem like the obvious conclusion. – tripleee Nov 30 '20 at 14:52
  • If that doesn't fix it, please reduce to a [mre] so we can see exactly what you are doing, ideally with code which only demonstrates the problem and doesn't attempt to do anything else. (Clumsily using `os.system` instead of `subprocess.run` is one obvious distraction here.) – tripleee Nov 30 '20 at 14:53

1 Answers1

0

This solved my problem:

When you define a variable in ~/.bashrc, that variable will be present as soon as ~/.bashrc is "sourced" (read). This only happens when you start a new shell (e.g. when you open a new terminal).

So, if you add the new line to your .bashrc file, you will then need to open a new terminal and run your python script there. Alternatively, you can run source ~/.bashrc to source it into the current shell.