How to create a virtual environment and activate it in Python

To create and activate a virtual environment in Python, you can follow these steps:

  1. Install virtualenv (if you haven't already):

    pip install virtualenv
  2. Create a virtual environment:

    virtualenv myenv

    This command will create a new directory named myenv which will contain the virtual environment files.


  3. Activate the virtual environment:

    • On Windows:
      myenv\Scripts\activate
    • On Unix or MacOS:b
      source myenv/bin/activate

    After activation, you should see the name of your virtual environment (myenv) in the terminal prompt.

To deactivate the virtual environment and return to the global Python environment, simply type deactivate in the command prompt or terminal.