IPython console can't locate “backports.shutil

2019-01-31 13:28发布

I'm running Python2.7 on windows 10 doing env and most pkg management with Anaconda. After upgrading a number of packages, my ipython console now fails to start in any IDE or at the console. When I attempt to run it at the console I get this error:

Traceback (most recent call last):
File "C:\Anaconda3\Scripts\ipython-script.py", line 3, in <module>
import IPython
File "C:\Anaconda3\lib\site-packages\IPython\__init__.py", line 48, in  <module>
from .core.application import Application
File "C:\Anaconda3\lib\site-packages\IPython\core\application.py", line 24, in <module>
from IPython.core import release, crashhandler
File "C:\Anaconda3\lib\site-packages\IPython\core\crashhandler.py", line 28, in <module>
from IPython.core import ultratb
File "C:\Anaconda3\lib\site-packages\IPython\core\ultratb.py", line 121, in <module>
from IPython.utils.terminal import get_terminal_size
File "C:\Anaconda3\lib\site-packages\IPython\utils\terminal.py", line 27, in <module>
import backports.shutil_get_terminal_size
ImportError: No module named backports.shutil_get_terminal_size

The first thing I tried to do was:

pip install --upgrade backports.shutil_get_terminal_size

output:

Requirement already up-to-date: backports.shutil_get_terminal_size in c:\anaconda3\lib\site-packages

I've uninstalled and reinstalled ipython with both

conda uninstall ipython
conda install ipython

and

pip uninstall ipython
pip install ipython

Still won't work. Help please!

7条回答
beautiful°
2楼-- · 2019-01-31 13:43

According to this thread this is due to a bug in conda which leads to a conflict with pip installs and can be solved with a force re-install. For the thread author, $ conda install --force ipython solved the issue, for me it was $ conda install --force backports.

查看更多
男人必须洒脱
3楼-- · 2019-01-31 13:45
  • Works for Anaconda Python version Anaconda2-4.2.0-Linux-x86_64.sh
  • Edit the file and get rid the "try" and "except" import statements
  • Add in the new import statement
  • Update config parser => ./conda install configparser
  • Install nbbrowserpdf => .pip install nbbrowserpdf

    vim +22 /home/alienone/anaconda2/lib/python2.7/site-packages/IPython/utils/terminal.py

    from backports import shutil_get_terminal_size as _get_terminal_size

查看更多
神经病院院长
4楼-- · 2019-01-31 13:45

The only thing which worked for me was to download the tarball from pypi and run python setup.py install

It worked like a charm

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-31 13:49

Virtualenv can prove very useful in a case like this, and even more specifically, a virtualenv with no global site packages allowed. Rule out many causes just by doing a clean install in an isolated virtualenv.

In my experience IPython and its dependencies really want to be in the same site. If you have the backports package installed globally but IPython installed in the user roaming site, you might expect runtime import errors such as the ones described in the OP.

I realize that sometimes we need global site packages but the penalty is a more complicated site and dependency handling within pip/setuptools. Depending on several python configuration and windows environment conditions, your packages may be spread across global sites, user (roaming) sites, and virtualenv sites.

Rule out weird site problems by building and installing clean in a virtualenv with no access to global or user packages. The virtualenvwrapper and add2virtualenv command can be used to cleanly allow certain global packages.

查看更多
倾城 Initia
6楼-- · 2019-01-31 13:51

In case this helps anybody, the problem for me seemed to be having installed configparser with pip locally, and then trying to install everything else using conda. To fix:

pip uninstall configparser
# You may need to install configparser now using conda...
ipython
# No ImportError now.

It seems like the configparser install puts a backports package in the import namespace, which took precedence over the conda installed package.

查看更多
家丑人穷心不美
7楼-- · 2019-01-31 14:00

I am on CentOS 7, and I needed to change my terminal.py as shown below.

On the import statements I messed around with the prefixes and got it to work -

import os
import sys
import warnings
try:
  from backports import get_terminal_size as _get_terminal_size
except ImportError:
  # use backport on Python 2
  from shutil_backports import get_terminal_size as _get_terminal_size
查看更多
登录 后发表回答