Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting up and using Python3, Pip3, Virtualenv (for Python3) and Virtualenvwrapper (for Python3)
First install pip for Python2. Download the get-pip.py file from https://bootstrap.pypa.io/get-pip.py
$ cd <download location>
$ sudo -H python ./get-pip.py
Installing pip also installs Python3
To run Python3
$ python3
Install pip3 by just executing the same file as in the step above, but this time using Python3
$ sudo -H python3 ./get-pip.py
To install virtualenv via pip
$ sudo -H pip3 install virtualenv
{If you want to use virtualenv as is, read below, else skip to the next pair of braces}
Note that virtualenv installs to the python3 directory. For me it's:
$ /usr/local/share/python3/virtualenv
Create a virtualenvs directory to store all virtual environments
$ mkdir somewhere/virtualenvs
Make a new virtual environment with no packages
$ virtualenv somewhere/virtualenvs/<project-name> --no-site-packages
To use the virtual environment
$ cd somewhere/virtualenvs/<project-name>/bin
$ source activate
You are now using the virtual environment for <project-name>. To stop:
$ deactivate
{Continue installation}
To install virtualenvwrapper
$ sudo -H pip3 install virtualenvwrapper
Use the following command to find the location of Python3 on your system
$ which python3
Add the following lines to ~/.bashrc (or your own shell's initialisation file)
> VIRTUALENVWRAPPER_PYTHON='<Python3 location>'
> source /usr/local/bin/virtualenvwrapper.sh
> WORKON_HOME=$HOME/.virtualenvs
Run the following commands
$ mkdir ~/.virtualenvs
$ source ~/.bashrc
All the virtual environments created using virtualenvwrapper will now be stored in ./virtualenvs
To create new Python3 virtual environment
$ mkvirtualenv <project name>
To create new Python2 virtual environment
$ mkvirtualenv --python=python2 <project name>
The virtualenv will automatically activate after creation
Install packages local to the python3 virtualenv (and not global to the system) using
$ pip3 install <package-name>
Install packages local to the python2 virtualenv (and not global to the system) using
$ pip install <package-name>
To exit the virtualenv
$ deactivate

Please ignore this file - I'm currently thinking of rewriting the above snippet in a proper markdown guide updated for 2019, but this is still wip. Please refer to the guide above atm.

Python3, Pip3, Virtualenv and Virtualenvwrapper Setup Guide (2020)

This guide illustrates how I set up Python on my devices, which may or may not necessarily be the best way. I have detailed steps for both Linux and Windows since they somewhat differ from each other at the time of writing.

Skip to instructions for Windows

Linux

First install pip for Python2. Download the get-pip.py file from https://bootstrap.pypa.io/get-pip.py $ cd $ sudo -H python ./get-pip.py

Installing pip also installs Python3 To run Python3 $ python3

Install pip3 by just executing the same file as in the step above, but this time using Python3 $ sudo -H python3 ./get-pip.py

To install virtualenv via pip $ sudo -H pip3 install virtualenv

{If you want to use virtualenv as is, read below, else skip to the next pair of braces}

Note that virtualenv installs to the python3 directory. For me it's: $ /usr/local/share/python3/virtualenv

Create a virtualenvs directory to store all virtual environments $ mkdir somewhere/virtualenvs

Make a new virtual environment with no packages $ virtualenv somewhere/virtualenvs/ --no-site-packages

To use the virtual environment $ cd somewhere/virtualenvs//bin $ source activate

You are now using the virtual environment for . To stop: $ deactivate

{Continue installation}

To install virtualenvwrapper $ sudo -H pip3 install virtualenvwrapper

Use the following command to find the location of Python3 on your system $ which python3

Add the following lines to ~/.bashrc (or your own shell's initialisation file)

VIRTUALENVWRAPPER_PYTHON='' source /usr/local/bin/virtualenvwrapper.sh WORKON_HOME=$HOME/.virtualenvs

Run the following commands $ mkdir ~/.virtualenvs $ source ~/.bashrc

All the virtual environments created using virtualenvwrapper will now be stored in ./virtualenvs

To create new Python3 virtual environment $ mkvirtualenv

To create new Python2 virtual environment $ mkvirtualenv --python=python2

The virtualenv will automatically activate after creation

Install packages local to the python3 virtualenv (and not global to the system) using $ pip3 install

Install packages local to the python2 virtualenv (and not global to the system) using $ pip install

To exit the virtualenv $ deactivate

@johndpope

This comment has been minimized.

Copy link

johndpope commented May 1, 2017

@gabrielfern

This comment has been minimized.

Copy link

gabrielfern commented May 5, 2017

well done, thanks

@IamAdiSri

This comment has been minimized.

Copy link
Owner Author

IamAdiSri commented Jun 5, 2017

@johndpope That's interesting, but i still prefer sticking to virtualenvwrapper, as i like how it allows me to differentiate between a folder and a virtualenv. Also, with virtualenvwrapper, I can continue to work wherever I want to and with whichever v-env I need.

@gamesbook

This comment has been minimized.

Copy link

gamesbook commented Jul 8, 2017

$ sudo -H pip install pip3

Collecting pip3
  Could not find a version that satisfies the requirement pip3 (from versions: )
No matching distribution found for pip3

I had to run:

sudo apt install python3-pip
@dancwilliams

This comment has been minimized.

Copy link

dancwilliams commented Jul 28, 2017

Thanks for capturing this. Helped out a LOT today.

@ichiLamuchy

This comment has been minimized.

Copy link

ichiLamuchy commented Aug 17, 2017

Thanks! Though I have error
bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
bash: /Users/myName/.bash_profile: line 31: unexpected EOF while looking for matching `''
bash: /Users/myName/.bash_profile: line 37: syntax error: unexpected end of file
when I run source ~/.bash_profile.

I have been trying to set this up for a few days now.. Could anyone know why it can't see virtualenvwrapper.sh ?
I see the both site-package and python3 exist.

@lukaszjanyga

This comment has been minimized.

Copy link

lukaszjanyga commented Sep 11, 2017

@ichiLamuchy
You probably installed it to a different location - check under /usr/bin/virtualenvwrapper.sh

@ghocfef

This comment has been minimized.

Copy link

ghocfef commented Jan 20, 2018

This duplicates absolutely everything in the limited user home folder, in the virtual environment. Is there a reasonably simple way to limit what gets imported into the virtual environment? I'd also like to be able to install local copies of various applications (such as text editors) instead of using the system-wide ones and having them lose access to, for example, their configuration files.

@IamAdiSri

This comment has been minimized.

Copy link
Owner Author

IamAdiSri commented Jan 22, 2018

@ghocfef well the virtualenv library is basically only for python environments and its packages. There isn't a way to integrate the kind of functionality you're looking for in the current context, as far as i know.

@IamAdiSri

This comment has been minimized.

Copy link
Owner Author

IamAdiSri commented Jan 22, 2018

@ichiLamuchy Are you sure you've installed virtualenvwrapper correctly? If you are then the only way forward is to locate the virtualenvwrapper.sh shell script. Check the location @alvarg93 suggests too. There is a chance you just installed the library elsewhere.

@IamAdiSri

This comment has been minimized.

Copy link
Owner Author

IamAdiSri commented Jan 22, 2018

@gamesbook Yeah I've noticed that pip no longer let's you install pip3. However, using the default apt-repository may not be the best way to download pip3 as the version may be outdated. I've made amendments to the gist to now download the current release of pip3 too.

@azzamsa

This comment has been minimized.

Copy link

azzamsa commented Apr 17, 2018

@IamAdiSri You rocks. Thanks lot.

I have some difficulties to understand the proper way to use virtualenv wrapper. Then here you go.

Thanks.

@MYeager1967

This comment has been minimized.

Copy link

MYeager1967 commented Jul 1, 2018

Is it possible to run the GUI python3 program inside an existing virtual environment?

@asugai

This comment has been minimized.

Copy link

asugai commented Jul 12, 2018

I believe an export may be missing here:

> VIRTUALENVWRAPPER_PYTHON='<Python3 location>'

shouldn't it be

> export VIRTUALENVWRAPPER_PYTHON='<Python3 location>'

according to this section of the install guide: http://virtualenvwrapper.readthedocs.io/en/latest/install.html#python-interpreter-virtualenv-and-path

@IamAdiSri

This comment has been minimized.

Copy link
Owner Author

IamAdiSri commented Jul 22, 2018

@MYeager1967 As far as I know yes, python programs employing graphics libraries (such as Tkinter, and Pygame) will run in the virtual environment.

@IamAdiSri

This comment has been minimized.

Copy link
Owner Author

IamAdiSri commented Jul 22, 2018

@asugai You need export to make the the variable available to any child processes. Since I'm only ever running the virtualenvs in the shell and since every time a new shell instance is created it reloads the configuration, we don't need to add export at the beginning of the command inside the shell's config file.
However, if you were to declare the variable manually every time you opened the shell (which would be inconvenient) then yes you can append export to the beginning of the statement.

Relevant stackoverflow post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.