代码之家  ›  专栏  ›  技术社区  ›  Baktaawar David Maust

主模块未在CLI中运行[已关闭]

  •  0
  • Baktaawar David Maust  · 技术社区  · 6 年前

    我有这样的项目结构

    project
    |--invoices
      |--abc_company
      |--xyc_company
    |--scripts
     |-- __init__.py
     |-- extract_xyz_data.py
     |-- extract_abc_data.py
     |-- extract_data.py
     |-- utilities.py
     |-- abc_validation.py
     |-- xyz_validation.py
    |--main.py 
    

    我的主.py脚本调用提取_数据.py脚本文件夹中的脚本。摘录内_数据.py,我正在调用其他脚本,如extract_abc_数据.py最后,它使用print语句输出提取并返回一个提取的\u dir。

    这是怎么做的主.py看

    # main module
    import argparse
    
    from scripts.extract_data import extract_data
    
    def main():
        """ Main Module to run the script
    
        """
    
        # Creating a parset object
        parser = argparse.ArgumentParser()
    
        # Adding Invoice Directory path
        parser.add_argument('invoice_dir_path',
                           action='store',
                           type =str,
                           default = './invoices',
                           help='Pass the directory path of invoice folder (ex ./invoices)')
    
        # Adding Template directory path
        parser.add_argument('templates_dir_path',
                           action='store',
                           default = './templates',
                           type =str,
                           help='Pass the directory path of templates folder')
    
        # Adding USD Rate param
    
        parser.add_argument('usd_rate',
                           action='store',
                           default = 0.72,
                           type =float,
                           help='Enter the USD Rate')
    
    
        # Adding Month of Invoice Billing
    
        parser.add_argument('month',
                           action='store',
                           type =str,
                           help='Enter month of billing in words (ex. Dec/December')
    
         
    
        args = parser.parse_args()
        print(vars(args))
    
    
        print("calling extract_data")
        extracted_dir = extract_data(args)
    
        print(extracted_dir)
    
    
    
        if __name__ == "__main__":
            main()
    

    python main.py ./invoices ./templates 0.72 dec 
    

    我什么也得不到。没有错误,没有输出,没有解压,没有创建目录。即使是我在def main()中放置的print语句,也不会被打印到控制台。

    1 回复  |  直到 6 年前
        1
  •  2
  •   joshmeranda    6 年前

    在主函数中有一个入口点

    def main():
       # ... code
       if __name__ == "__main__":
           main()
    

    你需要它在初始块之外:

    def main():
        # ... code
    if __name__ == "__main__":
       main()