0:00
/
0:00
Transcript

How To Install Virtual Environments in Python

Many people have this issue on windows computer, above video will solve it

Fixing PIP Errors & Mastering Python Virtual Environments on Windows

Python is an incredibly versatile language, but setting up your development environment can sometimes feel like navigating a maze, especially on Windows. We all have those struggles :). Two common areas where people often stumble are those nasty PIP errors and correctly setting up Python virtual environments. This guide, will demystify these processes, helping you get your Python projects running smoothly and avoid common pitfalls.

Why Virtual Environments?

Virtual environments create isolated setups for different Python projects [00:14]. This is incredibly useful when projects require different versions of the same library, preventing conflicts and keeping your main Python installation clean. Each project can have its own dedicated packages installed without affecting others [00:48].

Installing Python Correctly

To avoid common issues from the start, pay close attention during Python installation:

  • Download Python from python.org

  • During installation, choose "Customize installation"

  • Crucially, check "Add python.exe to path" and "Use admin privileges"

  • Ensure all optional features, including "pip," are selected

  • On the next screen, select "Install Python for all users"

Avoiding PIP Errors on Windows

PIP (Python's Package Installer) is essential, but errors are common. The most critical advice for Windows users is to always run Command Prompt as Administrator when using pip install commands

  • If you don't run as administrator, packages might install in a user-specific location not on the system path, leading to "command not recognized" errors [05:06], [05:49].

Installing the Virtual Environment Package

Once Python is correctly installed and your Command Prompt is running as Administrator:

  1. Open Command Prompt as Administrator.

  2. Run the command: pip install virtualenv

  3. You can verify the installation by typing virtualenv --version

Creating and Using a Virtual Environment

With virtualenv installed, you can now create isolated environments for your projects:

  1. Create an environment: Navigate to your project directory in the Command Prompt and run:

    Bash

virtualenv myenv
  1. Replace myenv with your desired environment name

  2. Activate the environment:

    Bash

myenv\Scripts\activate
  1. (Note the capital 'S' for Scripts) Your command prompt will now show the environment name in parentheses (e.g., (myenv)) to indicate it's active.

  2. Install packages: Once activated, any packages installed using pip install <package_name> (e.g., pip install requests ) will be specific to that virtual environment and won't affect your global Python installation or other environments

  3. You can view installed packages within the active environment using pip list

Demonstrating Isolation

The power of virtual environments lies in their isolation:

  • The video shows that packages installed inside an active virtual environment (e.g., 'requests') are not present when the environment is deactivated and pip list is run globally

  • Conversely, a package installed globally (e.g., 'flask' [09:50]) will not appear in the pip list of an activated virtual environment unless explicitly installed there

Deactivating a Virtual Environment

When you're done working in an environment, simply type deactivate in the command prompt to return to your global Python installation [09:34].

By following these steps, you can confidently manage your Python projects on Windows, avoiding common PIP errors and leveraging the power of virtual environments for a cleaner, more stable development workflow.


Source Video:

Discussion about this video

User's avatar