How do I activate a virtual environment with steps?

Activating a virtual environment involves running a specific script or command to set up your terminal session to use the Python interpreter and packages from within that virtual environment. Below are the steps to activate a virtual environment on different operating systems using the terminal. For this example, I'll use the common virtual environment folder namevenv, but you can replace it with the name of your virtual environment.

On Windows:

  1. Open the terminal in VS Code (`Ctrl + ``).

  2. Navigate to your project directory.

     cd path\to\your\project
    
  3. Activate the virtual environment:

     .\venv\Scripts\activate
    

    After activation, your terminal prompt should change to something like(venv) PS C:\path\to\your\project>, indicating that the virtual environment is active.

On macOS and Linux:

  1. Open the terminal in VS Code (`Ctrl + ``).

  2. Navigate to your project directory:

     cd path/to/your/project
    
  3. Activate the virtual environment:

     source venv/bin/activate
    

    After activation, your terminal prompt should change to something like (venv) user@hostname:path/to/your/project$, indicating that the virtual environment is active.

Notes:

  • When the virtual environment is active, the terminal prompt typically includes the virtual environment name in parentheses, as mentioned in the examples.

  • To deactivate the virtual environment and return to the global Python environment, you can use the deactivate command on both Windows and macOS/Linux.

  • Remember to activate the virtual environment whenever you work on your project. You may find it helpful to use tools like Pipenv or poetry for more advanced dependency management along with virtual environments.

0
Subscribe to my newsletter

Read articles from LingarajTechhub All About Programming directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

LingarajTechhub All About Programming
LingarajTechhub All About Programming

Corporate Software Development Trainer with a demonstrated track record of success in the IT and Ed-tech industries for product development. I started my career as a software developer and have since taught C, C++, Java, and Python to hundreds of IT enthusiasts, including corporate professionals, throughout the years. I have been developing software for over 12 years.