gpt4 book ai didi

python - 在 ipython 中执行 `ls` 时如何修复 PermissionError?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:03 26 4
gpt4 key购买 nike

当我使用 python 2.7 (2.7.12) 运行 ipython(版本 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

最佳答案

由于某些未知原因,未定义在环境变量 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/

关于python - 在 ipython 中执行 `ls` 时如何修复 PermissionError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51657307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com