从cat重定向导致不同的python解释器行为

时间:2018-05-08 20:22:10

标签: python python-3.x paramiko io-redirection pythoninterpreter

我不确定如何最好地解决或查询此问题。我有一个简单的程序正在使用fs.sshfs模块,该模块依赖于paramiko,前面是cryptogaphy.utils。来自我的virtualenv python3.5.2

(venv) $ cat tump.py
from fs.sshfs import SSHFS

remote = SSHFS('rushmore')
print(*remote.listdir('.'), sep='\n')

我的环境安装了以下模块。

(venv) $ pip freeze
appdirs==1.4.3
asn1crypto==0.24.0
bcrypt==3.1.4
cffi==1.11.5
cryptography==2.2.2
fs==2.0.21
fs.sshfs==0.8.0
idna==2.6
paramiko==2.4.1
pkg-resources==0.0.0
pyasn1==0.4.2
pycparser==2.18
PyNaCl==1.2.1
pytz==2018.4
six==1.11.0
typing==3.6.4

当我直接在文件上调用python时,我会针对AttributeError收到getargspec

(venv) $ python tump.py
Traceback (most recent call last):
  File "tump.py", line 1, in <module>
    from fs.sshfs import SSHFS
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/fs/sshfs/__init__.py", line 7, in <module>
    from .sshfs import SSHFS
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/fs/sshfs/sshfs.py", line 13, in <module>
    import paramiko
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/paramiko/__init__.py", line 22, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/paramiko/transport.py", line 34, in <module>
    from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/cryptography/hazmat/primitives/ciphers/__init__.py", line 7, in <module>
    from cryptography.hazmat.primitives.ciphers.base import (
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/cryptography/hazmat/primitives/ciphers/base.py", line 11, in <module>
    from cryptography import utils
  File "/mnt/max/home/robinsph/git/HCPVaTT/venv/lib/python3.5/site-packages/cryptography/utils.py", line 85, in <module>
    signature = inspect.getargspec
AttributeError: module 'inspect' has no attribute 'getargspec'

当我使用重定向到文件描述符调用时,

(venv) $ python <(cat tump.py)
lib32
boot
var
app
bin
lost+found
usr
snap
vmlinuz
initrd.img
root
srv
proc
home
lib
lib64
sys
initrd.img.old
mnt
tmp
media
libx32
run
opt
.bash_history
sbin
vmlinuz.old
dev
etc

任何人都可以重现这个吗?我想我已经失去了理智。

1 个答案:

答案 0 :(得分:1)

您的路径中有一个名为inspect.py的其他文件,而该文件是导入的文件。添加

import inspect
print(inspect.__file__)

到程序的开头帮助找到这个。就其本身而言,这是许多问题的重复。你的有趣之处在于重定向正在改变路径。如果我们在创建本地print(sys.path[:3])后将inspect.py添加到fs.sshfs的导入中,我会得到:

(36) dsm@winter:~/coding$ python <(cat tump.py)
/home/dsm/sys/miniconda3/envs/36/lib/python3.6/inspect.py
['/dev/fd', '/home/dsm/sys/miniconda3/envs/36/lib/python36.zip', '/home/dsm/sys/miniconda3/envs/36/lib/python3.6']
(36) dsm@winter:~/coding$ python tump.py 
/home/dsm/coding/inspect.py
['/home/dsm/coding', '/home/dsm/sys/miniconda3/envs/36/lib/python36.zip', '/home/dsm/sys/miniconda3/envs/36/lib/python3.6']
Traceback (most recent call last):
  File "tump.py", line 6, in <module>
    from fs.sshfs import SSHFS
[...]
AttributeError: module 'inspect' has no attribute 'getmro'

以及文件描述符版本工作的原因是因为有效的&#34;本地&#34;路径现在是/dev/fd而不是/home/dsm/coding,而/dev/fd不包含inspect.py冲突。会有微小的差异(我使用3.6等),但可能是同样的原因。