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
- Set up Ubuntu 18.04
- Run
pip install pipenv
orpip3 install pipenv
- Run
pip
orpip3
– 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)
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
to the profile.
Edit: Make sure you run
hash -r
or enter a new shell for this change to take effect.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 pathGetting the same problem on ubuntu 16.04.
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’sPATH
. Should be the default on Ubuntu, since 16.10. It is a good practice to have it since it enablespip install --user
behavior that is a perfectly valid use casepip 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 withpip3 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):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:
Traceback (most recent call last): File “/usr/bin/pip”, line 9, in <module> from pip import main ImportError: cannot import name main
import sys from pip._internal import main as _main if name == ‘main’: sys.exit(main())
Traceback (most recent call last): File “/usr/bin/pip3”, line 11, in <module> sys.exit(main()) NameError: name ‘main’ is not defined
import sys from pip._internal import main as _main if name == ‘main’: sys.exit(_main())
Traceback (most recent call last): File “/usr/bin/pip3”, line 11, in <module> sys.exit(main()) NameError: name ‘main’ is not defined
Ah I see the problem now. User modules take precedence over system ones, but
/usr/bin
is before$HOME/.local/bin
in youPATH
./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 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
.