Pip uninstall error

Problem

I had installed a package using Pip. However, when I tried to uninstall it, I got this error:

$ sudo pip uninstall h5py
Can't uninstall 'h5py'. No files were found to uninstall.

Solution

Since I could not discover the real reason for this error, I resorted to finding the deleting the installed directory for this package. I found the directory h5py-2.6.0-py2.7.egg-info under /usr/local/lib/python2.7/dist-packages and deleted that.

Tried with: Ubuntu 14.04

Advertisements

How to open IPython Notebook file

The .ipynb file is an IPython Notebook file containing the Python commands, output and notes that another user has recorded.

To be able to create or open this file, we need to install IPython and its notebook package:

$ sudo apt install ipython-notebook

After installation, it is invoked as:

$ ipython notebook

This open a Mathematica-like web interface in your browser. You can create new IPython Notebook files or open existing files for viewing.

On a recent IPython Notebook file, I got this error when I opened it:

Error loading notebook
Unreadable Notebook: Unsupported JSON nbformat version 4 (supported version: 3)

This means that a more recent version of notebook package needs to be used to open this file. More confusingly, the notebook package has been moved into a new project called Jupyter. Since this is a new development its package was not available on my ancient Ubuntu.

I installed it from PyPI using:

$ sudo pip3 install jupyter

To open its notebook web interface:

$ jupyter-notebook

I was able to open the problematic IPython Notebook file correctly using this.

Tried with: Jupyter 4.x and Ubuntu 14.04

Pip install error with PyCUDA

Problem

  • I tried to install PyCUDA using pip:
$ sudo pip install pycuda
  • The installation tries to compile a few C++ files and it failed on the very first file with this error:
In file included from src/cpp/cuda.cpp:1:0:
src/cpp/cuda.hpp:14:18: fatal error: cuda.h: No such file or directory
#include <cuda.h>
                ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Investigation

  • This error was strange because I had set CUDA_ROOT and had added the bin path of CUDA installation to PATH environment variable. So, the installer should have found cuda.h which I could see was present in $CUDA_ROOT/include

  • To see what was happening, I tried the same command with verbosity:

$ sudo pip -vvv install pycuda
  • Now I could see that it was failing to find nvcc.

  • On downloading the source code of PyCUDA and checking setup.py, I saw that the check for nvcc was used to figure out the CUDA_ROOT and CUDA_INC_DIR.

  • The reason nvcc was not visible was that CUDA_ROOT was set for my user, but this PATH is not visible when a command is run under sudo, as described here. The solution was to make the CUDA bin path visible to sudo.

Solution

To make the $CUDA_ROOT/bin available in PATH for sudo, we can follow the steps described here. For example, on my system with CUDA 7.0 I followed these steps:

  • Created a new file /etc/profile.d/cuda.sh and added this line:
export PATH=/usr/local/cuda-7.0/bin:$PATH
  • Opened root shell without resetting PATH and ran the pip installation:
$ sudo su -
$ pip install pycuda

This worked and PyCUDA was installed successfully! 🙂

Tried with: PyCUDA 2015.1.2, CUDA 7.0 and Ubuntu 14.04

Pip ImportError on IncompleteRead

Problem

I was using the Pip from the Ubuntu repositories, installed using the package python-pip. I upgraded some Python packages using Pip and after one of them failed, invoking pip for any command gave this ImportError on the name IncompleteRead:

$ pip list
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 351, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2363, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2088, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 61, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/lib/python2.7/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 25, in <module>
    from requests.compat import IncompleteRead
ImportError: cannot import name IncompleteRead

Solution

The Pip from Ubuntu can be very old and trails the packages a lot. This problem went away when I removed this Pip and installed the latest one from PyPI:

$ sudo apt remove python-pip
$ sudo easy_install pip

Tried with: Pip 1.5.4 (old) and 7.1.0 (new) and Ubuntu 14.04

How to upgrade all installed Pip packages

Pip makes it convenient to install Python programs and modules from PyPI. One problem that I face with it is to upgrade all my installed packages.

  • pip help install seems to indicate that the --upgrade will upgrade all installed packages. But in reality it does not.

  • One way to solve this is to install the pip-tools package:

$ sudo pip install pip-tools
  • This package provides a pip-review tool that can be used to upgrade all installed packages:
$ sudo pip-review --auto --verbose

Tried with: pip-tools 0.3.6, Pip 7.1.0, Python 2.7.6 and Ubuntu 14.04

How to install and uninstall Python packages using Pip

Installing and managing packages of Python is a pain. Many popular Python packages are available for installation from Ubuntu repositories, but they can very old. Latest packages can be installed from PyPI using easy_install, but it makes it difficult to uninstall or upgrade packages.

I have found that the most convenient way to install and manage Python packages is Pip.

Install

An ancient version of Pip is available in Ubuntu repositories as the package python-pip. This is very old and using it leads to lots of problems down the line. I recommend not using this package.

Pip itself is available as a Python package at PyPI, so it can be installed from there:

$ sudo easy_install pip

Use

  • To install a package named foo from PyPI:
$ sudo pip install foo
  • To uninstall a package named foo:
$ sudo pip uninstall foo
  • To upgrade an installed package named foo:
$ sudo pip install --upgrade foo
  • To list all installed packages and their versions:
$ pip list
  • To search PyPI for a package named foo:
$ pip search foo

Tried with: Pip 7.1.0, Python 2.7.6 and Ubuntu 14.04