From 0 to Senior in Python: How to set up a virtual environment for your Python project

Olof Baage
4 min readFeb 9, 2024

--

In order to use and manage specific versions of packages in your Python project, you need to set a virtual environment. In this article I walk you through the process.

Photo by Shamin Haky on Unsplash

Hi fellow coder! 👋 Awesome, that you decided to learn more about Python. 🤩 Here is, what I’m going to cover:

  • What is a virtual environment and why do we need it
  • How to install, uninstall and list packages with pip
  • Set up a virtual environment for your project
  • Create a dependency file
  • Use environment variables

What is a virtual environment and why do we need it

Python is globally installed on your system. The same goes for all the extra packages, that you install with pip. If needed you can install a specific version of a package. But a packages only can be installed in one version on your system. What, if you need different version for different projects? That is, were the virtual environment comes in. 👆

With a virtual environment you can isolate your project from Python’s base and install in that specific environment the packages you need for that project.

How to install, uninstall and list packages with pip

Before we jump right into how to set up the virtual environment, let’s take a look at the pip before. What is the pip?

pipis an abbreviation for Preferred Installer Program. It is Python’s package manager, like npm for node.js.

Install a package

python3 -m pip install package_name #Mac
py -m pip install package_name #Windows

Install a package with a specific version

python3 -m pip install package_name=1.19.2. #Mac
py -m pip install package_name=1.19.2. #Windows

Update a package

python3 -m pip install --upgrade package_name #Mac
py -m pip install -U package_name #Windows

Uninstall a package

python3 -m pip uninstall package_name #Mac
py -m pip uninstall package_name #Windows

List all installed packages

python3 -m pip list #Mac
py -m pip list #Windows

Set up a virtual environment for your project

Now, that you know how to manage packages with pip, we can go further and set up a virtual environment.

Let’s create a new, empty folder now for a tiny Python project. Give it any name you want and navigate with the terminal right into it.

The following command will create the virtual environment:

python3 -m venv .venv #Mac
py -m venv .venv #Windows

You should have a new folder, called .venv .now.

De/Activate the virtual environment

Since the folder structure on Mac and Windows is a little different, also this command varies.

source .venv/bin/activate #Mac
source .venv/Scripts/activate #Windows

After you hit enter you should see (.venv) in the terminal right before the path.

(.venv) xxx@xxx olofs-app %

To deactivate (stop) the virtual environment, run the following command in the terminal:

deactivate # the same for Mac and Windows

Congratulations! 👏 You just created your first virtual environment. That wasn’t that hard, was it?

Since you made it this far, let’s done something useful with it.

Create a dependency file

We usually do not share the virtual environment on GitHub. In order to let others know, what kind of packages we use, we can provide a requirements.txt.

For something I want to do in the last part of this tutorial, we need the package python-dotnev. So, why don’t we install that now.

If you take a look at the package list right now, it should be almost empty. Do you remember, how to look into the list of the installed packages? Let’s repeat it one more time. 😊

python3 -m list #Mac
py -m pip list #Windows

Now we install the package pyton-dotenv in our virtual environment.

python3 -m pip install python-dotenv #Mac
py -m pip install python-dotenv #Windows

If everything went well with the installation, and you check the packages list again, you now should the python-dotenv there.

Now, make sure that you are in the root folder of your project. With the following command we are going to create the requirements.txt:

python3 -m pip freeze -> requirements.txt #Mac
py -m pip freeze -> requirements.txt #Windows

After you hit enter, a .txt-file called requirements.txt should be created in your project root folder. Of course, you can give that file any name you want.

Use environment variables

Last but not least, let’s use our virtual environment to create a file for variables.

Create a new file, called .env. Open the file in your editor. Let’s add a variable.

API_KEY=hello,world

Of course, this is no API_KEY. But for the demonstration it’ll do. 😉

Make sure, to add the .env-file to you .gitignore-file, in case you work with GitHub. Those data should not be shared.

Now, let’s create a python file, e.g. app.py. and add some code so we can see how to work with the variables from the environment variable file.

import os
from dotenv import load_dotenv

load_dotenv()

print(os.getenv("API_KEY"));

# Expected Output: hello,world

How cool is that? 🤩 We can save sensitive data in an environment variable file and use them wherever we need them in our program.

If you learned something from this article, leave me clap, a comment or share this article to support me. I welcome you to follow me here or on LinkedIn.

Happy coding.

--

--

Olof Baage

I’m building the future. 👉 Student (Mechatronics - Robotics and Automation, B.Eng.) and passionate Developer (Python, C/C++, Java Script, SQL).