简体   繁体   English

模块导入前需要Python命令行导入环境变量

[英]Python command line import of environment variable required before module import

I use a module that has a default location to save files:我使用具有默认位置的模块来保存文件:

import foo # default directory is used to save files 

The module allows the use of an environment variable to change this location:该模块允许使用环境变量来更改此位置:

import os
os.environ['foo_path'] = "my/dir/path"
import foo # files now save in my/dir/path

If I import the module first, however, changing the environment variable does not help:但是,如果我先导入模块,更改环境变量无济于事:

import os
import foo
os.environ['foo_path'] = "my/dir/path" #No effect, files saved in default location

My problem is the following : I want to be able to run my module via command line, and I want to allow the user to pass a path to set os.environ['foo_path'] .我的问题如下:我希望能够通过命令行运行我的模块,并且我希望允许用户传递一个路径来设置os.environ['foo_path']

I am using the click module to pass some arguments, and thought about using it to get the path from the command line.我正在使用click模块来传递一些参数,并考虑使用它从命令行获取路径。 Ignoring the other arguments, this looks like this:忽略其他参数,这看起来像这样:

import click
import os

@click.command()
@click.option('--foo_path', default=None, help='dirpath to use instead of default', type=str)
def import_foo(foo_path):
    if foo_path:
        os.environ['foo_path'] = "my/dir/path" #Correctly changes the dirpath 
    import foo

This works , though the import in a function is a bit annoying.有效,尽管函数中的导入有点烦人。

However, if foo is somehow imported before, the approach does not work.但是,如果之前以某种方式导入了foo ,则该方法不起作用。 Imagine I need to import another module bar that also imports foo :想象一下,我需要导入另一个也导入foo模块bar

import click
import bar # which imports foo
import os

@click.command()
@click.option('--foo_path', default=None, help='dirpath to use instead of default', type=str)
def import_foo(foo_path):
    if foo_path:
        os.environ['foo_path'] = "my/dir/path" #No effect, since foo already imported   
    import foo # not executed, since foo already imported by bar

Does anyone have a suggestion for passing environment required in module imports?有没有人建议传递模块导入所需的环境?

The environment in which you execute your script usually provides a way to set the value of an environment variable.您执行脚本的环境通常提供一种设置环境变量值的方法。 For example, from the POSIX shell, you can simply prefix your Python command with an assignment that only applies to the new process's environment, rather than the current shell environment:例如,在 POSIX shell 中,您可以简单地在 Python 命令前加上一个仅适用于新进程环境而不是当前 shell 环境的赋值:

foo_path=/my/dir/path python script.py

If you plan on running the script multiple times, you can export the variable, so that each run inherits the value from the shell.如果您计划多次运行该脚本,则可以导出该变量,以便每次运行都从 shell 继承该值。

export foo_path=/my/dir/path
# Each of the following will see foo_path in its environment.
python script.py
python script.py
python script.py

Why do you need to save the location as an environment variable?为什么需要将位置保存为环境变量? Could you instead use sys.argv?你可以改用 sys.argv 吗?

Test.py:测试.py:

import sys

target_fp = sys.argv[1]
print('target_fp: ', target_fp)

Bash猛击

python test.py 'example/path/to/file'
# ('target_fp: ', 'example/path/to/file')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM