Virtualenv

From Code Self Study Wiki
Jump to: navigation, search

Virtualenv and virtualenvwrapper:

Watch a video tutorial: https://youtu.be/0cGltFJDUFM

For Ubuntu:

$ sudo apt-get update
$ sudo apt-get install python-pip
$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

I also put the last two lines in ~/.bashrc and source ~/.bashrc in ~/.bash_profile in order to let the terminal know where to find it.. (GNU/Linux)

For Mac[edit]

Visit this tutorial.

Python 3[edit]

mkvirtualenv --no-site-packages --distribute -p /usr/bin/python3.3 name_of_virtualenv

Without virtualenv wrapper:

virtualenv -p python3 envname

Alias[edit]

Put this in your ~/.bashrc file:

alias mkve3='mkvirtualenv --python=`which python3`'

Then you can create new virtualenvs with the command mkve3 name_of_your_venv.

Commands[edit]

$ mkvirtualenv my_venv

For Python3 with the custom alias:

$ mkve3 my_venv

Remove virtualenv:

$ mkve3 my_venv

List virtualenvs:

$ lsvirtualenv

Activate a virtualenv---you can use TAB completion after typing workon:

$ workon my_venv

Deactivate a virtualenv:

$ deactivate

Upgrade all pip installs:

allvirtualenv pip install --upgrade pip

See more commands in the official docs.

Requirements.txt[edit]

Save to requirements.txt:

pip freeze > requirements.txt

Install into a virtualenv from pip:

pip install -r requirements.txt