Python environments with Conda and virtualenv fast and easy
Tags: guide, python
Published: 2022-01-23 23:46:34

---
title: Python environments with Conda and virtualenv fast and easy
published: true
tags: Guide,Python
canonical_url: https://ramagg.com/virtual-env-windows-and-linux/
---

 ![How to create Python virtual environments on Windows and Linux](https://ramagg.com/content/images/2020/02/Python-virtual-env.png)

What's the point in using Virtual environments well the main benefits is that you can have the same package installed with different versions in the same computer/server.  
That's helpful if you have a bunch of projects that have the same packages but works with different versions of them.  
Also a venv can be in any folder you want, to use python env we need any python 3 version.

First of all we are going to separate the windows command from the linux commands using **(W)** for windows and **(L)** for Linux.

<!--kg-card-begin: markdown-->

Now we need to make sure we have the virtualenv package install: `pip3 install virtualenv`

<!--kg-card-end: markdown-->
## Creating the venv
<!--kg-card-begin: markdown-->

**Go to your terminal**  
Change dir to the folder of your env `cd name_of_env`  
(W): `python -m venv name_of_env`  
(L): `virtualenv name_of_env`

<!--kg-card-end: markdown-->

This will create venv in the selected folder  
Now to access the venv or rather to activate it:

<!--kg-card-begin: markdown-->

(W): `execute name_of_env/Scripts/activate.bat`  
(L): `source name_of_env/bin/activate`

<!--kg-card-end: markdown-->

To make sure we are in the venv we can see the terminal that we have the name of the env at the beginning of the line or doing:

<!--kg-card-begin: markdown-->

(W): `where python`  
(L): `which python`

<!--kg-card-end: markdown-->

This will show where python is executing and it should we in our env.

<!--kg-card-begin: markdown-->

Now if you execute `pip list` you will see that we don't have much installed in our environment but that was the point of all of this 😄

<!--kg-card-end: markdown--><!--kg-card-begin: markdown-->

If you want the current packages that you have in your env you can execute `pip freeze` and if you are wild, you can do `pip freeze > requirements.txt` that will save the output of the terminal into that file so you can have the same version of the packages  
To install the packages in other env or machine you can grab that file and do  
`pip install -r requirements.txt`

<!--kg-card-end: markdown-->

It's not a good practice to put files in your venv, so when you use one you should make the folder of the project and then a dedicated venv folder, so the  virtual env will be in project\_name/venv

## Deactivating and removing the env
<!--kg-card-begin: markdown-->

**To Deactivate the venv**  
(W): `execute deactivate`  
(L): `deactivate`

<!--kg-card-end: markdown-->

To remove the env we only need to delete the folder that the env it's in:

<!--kg-card-begin: markdown-->

(W): `rmdir folder_name /s`  
(L): `rm -rf folder_name`

<!--kg-card-end: markdown-->
## Bonus creating anaconda env

Conda it's a bundle of frameworks and libraries that we can also use to create env

<!--kg-card-begin: markdown-->

**Creating a conda environment**  
`conda create -n name_of_env python=3.6`

<!--kg-card-end: markdown-->

As you can see one of the advantages that conda gives is the ability to choose the version of python

<!--kg-card-begin: markdown-->

**To activate the env:**  
`conda activate name_of_env`  
**To see all the env:**  
`conda env list`  
**To deactivate:**  
`conda deactivate name_of_env`  
**To remove a conda env:**  
`conda env remove -n name_of_env`

<!--kg-card-end: markdown-->

That's it! if you have question or found an error talk to me on twitter [@ramgendeploy](https://twitter.com/ramgendeploy)