pipenv: Cannot install under Ubuntu 18.04, breaks pip ("ImportError: cannot import name main")

I want to install pipenv under Ubuntu 18.04. When I do so, it breaks pip / pip3.

Expected result

Installed and working version of pipenv.

Actual result

pip / pip3 are broken, depending on whether I wanted to install pipenv though pip or pip3.

➜ pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main
Steps to replicate
  1. Set up Ubuntu 18.04
  2. Run pip install pipenv or pip3 install pipenv
  3. Run pip or pip3 – the error is printed, and pip / pip3 do not work anymore.

In order to fix the issue I have to run:

sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall
sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

But I cannot install pipenv using pip. Installation through apt does not work because there is no PPA available…


Solution

See the solution below; you need to have

export PATH="${HOME}/.local/bin:$PATH"

in your shell configuration. If the path is not there, it won’t work.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 92
  • Comments: 30 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Ah, I see now what the problem is, thanks. Never thought that the path could influence this, which is why it didn’t occur to me to include it in the issue description. Sorry about that, and thanks for your help.

What fixed it for me was adding

export PATH="${HOME}/.local/bin:$PATH"

to the profile.

Edit: Make sure you run hash -r or enter a new shell for this change to take effect.

I don’t think there is much we can do to independently tackle it

Perhaps there could be some more detailed installation instructions or caveats? I’m not too experienced with how pip works, but I might have read a note about path issues. But I guess that the ecosystem of different package managers and distributions is too complex for a simple rule…

edit: @slhck 's solution solved it. Need to have ~/.local/bin in your path

Getting the same problem on ubuntu 16.04.

$ sudo apt install python3-pip
$ pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
$ python3 -m pip install --user pipenv
$ pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

# revert back and fix pip
$ sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

Here’s a nice step-by-step guide I used successfully on Ubuntu 18.04: https://phoikoi.io/2018/04/03/bootstrap-pipenv-debian.html

it is pretty stable when user has :

  • ˜/.local/bin at the begining of user’s PATH. Should be the default on Ubuntu, since 16.10. It is a good practice to have it since it enables pip install --user behavior that is a perfectly valid use case
  • user should always use pip install --user. Messing with the system’s pip is really bad in every distribution, even the weird “dist-packages/site-packages” folders in debian do not protect against mistakes from user.

We have ensured this behavor in all our ubuntu user installs (organisation of more that 1000 installs of various ubuntu version), and the transition to pip10 worked like a charm.

What do we have to do?

@slhck it’s actually so frustrating that there was this xkcd specifically about python environments and sudo and package manager installations a few days ago

I realized the issue is closed, but I’m posting with the hope this might help avoid modifications to PATH. I ran in to this today when setting up a new Ubuntu 18.04 machine and was able to resolve it without PATH modifications, though I did reboot (I’m fairly certain log out/in will work, but didn’t verify).

After installing pip3 via python3-pip and pipenv with pip3 install --user pipenv I received the subject error. I was about to use the workaround from @slhck when I noticed the following in ~/.profile (stock, I have no modifications):

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

Curious, I rebooted and sure enough, the issue was resolved as my .local/bin was now at the beginning of my PATH and pip3 worked again.

I was facing the same issue on Ubuntu 18 and Python 3.6

Below are the steps i followed:

  1. Firstly i was receiving the error :

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

  1. I modified /user/bin/pip file to:

import sys from pip._internal import main as _main if name == ‘main’: sys.exit(main())

  1. Then, it started giving me this error:

Traceback (most recent call last): File “/usr/bin/pip3”, line 11, in <module> sys.exit(main()) NameError: name ‘main’ is not defined

  1. I modified /usr/bin/pip3 to:

import sys from pip._internal import main as _main if name == ‘main’: sys.exit(_main())

  1. Then i started getting the error:

Traceback (most recent call last): File “/usr/bin/pip3”, line 11, in <module> sys.exit(main()) NameError: name ‘main’ is not defined

  1. I renamed main() to _main(), and voila … it worked !!! 😃 😃

Ah I see the problem now. User modules take precedence over system ones, but /usr/bin is before $HOME/.local/bin in you PATH. /usr/bin/python tries to import the system pip installation (which is still 9.x), but ended up finding the user installation (which is 10.x). What a mess.

I want to install pipenv under Ubuntu 18.04. When I do so, it breaks pip / pip3.

Expected result

Installed and working version of pipenv.

Actual result

pip / pip3 are broken, depending on whether I wanted to install pipenv though pip or pip3.

➜ pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main
Steps to replicate
1. Set up Ubuntu 18.04

2. Run `pip install pipenv` or `pip3 install pipenv`

3. Run `pip` or `pip3` – the error is printed, and pip / pip3 do not work anymore.

In order to fix the issue I have to run:

sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall
sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

But I cannot install pipenv using pip. Installation through apt does not work because there is no PPA available…

i have 3 pip in my ubuntu 18 pip, pip3 and pip3.6 pip is for 2.7 pip3 is for 3.5 and pip3.6 for 3.6 now which pip shows the file location in .local/bin . i removed the file pip and pip3 from here. now which pip3 shows me the /usr/bin/pip3. run the command sudo nano /usr/bin/pip3 change the first line for interpreter as !#/usr/bin/python3 to #/usr/bin/python3.5. works for me. all my pips work. i hope this helps

While that may have solved the problem for you, this is quite bad advice. You should not manually modify system files. Please have a look at my earlier comment for a solution.

@thernstig Yes, that’s correct – I added the necessary hash -r command to my solution above, which I forgot to explicitly mention.

There’s also https://github.com/pypa/python-packaging-user-guide/issues/396, which discusses the question of whether or not we might be able to come up with the checklist or troubleshooting script to help folks identify and resolve potential issues in their environment. I’ll make a note there about the potential for PATH/sys.path ordering conflicts.

#2095 is the same, but I want to keep this open because I think there should be a more robust solution than to depend on the user having a friendly PATH.