回答編集履歴
1
追記
test
CHANGED
@@ -21,3 +21,113 @@
|
|
21
21
|
```
|
22
22
|
|
23
23
|
なかんじで。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
---
|
28
|
+
|
29
|
+
**コメントでの補足を受けて、追記しました**
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
標準入出力のフラッシュとかcronジョブ特有の問題とかではなく、コードにエラーがある為にスクリプトの実行がされていないようです。Ubuntu17.04 + python3.5.3 で試した例を示します。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
スクリプトをtest01.pyとして以下を用意します。(ダメ押しで`flush`しています)
|
38
|
+
|
39
|
+
```Python3
|
40
|
+
|
41
|
+
# test01.py
|
42
|
+
|
43
|
+
import time
|
44
|
+
|
45
|
+
import sys
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
print("test1", flush=True)
|
50
|
+
|
51
|
+
print("test2", flush=True)
|
52
|
+
|
53
|
+
sys.stdout.flush();
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
time.sleep(5)
|
58
|
+
|
59
|
+
v = 1
|
60
|
+
|
61
|
+
# cause exception
|
62
|
+
|
63
|
+
print("str " + v)
|
64
|
+
|
65
|
+
# syntax error
|
66
|
+
|
67
|
+
print "test3"
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
これを実行すると、Syntax Errorが報告されるだけで、そもそも"test1"も"test2"も出力されません。コードが実行されていないのでしょう。(まぁ、当たり前と言えば当たり前)
|
74
|
+
|
75
|
+
```Bash
|
76
|
+
|
77
|
+
$ python3 test01.py
|
78
|
+
|
79
|
+
File "test01.py", line 13
|
80
|
+
|
81
|
+
print "test3"
|
82
|
+
|
83
|
+
^
|
84
|
+
|
85
|
+
SyntaxError: Missing parentheses in call to 'print'
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
修正して、python3としては文法エラーである`print "test03"の部分だけコメントにし、実行します。
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
```bash
|
96
|
+
|
97
|
+
$ python test01.py
|
98
|
+
|
99
|
+
test1
|
100
|
+
|
101
|
+
test2
|
102
|
+
|
103
|
+
Traceback (most recent call last):
|
104
|
+
|
105
|
+
File "test01.py", line 11, in <module>
|
106
|
+
|
107
|
+
print("str " + v)
|
108
|
+
|
109
|
+
TypeError: Can't convert 'int' object to str implicitly
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
ランタイムエラーでExceptionは吐かれますが、"test1", "test2"は出力され、コードが実行されていることが分かります。要は、まず「**python3のコードとして完成していなければならない。**」が最低条件だと思います。そうすれば、can110さんに実際に試していただいたように出力されるようになるはずです。
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
おまけですが、py_compileモジュールで文法チェックだけする方法があります。
|
118
|
+
|
119
|
+
文法エラーのある状態でtest01.pyをコンパイルした例
|
120
|
+
|
121
|
+
```Bash
|
122
|
+
|
123
|
+
$ python3 -m "py_compile" test01.py
|
124
|
+
|
125
|
+
File "test01.py", line 13
|
126
|
+
|
127
|
+
print "test3"
|
128
|
+
|
129
|
+
^
|
130
|
+
|
131
|
+
SyntaxError: Missing parentheses in call to 'print'
|
132
|
+
|
133
|
+
```
|