PythonicIT Blog

Deep dives on automation, security, architecture, and practical implementation lessons from real client work.

Creating a virtual environment in Python


When working on Python projects, it's a good practice to create a virtual environment for each project. A virtual environment is an isolated environment that allows you to install dependencies and manage packages for a specific project without affecting the system-wide Python installation. In other languages, this is similar to creating a virtual environment in Node.js or a virtual environment in Ruby.

Why Use Virtual Environments?

There are several reasons why you should use virtual environments when working on Python projects:

  • Isolation : Virtual environments provide an isolated environment for each project, allowing you to install project-specific dependencies without affecting other projects or the system-wide Python installation.
  • Dependency Management: Virtual environments make it easy to manage project dependencies and package versions. You can install, update, and remove packages without affecting other projects.
  • Reproducibility : Virtual environments ensure that your project is reproducible on different systems. By specifying the project dependencies in a requirements.txt file, you can easily recreate the environment on another machine.
  • Security : Virtual environments help prevent conflicts between different projects that may require different package versions. This can help avoid dependency conflicts and security vulnerabilities.

Creating a Virtual Environment

To create a virtual environment in Python, you can use the built-in venv module. The venv module is available in Python 3.3 and later versions. Here's how you can create a virtual environment for your project:

  1. Go to the folder of where your main root project is located, then run the following command in the terminal:
  2. linux/macOS: bash python3 -m venv .venv
  3. Windows: powershell python -m venv .venv or powershell py -m venv .venv

Activating the Virtual Environment

Once you've created the virtual environment, you need to activate it before you can start using it. To activate the virtual environment, run the following command in the terminal:

  • linux/macOS: bash source .venv/bin/activate
  • Windows: powershell .venv\Scripts\activate

You should see the name of the virtual environment in your terminal prompt, indicating that the virtual environment is active. You can now install project-specific dependencies using pip without affecting the system-wide Python installation. For example, if you just cloned a project from GitHub and you see a requirements.txt file, you can install all the dependencies by running: bash pip install -r requirements.txt

Deactivating the Virtual Environment

To deactivate the virtual environment and return to the system-wide Python installation, run the following command in the terminal:
deactivate
This will deactivate the virtual environment and return you to the global Python environment.


Conclusion

Creating a virtual environment in Python is a best practice when working on Python projects. It allows you to isolate project dependencies, manage package versions, and ensure reproducibility across different systems. By following the steps outlined in this guide, you can create and activate virtual environments for your Python projects and manage project-specific dependencies effectively.