Virtual Environment for Python 
Tags: python
Published: 2020-06-20 13:01:06

Below you learn why need a **Python virtual environment** and how are you can use it with Python 3 (<a href="https://python-commandments.org/python-2-vs-3/">Why Python 3?</a>).

You should know <a href="https://pythonprogramminglanguage.com/how-to-run/">how to run Python</a> before setting up a virtual environment. If you use PyCharm, it sets up a virtualenv automatically.

### What is Virtual Environment?

**Virtual environments** are particularly helpful if the program requires an individual environment, with a unique python version and specific module versions. It prevents package conflicts.

You can use **virtualenv** to create your isolated environment.

### Why do you need a virtual environment?

The **virtual environment** is a way to keep the python configuration isolated from other projects.

One user has multiple virtual environments on a single computer, one for eahch project. Different libraries can be incorporated in each virtual environment.

<img src="https://pythonbasics.org/wp-content/uploads/2018/03/virtualenv-modules.png" alt="virtual environment">

### How to create a virtual environment using Python3?

**Step1:** Install Virtual Environment    
    
    pip3 install virtualenv
    Collecting virtualenv
      Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
        100% |████████████████████████████████| 1.8MB 367kB/s
    Installing collected packages: virtualenv
    Successfully installed virtualenv-15.1.0
    

**Step2:** 
In python 3, you can use this command **'python3 –m venv /path/to/create/the/virtual/env'**.   
    
    -bash-4.2$ python3 -m venv test_venv
    -bash-4.2$ ls
    test_venv
    -bash-4.2$
    

**Step3:** Activate virtual environment    
    
    -bash-4.2$ source test_venv/bin/activate
    (test_venv) -bash-4.2$
    

**Step4:** Install the required libraries, using <a href="https://pythonbasics.org/how-to-use-pip-and-pypi/">pip</a> 

For the example below,we have used 'flask' library. Flask lets you build web apps, you can <a href="https://pythonspot.com/flask-web-app-with-python/">make all kinds of things</a> or even <a href="https://gitpress.io/u/1282/flask_matplotlib">plot</a>
    
    (test_venv) -bash-4.2$ pip3 install flask
    Collecting flask
      Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
        100% |████████████████████████████████| 102kB 3.8MB/s
    Collecting Jinja2>=2.10.1 (from flask)
      Using cached https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl
    Collecting click>=5.1 (from flask)
      Using cached https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl
    Collecting itsdangerous>=0.24 (from flask)
      Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
    Collecting Werkzeug>=0.15 (from flask)
      Using cached https://files.pythonhosted.org/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl
    Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
      Using cached https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
    Installing collected packages: MarkupSafe, Jinja2, click, itsdangerous, Werkzeug, flask
    Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0
     (test_venv) -bash-4.2$
    

More reading:
  
* <a href="https://gumroad.com/l/dcsp">Learn Python programming</a>
* <a href="https://virtualenv.pypa.io/en/stable/">More virtualenv</a>