How do I Install Python Packages with pip?

Learn how to install Python packages with pip in VS Code. Manage dependencies, use requirements files, and ensure your environment is correctly set up.

Introduction

Python packages are essential for expanding the functionality of your projects. The built-in package manager, pip, allows you to install and manage libraries efficiently. This guide will show you how to install Python packages using pip in VS Code.


Step 1: Ensure pip Is Installed

  • Most Python installations come with pip pre-installed.
  • To check if pip is installed, run the following command: pip --version
  • If pip is not installed, update Python or install it manually using: python -m ensurepip --default-pip

Step 2: Install a Python Package

To install a package, use the pip install command followed by the package name.

  • Example: To install requests, a popular HTTP library: pip install requests
  • For a specific package version, specify it like this: pip install requests==2.26.0

Step 3: Install Multiple Packages from a Requirements File

  • To install multiple dependencies at once, use a requirements.txt file.
  • Inside the file, list packages line by line, e.g.:
    requests==2.26.0
    flask==2.0.1
    numpy
  • Install all dependencies with: pip install -r requirements.txt

Step 4: Verify Installed Packages

  • To check installed packages, use: pip list
  • To see details about a specific package: pip show package_name

Step 5: Use pip Inside a Virtual Environment

  • Always activate your virtual environment before installing packages.
  • Activate the environment and then install dependencies:
    venv\Scripts\activate  # Windows
    source venv/bin/activate  # macOS/Linux
    pip install flask

Final Thoughts

Using pip is the easiest way to manage Python packages in VS Code. Whether installing a single package, multiple dependencies, or working within a virtual environment, this guide ensures you have the right setup.

Next Step: Learn why VS Code might not recognize your virtual environment and how to fix it: Why Is VS Code Not Recognizing My Virtual Environment?

Share the Post:

Related Posts