代码之家  ›  专栏  ›  技术社区  ›  pebox11

从shell调用python脚本,但将配置文件的位置作为参数传递

  •  0
  • pebox11  · 技术社区  · 6 年前

    如何从python脚本(ubuntu shell)调用函数,同时传递配置参数?一个相关的所以 post 似乎没有解决这个问题。

    这就是我现在拥有的:

    $ python -c ' from python_library import * ; function() ; -config /path/to/config/file '
    

    以上失败。以下组合(以及其他组合)也是如此:

        $ python -c ' from python_library import * ; function() -config /path/to/config/file '
    

        $ python -c ' from python_library import * ; function() ; -config "/path/to/config/file" '
    

    谢谢!

    2 回复  |  直到 6 年前
        1
  •  1
  •   avigil    6 年前

    您需要将conf参数作为Python的另一个参数拉出来。

    $ python -c ' from python_library import * ; function()' -config /path/to/config/file
    
        2
  •  1
  •   Andrej Kesely    6 年前

    您可以使用环境变量:

    MYPARAMETERS="-config /path/to/config/file" python -c "import os,sys;sys.argv = os.environ['MYPARAMETERS'].split(); import python_library import * ; function()"