Complete Guide to Virtual Environments (Virtualenv) in Python

Mahdi Ahmadi - Feb 17 - - Dev Community

1. What is a Virtual Environment in Python?

When developing multiple projects with Python, each project may require different verzsions of libraries. This is where Virtual Environment (Virtualenv) comes to the rescue!

A virtual environment is an isolated space for installing libraries and packages for a specific project without affecting your main system.

2. Why Should You Use Virtualenv?

  • Avoid version conflicts: If different projects require different versions of the same library, conflicts may arise without a virtual environment.

  • Project isolation: Each project has its own set of dependencies, ensuring stability.

  • Portability: You can easily recreate the project environment on another system using a requirements.txt file.

  • Increased security: Installing packages in an isolated environment prevents unintended changes to system files.

3. Installing and Using Virtualenv

  • Installing Virtualenv on Windows, Linux, and macOS

If Virtualenv is not already installed, you can install it using the following command:

pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Check installation:

virtualenv --version
Enter fullscreen mode Exit fullscreen mode
  • Creating a Virtual Environment

To create a virtual environment in your project directory, run:

virtualenv venv
Enter fullscreen mode Exit fullscreen mode

venv is the name of the folder where the virtual environment will be created. You can use any name you prefer.

  • Activating the Virtual Environment

The activation process depends on your operating system:
On Windows (CMD or PowerShell):

venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Or for PowerShell:

venv\Scripts\Activate.ps1
Enter fullscreen mode Exit fullscreen mode

On Linux and macOS:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Once activated, you will see the virtual environment name in the terminal prompt:
(venv) user@computer:~$

  • Installing Packages in the Virtual Environment

After activation, you can install project dependencies using:

pip install django
Enter fullscreen mode Exit fullscreen mode
  • Deactivating the Virtual Environment

To deactivate the virtual environment, simply run:

deactivate
Enter fullscreen mode Exit fullscreen mode

4. Saving and Recreating the Virtual Environment with requirements.txt

To save the list of installed packages in the virtual environment, use:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

To recreate the same environment on another system:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

5. Conclusion

  • Virtualenv helps you run Python projects in an isolated and conflict-free manner.

  • You can install it with pip install virtualenv.

  • Create and activate a virtual environment with venv.

  • Use requirements.txt to store and restore dependencies.

Thanks for readingโค๏ธ
I hope this guide helps you understand and use virtual environments effectively. If you have any questions or suggestions, feel free to leave a comment!

.