Virtual Environments | Dataplexa

Virtual Environments in Python

A virtual environment is a separate, isolated workspace where you can install Python packages without affecting your main system. It helps you avoid version conflicts and ensures each project has its own clean setup. Every professional Python developer uses virtual environments to manage dependencies safely.

Why Do We Need Virtual Environments?

Different projects may require different versions of libraries. Without isolation, installing packages globally can break another project. Virtual environments prevent this by keeping each project’s dependencies in its own folder.

Checking Your Python Location

Before creating a virtual environment, it’s useful to know which Python installation your system is using. This helps understand where the environment pulls resources from.

python --version
python -m site

Creating a Virtual Environment

Python provides a built-in module called venv to create virtual environments. The following command creates a new folder named env that stores the environment files.

python -m venv env

You can replace env with any name depending on your project.

Activating a Virtual Environment

After creating a virtual environment, you must activate it to start using it. Activation changes your terminal to use the environment’s Python and packages.

On Windows

env\Scripts\activate

On macOS / Linux

source env/bin/activate

When activated, your terminal usually shows the environment name at the beginning of the line.

Installing Packages Inside the Environment

Once activated, any package you install using pip stays inside the environment. This keeps your global system clean and avoids accidental version conflicts.

pip install numpy
pip install requests

Listing Installed Packages

You can view all the installed packages inside the virtual environment using pip list. This is helpful for reviewing dependencies or debugging issues.

pip list

Deactivating the Virtual Environment

When you’re done, deactivate the environment so your terminal returns to the system default Python. Deactivation is simple and works the same on all operating systems.

deactivate

Deleting a Virtual Environment

A virtual environment is just a folder. To delete it, simply remove the folder using your operating system’s file browser or a terminal command.

rm -r env

Freezing Dependencies (requirements.txt)

Projects often include a requirements.txt file to list all needed packages. This allows others to recreate the same environment easily.

Create requirements.txt

pip freeze > requirements.txt

Install from requirements.txt

pip install -r requirements.txt

This command installs the exact versions of packages used in the original project, ensuring complete consistency.


📝 Practice Exercises


Exercise 1

Create a new virtual environment called project_env.

Exercise 2

Activate the virtual environment and install pandas.

Exercise 3

Generate a requirements.txt file from your environment.

Exercise 4

Deactivate the virtual environment.


✅ Practice Answers


Answer 1

python -m venv project_env

Answer 2

project_env\Scripts\activate
pip install pandas

Answer 3

pip freeze > requirements.txt

Answer 4

deactivate