virtualenv with Python 3 using venv

Python 3 comes with its own virtualenv built in, and it’s called venv. Using it is fairly simple and quick. Make sure you have installed Python 3 on your system. Also make sure you have installed the Python 3 development library for your distribution. For example, install python3-devel on Fedora and python3-dev on Ubuntu.

Then run the following command to create your virtualenv.

cg@codeghar [~] $ pyvenv ~/myvenv

Note: In Ubuntu 14.04 (attempted on 2014-04-17) you may get an error:

cg@codeghar [~] $ pyvenv-3.4 ~/myvenv
Error: Command '['/home/cg/myvenv/bin/python3.4', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

To work around it, make sure you use the --without-pip flag, like so pyvenv-3.4 ~/myvenv --without-pip.

If you do an ls you’ll notice that the python you used to create this virtualenv is copied into your virtualenv. Thus your virtualenv will continue to use its local copy. You now need to “activate” this virtualenv.

cg@codeghar [~] $ source ~/myvenv/bin/activate

Sidebar: At some point you’ll want to “deactivate” your virtualenv. To do this just run the deactivate command.

Next step is to install setuptools. At one point Distribute was set to replace/supersede setuptools but that’s not the case anymore. Don’t worry, we’ll still use pip but to first get pip we need setuptools.

cg@codeghar [~] $ cd ~/myvenv

cg@codeghar [~/myvenv] $ mkdir pypioffline

cg@codeghar [~/myvenv/pypioffline] $ cd pypioffline

The latest version of setup tools at the time of writing was 1.1.6 so we’ll download it to the temporary/optional directory pypioffline.

cg@codeghar [~/myvenv/pypioffline] $ curl -O

cg@codeghar [~/myvenv/pypioffline] $ tar xvzf setuptools-1.1.6.tar.gz

cg@codeghar [~/myvenv/pypioffline] $ cd setuptools-1.1.6

cg@codeghar [~/myvenv/pypioffline/setuptools-1.1.6] $ python

cg@codeghar [~/myvenv/pypioffline/setuptools-1.1.6] $ easy_install pip

That’s pretty much it.

venv — Creation of virtual environments

2 Responses to virtualenv with Python 3 using venv

  1. Adam Chapman says:

    Great guide! The only additional things I needed to do (Ubuntu 14.04) was:
    $ sudo apt-get install curl
    $ sudo apt-get install python3


  2. Pingback: Install Python 3.4, Django 1.7 on Ubuntu 14.04 in a venv Virtual Environment | Chapmandu, Adam's digital cesspool

%d bloggers like this: