Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IamAdiSri/a379c36b70044725a85a1216e7ee9a46 to your computer and use it in GitHub Desktop.
Save IamAdiSri/a379c36b70044725a85a1216e7ee9a46 to your computer and use it in GitHub Desktop.
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
To get back into the virtualenv
$ workon <project-name>
@asugai
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
Copy link
Author

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

@IamAdiSri
Copy link
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

@francishunger
Copy link

Mac Users, who have propblems with virtualenvwrapper - It needs to be installed seperately like so: pip3 install virtualenvwrapper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment