Search code examples
macospermissionspipvirtualenvpymysql

Cannot install PyMySQL locally within a virtual environment [Errno 13] Permission denied


I'm trying to safely install PyMySQL. I've read many good arguments about why sudo pip install should be avoided, but sometimes it just seems impossible to get away from.

Is there a way to install PyMySQL locally in OSX without using sudo?

(VirtualEnv) : ^.^ :pip install --no-cache-dir --user PyMySQL
Collecting PyMySQL
  Downloading PyMySQL-0.8.0-py2.py3-none-any.whl (83kB)
    100% |████████████████████████████████| 92kB 1.8MB/s 
Installing collected packages: PyMySQL
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 342, in run
prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Python/2.7/site-packages/pip/wheel.py", line 247, in move_wheel_files
    prefix=prefix,
  File "/Library/Python/2.7/site-packages/pip/locations.py", line 153, in distutils_scheme
    i.finalize_options()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 346, in finalize_options
    self.create_home_path()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 565, in create_home_path
    os.makedirs(path, 0700)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Users/TomNason/Library/Python/2.7'

Solution

  • To install within a virtual env run this command.

    pip install --install-option="--prefix=/Users/TomNason/VirtualEnv/lib/python2.7" PyMySQL

    Generally- if you cannot install PyMySQL with pip inside a virtual environment (even if you are using the --user option) you can try...

    pip install --install-option="--prefix=PATH_TO_YOUR_VIRTUAL_ENV_DIR/lib/PYTHON_VERSION" PyMySQL

    This is still a curious situation for me. Any comments on why I would have to explicitely state my python directory are welcome. I thought that the reason for using a virtual environment was that pip will always choose to install there.