Pythonからコマンドラインを呼んで、test.pyを実行しようとします(他のプログラムとかでもOK)。これをJupyterでやってしまうとNGです。
import subprocess
import sys
p = subprocess.Popen(["python", "test.py"],
stdout=sys.stdout)
p.communicate()
Jupyter(ipython)上で実行するとこういうエラーが出ます。---------------------------------------------------------------------------
UnsupportedOperation Traceback (most recent call last)
in ()
3
4 p = subprocess.Popen(["python", "test.py"],
----> 5 stdout=sys.stdout)
6 p.communicate()
/home/shimo/anaconda3/lib/python3.6/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
665 (p2cread, p2cwrite,
666 c2pread, c2pwrite,
--> 667 errread, errwrite) = self._get_handles(stdin, stdout, stderr)
668
669 # We wrap OS handles *before* launching the child, otherwise a
/home/shimo/anaconda3/lib/python3.6/subprocess.py in _get_handles(self, stdin, stdout, stderr)
1182 else:
1183 # Assuming file-like object
-> 1184 c2pwrite = stdout.fileno()
1185
1186 if stderr is None:
/home/shimo/anaconda3/lib/python3.6/site-packages/ipykernel/iostream.py in fileno(self)
357
358 def fileno(self):
--> 359 raise UnsupportedOperation("IOStream has no fileno.")
360
361 def write(self, string):
UnsupportedOperation: IOStream has no fileno.
Jupyterでなく、コマンドラインからPythonを呼ぶとOKです。
# from_commandline.py
import subprocess
import sys
p = subprocess.Popen(["python", "test.py"],
stdout=sys.stdout)
p.communicate()
$ python from_commandline.py
でtest.pyが実行されるはずです。
https://stackoverflow.com/questions/31080829/python-error-io-unsupportedoperation-fileno
https://github.com/ipython/ipykernel/issues/272
参考:Powershellを呼ぶ場合
subprocess.Popen(["powershell.exe", ".....ps1"], stdout=sys.stdout)
0 件のコメント:
コメントを投稿