pythonpipuser-accounts

How can I fix permission errors from Pip?


When I try to install Python packages on my Mac at home, I frequently get permission errors from attempts to write to log files or to the site-packages directory, like so:

Command /usr/bin/python -c "import setuptools;__file__='/Users/markwalker/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/tq/hy1fz_4j27v6rstzzw4vymnr0000gp/T/pip-k6f2FU-record/install-record.txt failed with error code 1 in /Users/markwalker/build/pycrypto
Storing complete log in /Users/markwalker/Library/Logs/pip.log
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 8, in <module>
    load_entry_point('pip==1.1', 'console_scripts', 'pip')()
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main
    return command.main(args[1:], options)
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main
    log_fp = open_logfile(log_fn, 'w')
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile
    log_fp = open(filename, mode)
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

I only want the package to be installed under my current user account. How do I resolve the permission errors? Should I use sudo anyway?


Solution

  • 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.