1

I have installed virtualenv with sudo apt-get install virtualenv. It stands in this tutorial that you need to use your env/bin/ folder's own pip or pip3 command to install modules in the virtual environment, and it is explicitly stated that you don't use sudo to do this.

Unfortunately that doesn't work for me, I get a PermissionError when I try doing that. But using sudo before pip3 will install the module globally, so I won't get to take advantage of having a virtual environment at all.

E.g. when in my env/bin/ folder, I run pip3 install pytest:

Collecting pytest
  Downloading pytest-3.0.7-py2.py3-none-any.whl (172kB)
    100% |████████████████████████████████| 174kB 1.2MB/s 
Collecting py>=1.4.29 (from pytest)
  Downloading py-1.4.33-py2.py3-none-any.whl (83kB)
    100% |████████████████████████████████| 92kB 2.1MB/s 
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from pytest)
Installing collected packages: py, pytest
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.5/dist-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python3.5/dist-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/usr/local/lib/python3.5/dist-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python3.5/dist-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python3.5/dist-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python3.5/dist-packages/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/usr/local/lib/python3.5/dist-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/usr/lib/python3.5/os.py", line 241, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.5/dist-packages/py-1.4.33.dist-info'

How do I get started using virtualenv on Ubuntu 16.04 in less painful way?

5
  • Did you activate the virtual environment?
    – edwinksl
    May 12, 2017 at 15:35
  • I have tried running env/bin/activate in any way I could think of: with ./activate, or sudo activate. Nothing worked.
    – imrek
    May 12, 2017 at 15:41
  • 1
    You are supposed to source it. The command to do that is mentioned in the link you gave: source env/bin/activate.
    – edwinksl
    May 12, 2017 at 15:42
  • While that's true and I thank for pointing that out, the blog post only mentions the activation with source in the section coming after demonstrating the basic usage of virtualenv. That's where I got tripped off.
    – imrek
    May 12, 2017 at 15:48
  • After activating the virtual environment, are you able to install pytest?
    – edwinksl
    May 12, 2017 at 16:57

1 Answer 1

2

After you create the virtual environment, you need to activate it:

$ virtualenv myenv
$ source myenv/bin/activate
(myenv) $ pip install pytest

or for Python 3:

$ virtualenv -p `which python3` myenv
$ source myenv/bin/activate
(myenv) $ pip3 install pytest

When you are finished using the environment, you can deactivate it like so:

$ deactivate

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .