Virtual Environment Setup

🌎 Welcome to virtual environments! If you’ve ever installed a Python package only to find it breaks another project, you’re not alone. Virtual environments let you isolate dependencies for each project so they don’t interfere with each other. Let’s explore what they are, when to use them, and how to set one up.

What is a Virtual Environment? 🌎

A virtual environment is a self-contained directory that keeps track of its own Python version and installed packages. This means you can:

  • âś… Avoid version conflicts between projects.
  • âś… Work on multiple projects with different requirements.
  • âś… Keep your global Python installation clean.
  • âś… Recreate the same setup on another machine.

Think of it as a sandbox where your project lives safely.

When Should I Use a Virtual Environment?

You should consider using a virtual environment when:

  • You’re starting a new project with its own dependencies.
  • You need different versions of libraries across projects.
  • You’re collaborating and want a reproducible environment.

In short - Always use one for each project.

Creating & Activating a Virtual Environment

The first step in using a virtual environment is to create one. After creating it, you need to activate it so your project uses the correct Python packages.

Python comes with a built-in tool called venv that creates a lightweight virtual environment. This is the simplest way to get started.

# Create a virtual environment
python -m venv myenv  

# Activate it (Mac/Linux)
source venv/bin/activate  

# Activate it (Windows)
venv\Scripts\activate  

# Deactivate when finished
deactivate

Once activated, your terminal will show (myenv) at the start of the line, which means you’re working inside the virtual environment.

Anaconda is a popular tool for Data Science. It can manage both Python packages and non-Python libraries (like R, C libraries, etc.), making it more powerful than venv. If you haven’t installed Anaconda yet, visit our Data Science Setup.

# Create a new conda environment
conda create -n myenv python=3.11  

# Activate the environment
conda activate myenv  

# Deactivate when finished
conda deactivate

Conda is especially useful if your project needs scientific libraries like NumPy, pandas, or TensorFlow, since it can handle complex dependencies easily.

Virtual Environment in Different IDE’s

IDEs let you choose which virtual environment to use so that running your code in the editor uses the right packages. If you haven’t installed an IDE yet, visit our Data Science Setup.

In VS Code, you need to tell the editor which Python interpreter (environment) to use. This ensures your code runs with the packages installed in your venv or conda environment.

  • Use the Command Palette (Ctrl+Shift+P on Windows / Cmd+Shift+P on Mac).
  • Search for Python: Select Interpreter and choose the one inside your venv.
  • Your environment will be used for running and debugging code.

Learn more from VS Code documentation

PyCharm makes it easy to connect a project to a virtual environment so the editor knows which packages to use when running your code.

  • Go to Settings → Project → Python Interpreter.
  • Click the ⚙️ icon → Add Interpreter → Virtualenv Environment.
  • Point PyCharm to your venv folder.

Learn more from PyCharm documentation

Managing Dependencies with requirements.txt

A requirements.txt is a text file that keeps track of all your project’s dependencies.

To Create One:

Use this command to list all the packages currently installed in your environment and save them into a text file.

pip freeze > requirements.txt

To Install From One:

If you share your project with others, they can use this file to install the exact same packages on their computer.

pip install -r requirements.txt

This makes your project reproducible so it works the same way across different machines.

requirements.txt Best Practice

Always include a requirements.txt file in your project’s GitHub repository and mention it in the README.md file.