Skip to content

Latest commit

 

History

History
179 lines (118 loc) · 4.02 KB

README.md

File metadata and controls

179 lines (118 loc) · 4.02 KB

Chapter 14: Package Management with PIP

Table of Contents

Introduction to PIP

PIP is the package installer for Python. It allows you to install and manage additional libraries and dependencies that are not included in the standard library.

Checking PIP Version

You can check the version of PIP installed on your system using the following command:

pip --version

Output:

pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Installing and Uninstalling Packages

PIP makes it easy to install and uninstall Python packages.

Installing Packages

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

pip install requests

Output:

Collecting requests
  Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
Installing collected packages: requests
Successfully installed requests-2.25.1

Uninstalling Packages

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

pip uninstall requests

Output:

Found existing installation: requests 2.25.1
Uninstalling requests-2.25.1:
  Would remove:
    /usr/local/lib/python3.9/site-packages/requests-2.25.1.dist-info/*
    /usr/local/lib/python3.9/site-packages/requests/*
Proceed (y/n)? y
  Successfully uninstalled requests-2.25.1

Virtual Environments

Virtual environments allow you to create isolated Python environments for different projects. This helps to avoid conflicts between dependencies of different projects.

Creating a Virtual Environment

You can create a virtual environment using the venv module.

python -m venv myenv

Activating a Virtual Environment

To activate the virtual environment, use the following command:

  • On Windows:
myenv\Scripts\activate
  • On macOS and Linux:
source myenv/bin/activate

Deactivating a Virtual Environment

To deactivate the virtual environment, use the following command:

deactivate

Managing Dependencies

You can manage the dependencies of your project using a requirements.txt file. This file lists all the packages required for your project.

Creating a requirements.txt File

To create a requirements.txt file, use the following command:

pip freeze > requirements.txt

Installing Dependencies from requirements.txt

To install the dependencies listed in a requirements.txt file, use the following command:

pip install -r requirements.txt

Publishing Packages

You can publish your own Python packages to the Python Package Index (PyPI) so that others can install and use them.

Preparing Your Package

Create a setup.py file with the necessary information about your package.

from setuptools import setup, find_packages

setup(
    name="mypackage",
    version="0.1",
    packages=find_packages(),
    install_requires=[
        # List your package dependencies here
    ],
)

Building Your Package

To build your package, use the following command:

python setup.py sdist bdist_wheel

Uploading Your Package to PyPI

To upload your package to PyPI, use the twine tool.

pip install twine
twine upload dist/*

Summary

In this chapter, we covered package management with PIP, including installing and uninstalling packages, creating and managing virtual environments, managing dependencies with requirements.txt, and publishing packages to PyPI.

Tasks

  1. Install a package using PIP and verify its installation.
  2. Create a virtual environment and activate it.
  3. Generate a requirements.txt file for a project and install the dependencies from it.
  4. Create a simple Python package and publish it to PyPI.

Next Chapter: Bitwise Operators