Synologyタスクスケジューラを使用してスクリプトを実行すると、Python3 UnicodeEncodeErrorが発生します。コマンドラインから(PuTTYを使用して)スクリプトを実行すると、このエラーは発生しません。これはなぜですか、どうすれば解決できますか?Synologyタスクスケジューラ経由でPython3 UnicodeEncodeErrorを実行する
シンプルなテストスクリプト:
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)
コマンドライン出力:
[email protected]:/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のバージョンとスクリプトが実行されている使用して
両方の状況で注2:私は、コマンドライン経由でスクリプトを実行するが、Python2.7を使用している場合は、以前の(ライン1)Unicodeのエラーが発生します(FYI以下、Pythonの2対Pythonの3)を
[email protected]:/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
[email protected]:/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
は、このUnicodeの問題ことができますスクリプトに第1行または第2行として次を追加してPython2.7で解決してください:
# -*- coding: UTF-8 -*-
スクリプトはコマンドラインから正常に実行されます。
しかし、このUTF-8行は、エラーがまだ発生し、Synologyのタスクスケジューラからスクリプトを実行すると問題が解決しない追加:?!
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)