0

Command pip doesn't work without sudo

I was happily using pip with sudo but I have read here that I shouldn't do that.

However, when I try to use pip without sudo I get this:

[souto@~]$ pip install <package-name>
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

I have tried James Sullivan's tip so my .bash_history file has the content below but to no avail.

[souto@~]$ cat .bash_aliases
alias pip=/usr/bin/pip

I am working on WSL, in case this is relevant.

msoutopico
  • 357
  • 3
  • 15
  • 2
    The question you link to has an example which links to `virtualenv` which is really the answer here. Like many questions on Ask Ubuntu, the answers are not exactly precise or analytical there (nor is the question in this case). – tripleee Aug 21 '19 at 11:36
  • Did you add your pip in your PATH ? – hissroth Aug 21 '19 at 11:38
  • Thanks, triplee. However, I would like to use python and pip without a `virtualenv` (at least for the time being, I don't need that -- I'm working on my own machine and I'm the only user). – msoutopico Aug 21 '19 at 11:41
  • No, hissroth. I don't think I have done that (well, I'm not sure). How do I do that? Shall I just do `export PATH=/usr/bin/pip:$PATH` or is there anything else needed? – msoutopico Aug 21 '19 at 11:46
  • @tripleee, does your comment (and marking my question as duplicate) mean that creating a virtual environment (`virtualenv`) is the only way to run `pip` without `sudo`? – msoutopico Aug 21 '19 at 17:55
  • The duplicate contains alternatives, such as `pip install --user`. – tripleee Aug 22 '19 at 03:51
  • You add directories to your `PATH`. If your `pip` is in `/usr/bin/pip` then `/usr/bin` is what needs to be on your `PATH` (but it should already be there, so no need to add it). – tripleee Aug 22 '19 at 03:52
  • `virtualenv` also shields you from yourself. If you are only ever going to run a single Python script on your computer then by all means regard it as a throwaway OS and install everything you need with `sudo`. If that is not your scenario, you are probably going to want to use more than one set of libraries, and so `virtualenv` is useful hygiene. – tripleee Aug 22 '19 at 03:53
  • Thanks, @tripleee. I thought `virtualenv` was a complication I can live without, but I'll give it a chance. This is only for python2 -- I don't need to use `sudo` with python3, so I don't need to use `virtualenv` either. Thanks. – msoutopico Aug 23 '19 at 14:01
  • I found the answer to my question here: https://askubuntu.com/a/1106328/230240. @tripleee, I don't think my question is a duplicate (I'm raising a different issue than the one you have linked to). I appreciate the tips to use `virtualenv` and I see the value of it, but probably that should be a choice and that's not the answer to my question (it didn't fix my issue). – msoutopico Sep 14 '19 at 23:16
  • Thanks for following up; I added a second duplicate. – tripleee Sep 15 '19 at 07:40

0 Answers0