导入一个文件的函数为什么要引入参数

Why do I need to introduce the arguments when importing the functions of one file

我有一个带有函数的程序 (file1.py),我想从文件 test1.py 中测试这些函数。当我导入第一个函数时,我不知道为什么终端告诉我需要引入我运行 file1.py 时需要的参数。我无法理解为什么会发生这种情况,因为据我所知 test1.py 我正在使用第一个功能而不是完整的 file1.py.

file1.py(直到第一个函数)

import os
import argparse
import pandas as pd
import numpy as np

# Enter the path/file names

parser = argparse.ArgumentParser()
parser.add_argument('--vcf1', type=str, required=True)
parser.add_argument('--vcf2', type=str, required=True)
args = parser.parse_args()
NAME_FILE_1 = args.vcf1
NAME_FILE_2 = args.vcf2


def load_sample (Name_file):
    '''
    Take the header of the body of the CSV file
    '''
    with open(Name_file, 'r') as f:
        for line in f:
            if line.startswith('#') and len(line)>2 and line[1] != '#':
                columns = line[1:-1].split('\t')
                data = pd.read_csv(Name_file, comment='#', delimiter='\t', names=columns)
                break
    return data

# The data of the VCF is here
dataA = load_sample (NAME_FILE_1)
dataB = load_sample (NAME_FILE_2)

还有我的test1.py

import os

import pandas as pd
import numpy as np

from VCF_matcher.app.run import load_sample


NAME_FILE_1 = "./test_sample.vcf"

# FIRST TEST

def test_load_sample():
    '''Verify all rows of the body of the vcf file is taken'''
    data_to_test = load_sample (NAME_FILE_1)
    assert len(data_to_test) == 10425

输出:

======================================================== ERRORS ========================================================
_________________________________________ ERROR collecting test_vcf_matcher.py _________________________________________
test_vcf_matcher.py:13: in <module>
    from VCF_matcher.app.run import load_sample
../app/run.py:26: in <module>
    args = parser.parse_args()
../../../opt/anaconda3/lib/python3.8/argparse.py:1768: in parse_args
    args, argv = self.parse_known_args(args, namespace)
../../../opt/anaconda3/lib/python3.8/argparse.py:1800: in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
../../../opt/anaconda3/lib/python3.8/argparse.py:2034: in _parse_known_args
    self.error(_('the following arguments are required: %s') %
../../../opt/anaconda3/lib/python3.8/argparse.py:2521: in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
../../../opt/anaconda3/lib/python3.8/argparse.py:2508: in exit
    _sys.exit(status)
E   SystemExit: 2
--------------------------------------------------- Captured stderr ----------------------------------------------------
usage: pytest [-h] --vcf1 VCF1 --vcf2 VCF2
pytest: error: the following arguments are required: --vcf1, --vcf2
=============================================== short test summary info ================================================
ERROR test_vcf_matcher.py - SystemExit: 2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

如果您不想在每次从其他 Python 文件导入此文件时 运行 “主要”部分,则必须按如下方式构建 file1.py

import os
import argparse
import pandas as pd
import numpy as np


def load_sample (Name_file):
    '''
    Take the header of the body of the CSV file
    '''
    with open(Name_file, 'r') as f:
        for line in f:
            if line.startswith('#') and len(line)>2 and line[1] != '#':
                columns = line[1:-1].split('\t')
                data = pd.read_csv(Name_file, comment='#', delimiter='\t', names=columns)
                break
    return data


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--vcf1', type=str, required=True)
    parser.add_argument('--vcf2', type=str, required=True)
    args = parser.parse_args()
    NAME_FILE_1 = args.vcf1
    NAME_FILE_2 = args.vcf2
    
    dataA = load_sample(NAME_FILE_1)
    dataB = load_sample(NAME_FILE_2)

为了更好的解释,see