virtual environment in python

To create a virtual environment in Python, follow these steps:

  1. Open a terminal/command prompt window.

  2. Navigate to the root directory where you want to create your virtual environment.

  3. Run the following command to create a new virtual environment:

    main.py
    python -m venv myenv
    
    21 chars
    2 lines

    Replace "myenv" with any other name you want to give to your virtual environment.

  4. Activate the newly created virtual environment by running the appropriate script:

    • On Windows:

      main.py
      myenv\Scripts\activate.bat
      
      27 chars
      2 lines
    • On Unix or Linux:

      main.py
      source myenv/bin/activate
      
      26 chars
      2 lines
  5. You should now see your prompt reflecting that you are working inside the virtual environment.

  6. You can now install packages specific to this virtual environment using pip, without affecting other projects or the system installation:

    main.py
    pip install package_name
    
    25 chars
    2 lines
  7. When you are done working in the virtual environment, you can deactivate it by running the following command:

    main.py
    deactivate
    
    11 chars
    2 lines

gistlibby LogSnag