在python中导入mysql.connector在timer trigger azure函数中无法工作

zzoitvuj  于 2023-11-21  发布在  Python
关注(0)|答案(1)|浏览(169)

我试图使用MySQL数据库在Azure函数中通过计时器触发器在Python中生成定期汇总报告。Python代码使用import mysql.connector让我的查询数据自行正常工作。然而,当我插入这部分代码用于触发器操作时,我得到ModuleNotFoundError:No module named 'mysql'。
MySQL连接器和定时器触发器功能生成的电子邮件报告都工作得很好,但是当把它们放在一起时就不工作了。

  1. import logging
  2. import azure.functions as func
  3. app = func.FunctionApp()
  4. @app.schedule(schedule="*/30 * * * * *", arg_name="myTimer",
  5. run_on_startup=True,
  6. use_monitor=False)
  7. def timer_trigger1(myTimer: func.TimerRequest) -> None:
  8. import mysql.connector
  9. bp1db = mysql.connector.connect(
  10. host="xxxx.xxxxx.com",
  11. user="xxxxx",
  12. password="xxxx01",
  13. db = 'boppwh'
  14. )
  15. cur1 = bp1db.cursor()
  16. cur1.execute("SELECT COUNT(cellid) AS free FROM cells WHERE Status = 0")
  17. result1 = cur1.fetchall()
  18. free1 = result1[0]
  19. logging.info('Python timer trigger function executed.')

字符串

92vpleto

92vpleto1#

我在代码中的导入中添加了import mysql.connector,并且能够触发定时器触发器函数。

产品代码:

  1. import logging
  2. import azure.functions as func
  3. import mysql.connector
  4. app = func.FunctionApp()
  5. @app.schedule(schedule="*/30 * * * * *", arg_name="myTimer", run_on_startup=True,
  6. use_monitor=False)
  7. def timer_trigger1(myTimer: func.TimerRequest) -> None:
  8. bp1db = mysql.connector.connect(
  9. host="<server_name>.mysql.database.azure.com",
  10. user="<user_name>",
  11. password="<password>",
  12. db='boppwh'
  13. )
  14. cur1 = bp1db.cursor()
  15. cur1.execute("SELECT COUNT(cellid) AS free FROM cells WHERE Status = 0")
  16. result1 = cur1.fetchall()
  17. free1 = result1[0]
  18. logging.info('Python timer trigger function executed.')

字符串

requirements.txt:

  1. azure-functions
  2. mysql-connector-python

输出:

运行成功并触发定时器触发功能,如下所示。


的数据

展开查看全部

相关问题