从另一个脚本导入python脚本并使用参数运行它 [英] importing a python script from another script and running it with arguments
问题描述
我有一个Python脚本,该脚本已打包为命令行脚本( Pyrseas 中的dbtoyaml.py你问).
I have a python script which has been packaged up as a command line script (dbtoyaml.py in Pyrseas since you ask).
我正在运行另一个要从中调用此脚本的python脚本.有没有办法从我的第二个脚本中导入模块并人工填充所需的参数,从而根本避免更改任何pyrseas代码?
I am running another python script from which I want to call this script. Is there no way to import the module and artificially populate the required arguments from my second script to avoid changing any of the pyrseas code at all?
from pyrseas import dbtoyaml
-- My initial script, which also takes arguments
dbtoyaml.main(['-m','-H MYHOSTNAME' .... other options])
from pyrseas import dbtoyaml
-- My initial script, which also takes arguments
dbtoyaml.main(['-m','-H MYHOSTNAME' .... other options])
还没有为我工作.
我收到一个奇怪的错误:
I get a strange error:
usage: checkSchemaChanges.py [-h] [-H HOST] [-p PORT] [-U USERNAME] [-W]
[-c CONFIG] [-r REPOSITORY] [-o OUTPUT]
[--version] [-m] [-O] [-x] [-n SCHEMA]
[-N SCHEMA] [-t TABLE] [-T TABLE]
dbname
checkSchemaChanges.py: error: unrecognized arguments: MYHOSTNAME mydatabaseuser
这是我的新脚本(checkSchemaChanges.py,底部的MYHOSTNAME和mydatabaseuser)和dbtoyaml中的参数的正确组合.
Which is a mixture of my new script (checkSchemaChanges.py, and MYHOSTNAME and mydatabaseuser at the bottom) and the parameters from dbtoyaml, which are all correct.
可能是使argparse混淆的双重参数吗?
Could it be the double set of parameters which is confusing argparse?
推荐答案
这似乎不是最好的方法,但您可以设置sys.argv
this seems like not the best way to do it but you can probably set sys.argv
import sys
sys.argv += ['-m','-H MYHOSTNAME' .... other options]
from pyrseas import dbtoyaml
dbtoyaml.main()
但实际上我不知道dbtoyaml.py是什么样子
but really I have no idea what dbtoyaml.py lookslike or is
这篇关于从另一个脚本导入python脚本并使用参数运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!