运行virtualenv时出现Errno 13权限被拒绝错误

6
在Mac OS X上使用brew安装的Python 2.7创建virtualenv时,我遇到了以下错误:
Could not install packages due to an EnvironmentError: 
  [Errno 13] Permission denied: '/lib'

完整输出:

▶ virtualenv ./virtualenv               
New python executable in /Users/alexharvey/git/home/sam-test/virtualenv/bin/python2.7
Also creating executable in /Users/alexharvey/git/home/sam-test/virtualenv/bin/python
Installing setuptools, pip, wheel...                    

  Complete output from command /Users/alexharvey/gi...ualenv/bin/python2.7 - setuptools pip wheel:
  Looking in links: /usr/local/lib/python2.7/site-packages, /usr/local/lib/python2.7/site-packages/virtualenv_support, /usr/local/lib/python2.7/site-packages/virtualen
v_support                                                                                                                                                             
Collecting setuptools                                                       
  Using cached https://files.pythonhosted.org/packages/37/06/754589caf971b0d2d48f151c2586f62902d93dc908e2fd9b9b9f6aa3c9dd/setuptools-40.6.3-py2.py3-none-any.whl      
Collecting pip                                                          
Collecting wheel                                                                                                                                                      
  Using cached https://files.pythonhosted.org/packages/ff/47/1dfa4795e24fd6f93d5d58602dd716c3f101cfd5a77cd9acbe519b44a0a9/wheel-0.32.3-py2.py3-none-any.whl           
Installing collected packages: setuptools, pip, wheel                                                                                                                 
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/lib'
Consider using the `--user` option or check the permissions.                                                                                                          

----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 11, in <module>
    sys.exit(main())                                                                                                                                                  
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 762, in main                                                                                      
    symlink=options.symlink,
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 1015, in create_environment
    install_wheel(to_install, py_executable, search_dirs, download=download)
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 968, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)                                                                                              
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 854, in call_subprocess
    raise OSError("Command {} failed with error code {}".format(cmd_desc, proc.returncode))
OSError: Command /Users/alexharvey/gi...ualenv/bin/python2.7 - setuptools pip wheel failed with error code 1

我看到这里经常会有类似的问题(例如这里),但是被赞的答案似乎推荐使用sudo,这显然是错误的。
请注意,我已经按照这个页面上的建议进行了操作:在Mac OS X上有效地使用Python
我该如何解决这个问题(不使用sudo)?

1
修复或删除您的~/.pydistutils.cfg文件。很可能是install_lib键的问题。 - phd
是的,关闭。非常感谢指针。 - Alex Harvey
2个回答

8

根据评论建议,存在一个自定义的Distutils配置文件问题。该文件显然可以位于以下三个地方之一(参考):

  • 系统: <prefix>/lib/pythonver/distutils/distutils.cfg
  • 个人: $HOME/.pydistutils.cfg
  • 本地: setup.cfg

与此同时, 我按照这里所述创建了一个setup.cfg 文件, 其内容如下:

[install]
prefix=

我已经不记得为什么要查看那个答案,或者为什么把那个文件放在那里,但是在我删除它之后,virtualenv又正常工作了,而且我再也没有遇到任何问题。


2
我曾经遇到两种情况:要么添加特殊的$HOME/.pydistutils.cfg文件可以解决virtualenv的问题,要么删除此文件即可解决问题。我不知道其中的原因;我会尝试这两种方法,希望其中之一有效。 - mnieber
我发现我有$HOME/.pydistutils.cfg文件,其中包含[install] prefix=。我将prefix=注释为# prefix=,这对我来说解决了问题。 - Emile

0

将任何东西安装到全局/lib中显然需要root权限。

您可以在安装任何软件包时添加--user参数,将模块安装到用户文件夹中,而无需使用sudo


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接