通过Synology Task Scheduler运行时发生Python3 UnicodeEncodeError [英] Python3 UnicodeEncodeError when run via Synology task scheduler

查看:83
本文介绍了通过Synology Task Scheduler运行时发生Python3 UnicodeEncodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Synology任务计划程序运行脚本时,出现Python3 UnicodeEncodeError.通过命令行(使用PuTTY)运行脚本时,没有出现此错误.为什么会这样,我该如何解决?

I get a Python3 UnicodeEncodeError when I run my script via the Synology task scheduler. I do not get this error when I run the script via the commandline (using PuTTY). Why is this and how can I solve it?

简单的测试脚本:

import sys
print (sys.version) # to confirm the correct Python version
print("Fichier non trouvé♠ #M–Nein") # to test non ascii characters
test = "Fichier non trouvé♠ #M–Nein"
print ("test is " + test)
test2 = str(test) # to test if the string function causes and issue
print ("test2 is " + test2)

命令行输出:

admin@DiskStation:/volume1/@appstore/py3k/usr/local/bin$ /volume1/@appstore/py3k/usr/local/bin/python3 /volume1/Documenten/MyPythonScripts/Test.py
3.5.1 (default, Feb 23 2016, 17:46:04)
[GCC 4.9.3 20150311 (prerelease)]
Fichier non trouvé♠ #M–Nein
test is Fichier non trouvé♠ #M–Nein
test2 is Fichier non trouvé♠ #M–Nein

任务计划程序输出:

3.5.1 (default, Feb 23 2016, 17:46:04) 
[GCC 4.9.3 20150311 (prerelease)]
Traceback (most recent call last):
  File "/volume1/Documenten/MyPythonScripts/Test.py", line 3, in <module>
    print("Fichier non trouv\xe9\u2660 #M\u2013Nein") # to test non ascii characters
UnicodeEncodeError: 'ascii' codec can't encode characters in position 17-18: ordinal not in range(128)

注意:相同的Python版本和脚本使用来运行

Note: The same Python version and script are run using

/volume1/@appstore/py3k/usr/local/bin/python3
/volume1/Documenten/MyPythonScripts/Test.py

在两种情况下.

注2:如果我通过命令行但使用Python2.7运行脚本,则会发生早期(第1行)的Unicode错误:(下面的FYI,Python 3 vs Python 2)

Note2: An earlier (line 1) Unicode error occurs if I run the script via the commandline but using Python2.7: (FYI below, Python 3 vs Python 2)

admin@DiskStation:/volume1/Documenten/MyPythonScripts$ **python3** Test.py
Fichier non trouvé♠ #M–Nein
test is Fichier non trouvé♠ #M–Nein
test2 is Fichier non trouvé♠ #M–Nein
admin@DiskStation:/volume1/Documenten/MyPythonScripts$ **python** Test.py
  File "Test.py", line 1
SyntaxError: Non-ASCII character '\xc3' in file Test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

可以在Python2.7中解决此Unicode问题,方法是在脚本中添加以下内容作为第一行或第二行:

This Unicode issue can be solved in Python2.7 by adding the following as 1st or 2nd line to the script:

# -*- coding: UTF-8 -*-

然后该脚本可以从命令行正常运行.

Then the script runs fine from the command line.

但是添加此UTF-8行并不能解决从Synology Task Scheduler运行脚本的问题,那么仍然会引发错误?!:

But adding this UTF-8 line does not resolve the issue with running the script from the Synology Task Scheduler, then the error is still raised?!:

3.5.1 (default, Feb 23 2016, 17:46:04) 
[GCC 4.9.3 20150311 (prerelease)]
Traceback (most recent call last):
  File "/volume1/Documenten/MyPythonScripts/Test.py", line 4, in <module>
    print("Fichier non trouv\xe9\u2660 #M\u2013Nein") # to test non ascii characters
UnicodeEncodeError: 'ascii' codec can't encode characters in position 17-18: ordinal not in range(128)

推荐答案

从命令行运行时,Python会检测终端的编码,并以该编码将Unicode文本编码到终端.在您的任务计划程序下运行时,Python不会检测输出编码,并且默认为 ascii .

When running from the command line, Python detects the encoding of the terminal and encodes Unicode text to the terminal in that encoding. When running under your task scheduler, Python is not detecting the output encoding and is defaulting to ascii.

在将源编码声明为 utf8 时,它在Python 2中有效,因为您未使用Unicode字符串,而 print 只是将UTF-8编码的字节字符串发送到终点站.您的终端为UTF-8,因此可以正常工作.

It works in Python 2 when declaring the source encoding as utf8, because you are not using Unicode strings and print just sends the UTF-8-encoded byte string to the terminal. Your terminal is UTF-8, so it works.

在调度程序下运行时,可以通过设置环境变量 PYTHONIOENCODING = utf8 来覆盖Python的默认假设.该变量在所有平台上都可用.

You can override Python's default assumptions by setting the environment variable PYTHONIOENCODING=utf8 when running under the scheduler. This variable is available under all platforms.

参考: PYTHONIOENCODING

这篇关于通过Synology Task Scheduler运行时发生Python3 UnicodeEncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆