A simple guide to creating a virtual environment in Python for Windows and Mac

Juan Díaz
Data Cat
Published in
3 min readFeb 22, 2020

--

Virtual environments can be a useful tool if we want to separate our Python projects and make them more robust to changes on dependencies or versions. The idea is to create an isolated environment of python that contains all that you need for your project. Having said that, this article has the intention to show you in simple words how you can create a virtual environment for Windows and Mac in Python using the package virtualenv.

All this process is going to be performed using the Shell of your computer (Terminal on Mac, Command Prompt on windows, PowerShell or any interface that you want to use).

Photo by Chris Ried on Unsplash

Installation

First of all, make sure you have installed the Python package manager pip. For this, simply type the following commands on the Shell depending on your system.

[WINDOWS] > py -m pip --version
[MAC] > python3 -m pip --version

This will show you whether or not pip is installed by showing you the version and the path where pip is located on your computer.

Once you have pip installed, we can proceed to install the python package virtualenv using the following command.

[WINDOWS] > py -m pip install --user virtualenv
[MAC] > python 3 -m pip install --user virtualenv

And that’s pretty much what we need in order to start creating your first virtual environment.

Create an environment

1. Go to your project folder. The environment that we want to create is going to be located in any specific path where your project is located in your machine. For this reason, the first thing is to go to your path where you have or want to create your project. Use the following command using your own path if you already created a folder

[WINDOWS] > cd "C:\Users\username\Documents"
[MAC] > cd /Users/username/Documents/

2. Create a virtual environment. Once inside your project folder, run the following command to create the environment on it. Here, I’m giving it the name _env. But off course you can change it to any name you prefer.

[WINDOWS] > py -m venv _env
[MAC] > python3 -m venv _env

3. Activate the virtual environment. Now that we created the environment it’s time to activate it. For that, we need to call the source where the activation file is located.

[WINDOWS] > .\_env\Scripts\activate
[MAC] > source _env/bin/activate

Done! That’s pretty much all we need to create an activate an environment. That easy. Now you can start installing your dependencies inside this environment. If you want to deactivate the current environment you just have to type deactivate and it will close.

Saving your dependencies into a text file

Once you have your project ready, you probably will need to have a list of all the dependencies and versions that your project is using in order to install a new environment in another machine. To achieve that, you can use the following command in order to save it into a text file called requirements.

pip freeze > requirements.txt

This text file contains all you need to reproduce your project in other environments and to prevent conflicts with dependencies, versions of packages and other problems that may cause that your code will not run properly under other conditions.

I hope you liked this short explanation and if you did, don’t forget to clap and subscribe to Data Cat to stay tuned for more Data Science topics.

--

--

Juan Díaz
Data Cat

M.Sc Data Science with more than 4 years of experience as data analyst in financial sector. I love traveling and programming!