Linux – How to properly install Pipenv on WSL Ubuntu 18.04

pippythonubuntu 18.04windows-subsystem-for-linux

I know this sounds like a pretty basic question but i've been frustrating myself to no end for the better part of a day. Am trying to set up a python environment in Windows Subsystem for Linux and everything seems to break when I install pipenv.

Steps to reproduce:

  1. Fresh install of Ubuntu 18.04 from windows store

  2. sudo apt update && sudo apt upgrade

  3. sudo apt install python3-pip

  4. pip3 install --user pipenv

Pip outputs everything i would expect, no errors, looks like pipenv is installed.

if I try use pipenv i get pipenv: command not found even though my local bin is in my PATH

worse than that though, pip is now broken and attempting to use pip gives me the following:

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

So it seems like the pipenv installation ruined the system pip by trying to install its own. I thought the --user flag would have avoided that but anyways am stuck, any help appreciated.

Best Answer

If pipenv installed successfully there will be a short notice like this:

The scripts pipenv and pipenv-resolver are installed in '~/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

According to the documentation:

This does a user installation to prevent breaking any system-wide packages. If pipenv isn’t available in your shell after installation, you’ll need to add the user base’s binary directory to your PATH.

On Linux and macOS you can find the user base binary directory by running python -m site --user-base and adding bin to the end. For example, this will typically print ~/.local (with ~ expanded to the absolute path to your home directory) so you’ll need to add ~/.local/bin to your PATH. You can set your PATH permanently by modifying ~/.profile.

Follow this:

  • Open ~/.profile file.
  • Check if ~/.local/bin path exist in that file.
  • If not add these following lines
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
  • Run bash --login for login mode because ~/.profile is executed for login shells.
  • Optionally, add those lines in ~/.bashrc for non login shell.

Related Question