---
# System prepended metadata

title: virtualenv

---

# virtualenv
## Table of content
* [Installation](#Installation)
* [Create a python virtual environment](#Create-a-python-virtual-environment)
* [Delete virtual environment](#Delete-virtual-environment)
## Installation
* `pip install virtualenv` or `pip3 install virtualenv`

## Create a python virtual environment
### Step 1: Create a virtual environment
* Option 1 --- Create with default python binary: 
`$ virtualenv [env_name]`

    > Ex. `$ virtualenv venv `
    > This will create a directory that contains the virtual environment

* Option 2 --- Create with specific python binary: 
    `$ virtualenv -p [path_to_python_exe_file] [env_name]`
    
    > Ex. `$ virtualenv -p /usr/bin/python2.7 venv`


### Step 2: Activate the environment

`. env_name/bin/activate`
> Ex. `. venv/bin/activate`


### Deactivate the environment
`$ deactivate`

## Delete virtual environment
`rm -rf [virtualenv_dir]`
> Ex. `rm -rf venv`

## Create requirement.txt
* `$ pip freeze > requirements.txt`

## References
* [How to install virtualenv](https://gist.github.com/Geoyi/d9fab4f609e9f75941946be45000632b)
* [Python虛擬環境virtualenv的安裝與使用詳解](https://codertw.com/程式語言/365510/)