简体   繁体   English

从另一个python运行脚本

[英]Running a script from another python

I just want to have some ideas to know how to do that... 我只想有一些想法知道该怎么做...

I have a python script that parses log files, the log name I give it as an argument so that when i want to run the script it's like that.. ( python myscript.py LOGNAME ) what I'd like to do is to have two scripts one that contains the functions and another that has only the main function so i don't know how to be able to give the argument when i run it from the second script. 我有一个用于解析日志文件的python脚本,我将其作为参数提供给日志名称,以便当我想运行该脚本时就这样。.( python myscript.py LOGNAME )我要做的就是两个脚本,一个包含函数,另一个仅包含主要函数,因此当我从第二个脚本运行参数时,我不知道如何给出参数。


here's my second script's code: 这是我的第二个脚本的代码:

import sys
import os 

path = "/myscript.py"
sys.path.append(os.path.abspath(path))
import myscript 

mainFunction()

the error i have is: 我的错误是:

script, name = argv 
valueError: need more than 1 value to unpack

Python (just as most languages) will share parameters across imports and includes. Python(与大多数语言一样)将在导入和包含之间共享参数。 Meaning that if you do: 意味着如果您这样做:

python mysecondscript.py heeey that will flow down into myscript.py as well. python mysecondscript.py heeey也会流入myscript.py So, check your arguments that you pass. 因此,请检查您通过的论点。

Script one 剧本一

myscript = __import__('myscript')
myscript.mainfunction()

script two 剧本二

import sys
def mainfunction():
    print sys.argv

And do: 然后做:

python script_one.py parameter

You should get: 您应该得到:

["script_one.py", "parameter"] [“ script_one.py”,“参数”]

You have several ways of doing it. 您有几种方法可以做到这一点。

>>> execfile('filename.py')

Check the following link: 检查以下链接:

How to execute a file within the python interpreter? 如何在python解释器中执行文件?

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

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