在Python中使用argparse从脚本导入函数

在Python中使用argparse从脚本导入函数,python,parsing,argparse,Python,Parsing,Argparse,我已经用Python编写了一个脚本,它使用ArgumentParser并在从cmd行运行时通过argparse进行切换。现在,我需要编写第二个脚本,它使用来自的函数,但有自己的解析器用于自己的输入 在脚本my argparse函数中: def switches(): filepath = os.path.dirname(os.path.abspath(__file__)) parser = argparse.ArgumentParser(prog='log-analyser')

我已经用Python编写了一个脚本,它使用ArgumentParser并在从cmd行运行时通过argparse进行切换。现在,我需要编写第二个脚本,它使用来自的函数,但有自己的解析器用于自己的输入

在脚本my argparse函数中:

def switches():
    filepath = os.path.dirname(os.path.abspath(__file__))
    parser = argparse.ArgumentParser(prog='log-analyser')
    parser.add_argument('-ch', '--channels', help='desired number of output channels. 0 here will terminate the script.', default=2, type=int)
    parser.add_argument('-r', '--ranges', nargs='?', help='input the ranges limits. use square brackets format [a,b,c,....,n] to get a-b, b-c, c-d, (...) ranges. DONT USE SPACES! frames beyond the limits will not be checked.')
    parser.add_argument('-m', '--margins', nargs='?', help='input the margins for stated ranges. use square brackets format [x,y,z,...,xz] to get x for a-b, y for b-c etc. DONT USE SPACES! (r-1) margins needed. ')
    parser.add_argument('-log', '--logfilepath', default='log1', help='filename of input_logfile to process (logfile for pcmtool supported). could be whole path to input_logfile.')
    parser.add_argument('-pre', '--prefix', default='', help='output file prefix and name of folder for outputs. optional; if left empty, program will output files with input_logfile prefix (e.g. log1_errors_ch1.txt).')
    parser.add_argument('-outdir', '--outputpath', default=filepath, help='path for every output for this program. .py file dir by default.')
    parser.add_argument('--marginsfilename', default='margins.txt', help='name of margin file to process. optional; if file exists, program inputs ranges and margins from this file. if file doesnt exist, it will be created, but correct -r and -m data is required.')
return parser.parse_args()
我需要这个脚本中的几个其他函数,例如:

def convert_to_floatlist(string_input):
    out = string_input
    out = out.replace('[', '')
    out = out.replace(']', '')
    out = out.split(',')
for i in range (0, len(out)):
    out[i] = float(out[i])
return out
但是,当我在第二个脚本中调用

from log_analyser import convert_to_floatlist
我从脚本B得到的只是脚本A的参数检查失败

>> Please use -r & -m switches or specify correct marginfile directory. Program will now terminate.

有解决办法吗?我不需要A中的switches(),但我需要一些其他函数,我想在B中argparse。

似乎
log\u分析器
不使用
如果uuuu name\uuuu=='.\uu main\uuu':
guard;你应该放一个进去。哇,谢谢,马上就好了!