简体   繁体   English

如何从需要命令行输入的python脚本中导入

[英]How to import from a python script that requires command line input

As an example: I have a python script scip.py with 例如:我有一个python脚本scip.py与

from sys import argv

# parsing the input
script, NU = argv

def main(NU):
    return 

def somefunc():
    return

if __name__ == '__main__':
    main(NU)

Assume I am in a [I]python shell. 假设我在[I] python外壳中。 I can run the script eg via run scip.py 1 . 我可以通过run scip.py 1运行脚本。 But how can I import the functions from it? 但是如何从中导入功能? import scip fails, because it needs variables to unpack. import scip失败,因为它需要解压缩变量。 import scip 1 gives a SyntaxError. import scip 1给出SyntaxError。

this should do the trick: 这应该可以解决问题:

def main(NU):
    return 

def somefunc():
    return

if __name__ == '__main__':
    from sys import argv

    # parsing the input
    script, NU = argv
    main(NU)

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

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