当我在ipython
2.7(2.7.12)中运行python
(版本5.4.1)并执行ls
时,出现PermissionError:
$ ipython
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
Type "copyright", "credits" or "license" for more information.
IPython 5.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: ls
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-1-e6d7425bc79f> in <module>()
----> 1 get_ipython().magic(u'ls ')
/home/adietz/.local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
2158 magic_name, _, magic_arg_s = arg_s.partition(' ')
2159 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2160 return self.run_line_magic(magic_name, magic_arg_s)
2161
2162 #-------------------------------------------------------------------------
/home/adietz/.local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
2079 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2080 with self.builtin_trap:
-> 2081 result = fn(*args,**kwargs)
2082 return result
2083
/home/adietz/.local/lib/python2.7/site-packages/IPython/core/alias.pyc in __call__(self, rest)
185 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
186
--> 187 self.shell.system(cmd)
188
189 #-----------------------------------------------------------------------------
/home/adietz/.local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in system_raw(self, cmd)
2247 try:
2248 # Use env shell instead of default /bin/sh
-> 2249 ec = subprocess.call(cmd, shell=True, executable=executable)
2250 except KeyboardInterrupt:
2251 # intercept control-C; a long traceback is not useful here
/usr/lib/python2.7/subprocess.pyc in call(*popenargs, **kwargs)
521 retcode = call(["ls", "-l"])
522 """
--> 523 return Popen(*popenargs, **kwargs).wait()
524
525
/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
709 p2cread, p2cwrite,
710 c2pread, c2pwrite,
--> 711 errread, errwrite)
712 except Exception:
713 # Preserve original exception in case os.close raises.
/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
1341 raise
1342 child_exception = pickle.loads(data)
-> 1343 raise child_exception
1344
1345
OSError: [Errno 13] Permission denied
我删除并重新安装了ipython
,但没有成功。
但是,当我执行glob("*")
时,它可以按预期运行,并且可以获取当前目录的内容。
有什么想法可以解决此问题吗?或如何找出原因?
附录
经过一些实验,我发现令人讨厌的行是subprocess.call
函数,我可以通过运行来重现错误
subprocess.call('ls', shell=True, executable=os.environ.get('SHELL', None))
此外,最后一部分的计算结果为空字符串:
In [7]: os.environ.get('SHELL', None)
Out[7]: ''
这似乎是令人反感的部分。没有它,它运行正常
In [8]: subprocess.call('ls', shell=True)
adietz Desktop Documents Downloads examples.desktop Music opt Pictures Private Projects Public Templates Videos Work
Out[8]: 0
答案 0 :(得分:0)
由于某些未知原因,未定义在环境变量SHELL
中指定的用户首选的shell命令。定义此文件的通常位置是文件`/ etc / passwd'(请参见here)。
因此,手动设置此变量可以消除上述问题:
$ export SHELL=/bin/bash
$ ipython
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
Type "copyright", "credits" or "license" for more information.
IPython 5.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: ls
adietz@ Desktop/ Documents/ Downloads/ examples.desktop Music/ opt/ Pictures/ Private/ Projects/ Public/ Templates/ Videos/ Work/