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

如何将变量传递给python脚本?

  •  3
  • Shubham  · 技术社区  · 16 年前

    实际上,我已经用vB(用于GUI)和python(用于脚本)构建了一个应用程序。我需要将变量传递给python,类似于它的关键字参数,即x=val1,y=val2。有没有办法做到这一点?

    6 回复  |  直到 16 年前
        1
  •  9
  •   Rui Vieira    16 年前

    如果您使用的是Python<2.7我建议 optparse optparse已被弃用,在2.7中您应该使用 argparse

    它使传递命名参数变得轻而易举。

        2
  •  6
  •   pyInTheSky    16 年前

    thepyscript.py "x = 12,y = 'hello world', z = 'jam'"
    

    解析do:

    stuff = arg[1].split(',')
    for item in stuff:
        exec(item) #or eval(item) depending on how complex you get 
    #Exec can be a lot of fun :) In fact with this approach you could potentially  
    #send functions to your script.
    #If this is more than you need, then i'd stick w/ arg/optparse
    
        3
  •  3
  •   ars    16 年前

    IronPython 可能是一种选择。由于VB和IronPython都可以通过.NET进行交互,因此可以将脚本封装在程序集中,并公开一个用所需参数调用的函数。

        4
  •  1
  •   extraneon    16 年前

    getopt module Dive Into Python .

    如果您使用的是python2.7(而不是更低版本),那么您还可以查看 argparse module

        5
  •  1
  •   Adam Matan    16 年前

    The .ini style is easily readable by ConfigParser :

    [Section_1]
    foo1=1
    foo2=2
    foo3=5
    ...
    
    [Section_2]
    bar1=1
    bar2=2
    bar3=3
    ...
    

    如果你有大量的变量,这可能是正确的方法。

        6
  •  0
  •   schoetbi    16 年前

    您认为如何创建一个python脚本,从gui端设置这些变量?启动python应用程序时,只需启动这个脚本,就可以使用vars。

    Execfile