2

I have Jupyter Notebook installed on Ubuntu. Python 2 and 3 run OK on the terminal but only Python 3 shows up on IPython. I am trying to get both kernels (2 and 3) up on IPython.

I've tried to manually install ipykernel from https://github.com/ipython/ipykernel which was successful but still can't get Python 2 kernel to show up. I also tried:

> sudo ipython2 kernelspec install-self

And got the error: ImportError: No module named shutil_get_terminal_size

Next, i tried:

python2 -m pip install --upgrade ipykernel 
python2 -m ipykernel install 

The second line returned an error:

/usr/bin/python2: No module named shutil_get_terminal_size; 'ipykernel' is a package and cannot be directly executed

I guess the challenge is with the module named shutil_get_terminal_size. Please, kindly advise on how I can fix this. I read this post: https://github.com/ipython/ipython/issues/9416 but can't make much sense of it.

Thank you!

1
  • Not exactly an answer, but why not use separate virtual environments for Python 2 and Python 3? This is what I do. Commented Jun 8, 2016 at 21:52

1 Answer 1

1

I experienced the same issue and found that a backports package was installed in both the Python 2.7 system site directory and the user site directory. Furthermore, the backports.shutil_get_terminal_size package was only in the system site directory:

$ ls -l $(find / -name 'backports' 2>/dev/null)
/root/.local/lib/python2.7/site-packages/backports:
total 0
drwxr-xr-x 1 root root 88 Aug  4 16:04 configparser

/usr/lib/python2.7/site-packages/backports:
total 8
-rw-r--r-- 1 root root  75 Aug  4 17:58 __init__.py
-rw-r--r-- 1 root root 267 Aug  4 17:58 __init__.pyc
drwxr-xr-x 1 root root 128 Aug  4 17:58 shutil_get_terminal_size

When Python executes an import statement, it looks in the user site directory first, and then looks in the system site directory. Since the shutil_get_terminal_size package is not in the backports package in the user site directory, that import will always fail. Here's what the import error looks like in its minimal form:

$ echo "from backports.shutil_get_terminal_size import get_terminal_size; \
        print(get_terminal_size())" | python2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named shutil_get_terminal_size

Adding the -s option on the python command line tells Python to not use the user site directory, and the import command succeeds:

$ echo "from backports.shutil_get_terminal_size import get_terminal_size; \
        print(get_terminal_size())" | python2 -s
terminal_size(columns=319, lines=104)

The install command also succeeds when the -s option is added to the python command line:

$ python2 -s -m ipykernel install
Installed kernelspec python2 in /usr/local/share/jupyter/kernels/python2

How the backports package got into the user site directory in the first place is still a mystery.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.