6

When trying to create a virtualenv using a brew-installed Python 2.7 on Mac OS X I get the following error:

Could not install packages due to an EnvironmentError: 
  [Errno 13] Permission denied: '/lib'

Full output:

▶ 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

I see that questions like this have been asked here frequently before (e.g. here) and yet upvoted answers seem to recommend using sudo, which is obviously wrong.

Note I have followed recommendations at this page here: Using python effectively on Mac OS X.

How can I fix this (without using sudo)?

phd
  • 82,685
  • 13
  • 120
  • 165
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • 1
    Fix or remove your [`~/.pydistutils.cfg`](https://stackoverflow.com/questions/25914145/whats-pydistutils-cfg-for). Most probably `install_lib` key. – phd Dec 24 '18 at 16:30
  • Yes, close. Thanks very much for the pointer. – Alex Harvey Dec 25 '18 at 14:14

2 Answers2

8

As suggested in a comment, there was an issue with a custom Distutils config file. This file can apparently be in one of three places (ref):

  • system: <prefix>/lib/pythonver/distutils/distutils.cfg
  • personal: $HOME/.pydistutils.cfg
  • local: setup.cfg

I had meanwhile created a setup.cfg according to this SO answer here with content:

[install]
prefix=

I can no longer remember why I had consulted that answer, or why I had put that file there, but after I removed it, virtualenv worked fine again, and I'm not having any more issues.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • 2
    I've experienced both cases: either adding the special $HOME/.pydistutils.cfg file fixed a problem with virtualenv, or removing this file fixed the problem. I don't know the underlying reasons; I will just try both solutions and hope one of them works. – mnieber Jun 20 '19 at 09:26
  • I found I had `$HOME/.pydistutils.cfg` with `[install] prefix=`. I commented out `prefix=` to `# prefix=` and that solved it for me. – Emile Oct 27 '19 at 04:46
0

Installing anything into global /lib obviously requires root privileges.

You can install modules to your user's folder without using sudo, just add --user argument when installing any package.

grapes
  • 8,185
  • 1
  • 19
  • 31