CarltonHere / auto-cpdaily

今日校园自动化是一个基于Python的爬虫项目,主要实现今日校园签到、信息收集、查寝等循环表单的自动化任务
https://carltonhere.github.io/auto-cpdaily/
Other
313 stars 69 forks source link

云函数无法识别utf-8,但好像又能识别 #21

Closed hhhllll888 closed 2 years ago

hhhllll888 commented 2 years ago

在云函数里git到全部数据安装依赖后报错

[root@ws-plzvvc-0 src]# python3 index.py 
Traceback (most recent call last):
  File "index.py", line 106, in <module>
    main()
  File "index.py", line 13, in main
    Utils.log("\u81ea\u52a8\u5316\u4efb\u52a1\u5f00\u59cb\u6267\u884c")
  File "/usr/local/var/functions/ap-guangzhou/lam-1yasdjie/auto-cq/src/actions/Utils.py", line 131, in log
    (Utils.getYmlConfig()['Version'], content))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 27-35: ordinal not in range(128)

我寻思可能是index.py没有定义utf-8 然后定义,再次运行, 同样结果, 于是我以为我的定义写错了,使用其他编码尝试 然后再次运行

[root@ws-plzvvc-0 src]# python3 index.py 
  File "index.py", line 1
SyntaxError: encoding problem: gbk

这能定义到,但是一直找不到原因,在电脑是可以运行的,上次反馈在云函数运行和电脑可以那次我忘了怎么设置的了 真烦人,这破云函数

CarltonHere commented 2 years ago

请检查您的python版本是否为3.6+,建议重装python环境后重试,必要的时候建议使用一键部署脚本

hhhllll888 commented 2 years ago

我这个是腾讯云函数

hhhllll888 commented 2 years ago
[root@ws-plzvvc-0 src]# python3 index.py 
Traceback (most recent call last):
  File "index.py", line 106, in <module>
    main()
  File "index.py", line 13, in main
    Utils.log("\u81ea\u52a8\u5316\u4efb\u52a1\u5f00\u59cb\u6267\u884c")
  File "/usr/local/var/functions/ap-guangzhou/lam-21b5a7b8/auto-cqdaily/src/actions/Utils.py", line 131, in log
    (Utils.getYmlConfig()['Version'], content))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 27-35: ordinal not in range(128)
[root@ws-plzvvc-0 src]# python3 index.py 
Traceback (most recent call last):
  File "index.py", line 106, in <module>
    main()
  File "index.py", line 13, in main
    Utils.log("\u81ea\u52a8\u5316\u4efb\u52a1\u5f00\u59cb\u6267\u884c")
  File "/usr/local/var/functions/ap-guangzhou/lam-21b5a7b8/auto-cqdaily/src/actions/Utils.py", line 131, in log
    (Utils.getYmlConfig()['Version'], content))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 27-35: ordinal not in range(128)

我服了啊,为啥python2就能识别utf-8,python3就没了

[root@ws-plzvvc-0 src]# python index.py 
  File "index.py", line 19
    f"10s后开始执行用户{user['user']['username'] if user['user']['username'] else '默认用户'}的任务"
                                                                                                                 ^
SyntaxError: invalid syntax
hhhllll888 commented 2 years ago

腾讯云函数只有python3.6的环境,没更高的了

hhhllll888 commented 2 years ago

我只能把测试日志发出来了

START RequestId: 6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9Init Report RequestId: 6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9 Coldstart: 2800ms (PullCode: 423ms InitRuntime: 16ms InitFunction: 2361ms) Memory: 128MB MemUsage: 29.96MBERROR RequestId: 6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9 Result: {"errorCode": -1, "errorMessage": "Traceback (most recent call last):
  File "/var/runtime/python3/bootstrap.py", line 133, in init_handler
    func_handler = get_func_handler(file.rsplit(".", 1)[0], func)
  File "/var/runtime/python3/bootstrap.py", line 160, in get_func_handler
    return getattr(mod, fname)
AttributeError: module 'index' has no attribute 'py'", "statusCode": 443}END RequestId: 6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9Report RequestId: 6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9 Duration: 0ms Memory: 128MB MemUsage: 29.96MBSTART RequestId:6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9
ERROR RequestId:6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9 Result:{"errorCode":-1,"errorMessage":"Traceback (most recent call last):\n  File \"/var/runtime/python3/bootstrap.py\", line 133, in init_handler\n    func_handler = get_func_handler(file.rsplit(\".\", 1)[0], func)\n  File \"/var/runtime/python3/bootstrap.py\", line 160, in get_func_handler\n    return getattr(mod, fname)\nAttributeError: module 'index' has no attribute 'py'","statusCode":443}
END RequestId:6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9
Report RequestId:6a7fcfd6-7c43-4caa-9cca-bf2830c7dea9 Duration:0ms Memory:128MB MemUsage:0.000000MB
hhhllll888 commented 2 years ago

额,奇迹般的,把执行方法写回去可以了