无法在虚拟环境中本地安装 PyMySQL [Errno 13] 权限被拒绝

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

我正在尝试安全安装 PyMySQL。我读过很多关于为什么应该避免 sudo pip install 的很好的论点,但有时似乎无法摆脱。

有没有办法在 OSX 中本地安装 PyMySQL 而无需使用 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'

要在虚拟环境中安装 运行 此命令。

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

通常 - 如果您无法在虚拟环境中使用 pip 安装 PyMySQL(即使您使用 --user 选项),您可以尝试...

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

这对我来说仍然是一个奇怪的情况。欢迎任何关于为什么我必须明确说明我的 python 目录的评论。我认为使用虚拟环境的原因是 pip 将始终选择安装在那里。