从另一个脚本导入 python 脚本并使用参数运行它 [英] importing a python script from another script and running it with arguments

查看:31
本文介绍了从另一个脚本导入 python 脚本并使用参数运行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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-- 我的初始脚本,它也接受参数dbtoyaml.main(['-m','-H MYHOSTNAME' .... 其他选项])

还没有为我工作.

我收到一个奇怪的错误:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆