Is it acceptable and safe to run pip install under sudo?

后端 未结 6 908
时光说笑
时光说笑 2020-11-22 14:29

I\'ve started to use my Mac to install Python packages in the same way I do with my Windows PC at work; however on my Mac I\'ve come across frequent permission denied

相关标签:
6条回答
  • 2020-11-22 14:38

    Your original problem is that pip cannot write the logs to the folder.

    IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'
    

    You need to cd into a folder in which the process invoked can write like /tmp so a cd /tmp and re invoking the command will probably work but is not what you want.

    BUT actually for this particular case (you not wanting to use sudo for installing python packages) and no need for global package installs you can use the --user flag like this :

    pip install --user <packagename>
    

    and it will work just fine.

    I assume you have a one user python python installation and do not want to bother with reading about virtualenv (which is not very userfriendly) or pipenv.

    As some people in the comments section have pointed out the next approach is not a very good idea unless you do not know what to do and got stuck:

    Another approach for global packages like in your case you want to do something like :

    chown -R $USER /Library/Python/2.7/site-packages/
    

    or more generally

    chown -R $USER <path to your global pip packages>
    
    0 讨论(0)
  • 2020-11-22 14:47

    Use a virtual environment:

    $ virtualenv myenv
    .. some output ..
    $ source myenv/bin/activate
    (myenv) $ pip install what-i-want
    

    You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation.

    It is best to use a virtual environment which isolates packages for you. That way you can play around without polluting the global python install.

    As a bonus, virtualenv does not need elevated permissions.

    0 讨论(0)
  • 2020-11-22 14:47

    I had a problem installing virtualenvwrapper after successfully installing virtualenv.

    My terminal complained after I did this:

    pip install virtualenvwrapper
    

    So, I unsuccessfully tried this (NOT RECOMMENDED):

    sudo pip install virtualenvwrapper
    

    Then, I successfully installed it with this:

    pip install --user virtualenvwrapper
    
    0 讨论(0)
  • 2020-11-22 14:53

    Because I had the same problem, I want to stress that actually the first comment by Brian Cain is the solution to the "IOError: [Errno 13]"-problem:

    If executed in the temp directory (cd /tmp), the IOError does not occur anymore if I run sudo pip install foo.

    0 讨论(0)
  • 2020-11-22 14:56

    It looks like your permissions are messed up. Type chown -R markwalker ~ in the Terminal and try pip again? Let me know if you're sorted.

    0 讨论(0)
  • 2020-11-22 14:58

    Is it acceptable & safe to run pip install under sudo?

    It's not safe and it's being frowned upon – see What are the risks of running 'sudo pip'? To install Python package in your home directory you don't need root privileges. See description of --user option to pip.

    0 讨论(0)
提交回复
热议问题