【问题标题】:Python - Exception unhandled "name 'main' is not defined"Python - 未处理的异常“未定义名称'main'”
【发布时间】:2020-02-06 20:18:20
【问题描述】:

我目前正在学习 python,需要一些帮助。

我不确定哪里的语法不正确。它指出“名称'main'未定义”

我看到了其他语法看起来相同但由于某种原因我的示例无法正常工作的示例。

 class Main(main):
    """description of class"""

    def main():
      print('starting etl')
      # establish connection for target database (sql-server)
      target_cnx = pyodbc.connect(**datawarehouse_db_config)    
      # loop through credentials

      # sql-server
      for config in sqlserver_db_config: 
        try:
          print("loading db: " + config['database'])
          etl_process(sqlserver_queries, target_cnx, config, 'sqlserver')
        except Exception as error:
          print("etl for {} has error".format(config['database']))
          print('error message: {}'.format(error))
          continue
      target_cnx.close()

    if __name__ == "__main__":
        main()

任何帮助将不胜感激。

【问题讨论】:

  • 把班级里面的所有东西都拿出来,移出班级,然后删除班级。所有这些东西都不应该在课堂上。
  • 太好了,感谢您的帮助!

标签: python sql-server etl


【解决方案1】:

您的类 Main 尝试从不存在的类 main 继承。

main 只定义为Main 类的方法。

看起来您只想拥有一个函数 main 而没有类 Main

def main():
  print('starting etl')
  # establish connection for target database (sql-server)
  target_cnx = pyodbc.connect(**datawarehouse_db_config)    
  # loop through credentials

  # sql-server
  for config in sqlserver_db_config: 
    try:
      print("loading db: " + config['database'])
      etl_process(sqlserver_queries, target_cnx, config, 'sqlserver')
    except Exception as error:
      print("etl for {} has error".format(config['database']))
      print('error message: {}'.format(error))
      continue
  target_cnx.close()

if __name__ == "__main__":
    main()

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2022-11-14
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多