-1

I'm trying to create a virtualenv, but I'm getting a Permission denied error. I'm using the following code

python2.7 -m virtualenv -p `which python2.7` test

Which gives this error message:

--edit--

I get a permission denied error

Traceback (most recent call last):
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/wheel.py", line 287, in clobber
    ensure_dir(dest)  # common for the 'include' path
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/home/marco/development/openplus/lambda/resizeTestSam/test/lib/python2.7/os.py", line 152, in makedirs
    makedirs(head, mode)
  File "/home/marco/development/openplus/lambda/resizeTestSam/test/lib/python2.7/os.py", line 159, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/lib/python2.7'

and a final error the operation failed

...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv.py", line 2328, in <module>
    main()
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv.py", line 945, in create_environment
    download=download,
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv.py", line 901, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/home/marco/.local/lib/python2.7/site-packages/virtualenv.py", line 797, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /home/marco/developm...m/test/bin/python2.7 - setuptools pip wheel failed with error code 2

To debug the error in os.py, I've added a print statement, it looks like the script tries to make the directories /lib/python2.7 and /lib/python2.7/site-packages. I don't understand why virtualenv tries to create these directories there and not in my current directory.

I've installed virtualenv without sudo, so I don't expect it to create directories in a path that is owned by root.

In the end only the python executable is copied to the 'test' directory, but not setuptools, pip and wheels.

Is there a way to avoid virtualenv from accessing root owned directories?

--update--

using python3 with venv works fine, the problem is with virtualenv specifically.

Executing virtualenv from my home directory give no error.

araker
  • 31
  • 2
  • 7
  • 1
    Please post error message as code instead of printscreen. – Kevin Lemaire Jan 22 '18 at 16:50
  • Have you checked the following: https://stackoverflow.com/questions/35961371/pip-install-django-error/39618607#39618607 https://stackoverflow.com/questions/8951862/installing-python-and-distribute-on-windows-7-gives-writing-failed-permissi ? – Hamoudaq Jan 22 '18 at 16:57
  • Possible duplicate of [How to avoid "Permission denied" when using pip with virtualenv](https://stackoverflow.com/questions/19471972/how-to-avoid-permission-denied-when-using-pip-with-virtualenv) – Hamoudaq Jan 22 '18 at 20:19
  • Thanks for the references, but they're not fully applicable to this problem. The other users have problems with using pip in virtualenv. My problem is in the step before, when creating the virtualenv. Since it's a rights problem I could use sudo for installing the virtualenv and sudo for pip, but eventually this code will be used in a script that needs to run without root permissions, so that's not an option. – araker Jan 23 '18 at 09:14

2 Answers2

1

Looks like you're trying to create the virtualenv test within /lib/python2.7/site-packages, which, by all rights, you shouldn't have write access to.

Try running cd ~ first, to create the venv in your home directory.

Adam Barnes
  • 2,922
  • 21
  • 27
  • Executing it from the home directory works, so that's a nice workaround. I'm executing the command from my project directory (a subdirectory of my home directory), so I'm wondering why it doesn't work from there. – araker Jan 23 '18 at 13:35
  • Do `ls -lash` in the project directory. My guess is that the `test` directory is already there in the way, or `.` is owned by someone other than you. The key thing to take away is to _definitely not_ use `sudo` for this. – Adam Barnes Jan 23 '18 at 13:39
  • I delete the directory every time before I use the command. After running the command, I see that the directory is owned by my current user. – araker Jan 23 '18 at 13:47
  • So `rm -rf test && ls` shows `test` still in existence? If not, does `virtualenv [...] test` then work? – Adam Barnes Jan 23 '18 at 13:49
  • No the directory is removed. When running the command again, I still get the same error message. – araker Jan 23 '18 at 13:52
  • But that directory is owned by your user account, and is writable? – Adam Barnes Jan 23 '18 at 13:55
  • Too bad we can't chat (not enough rep points). The directory is writable and owned by my user account indeed. – araker Jan 23 '18 at 14:06
  • Then unfortunately I'm not sure what's wrong. My next steps from here would be to run the process up to its failure point, and poke around in the filesystem to see if I can find anything amiss. Perhaps install a python 3 interpreter, and use that for the venv instead to see if the same issue occurs. – Adam Barnes Jan 23 '18 at 14:09
  • Thanks for your help, I have a workaround now at least. Using python 3 and venv does work. The problem is with virtualenv specifically. – araker Jan 23 '18 at 14:18
-1

I had the same mysterious error; it turns out I had forgotten a setting in my $HOME/.pip/pip.conf that changed the prefix.

Simon
  • 1