- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Questions & Answers
- Python - Online Quiz
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - PIP
Python's standard library is a large collection of ready-to-use modules and packages. In addition to these packages, a Python programmer often needs to use certain third-party libraries. Third-party Python packages are hosted on a repository called Python Package Index (https://pypi.org/).
To install a package from this repository, you need a package manager tool. PIP is one of the most popular package managers.
The PIP utility is automatically installed with Python's standard distribution especially with version 3.4 onwards. It is present in the scripts folder inside Python's installation directory. For example, when Python 3.11 is installed on a Windows computer, you can find pip3.exe in C:\Python311\Scripts folder.
If pip is not installed by default, it can be installed by the following procedure.
Download get-pip.py script from following URL −
https://bootstrap.pypa.io/get-pip.py
To install run above script from command prompt −
c:\Python311>python get-pip.py
In scripts folder both pip and pip3 are present. If pip is used to install a certain package, its Python 2.x compatible version will be installed. Hence to install Python 3 compatible version, use pip3.
Install a Package
To install a certain package from PyPi, use install command with PIP. Following command installs Flask library in the current Python installation.
pip3 install flask
The package, along with its dependencies if any, will be installed from the PyPI repository. The above command produces following log in the terminal −
Collecting flask Downloading Flask-2.2.3-py3-none-any.whl (101 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 101.8/101.8 kB 3.0 MB/s eta 0:00:00 Collecting Werkzeug>=2.2.2 Downloading Werkzeug-2.2.3-py3-none-any.whl (233 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 233.6/233.6 kB 7.2 MB/s eta 0:00:00 Collecting Jinja2>=3.0 Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 kB 8.2 MB/s eta 0:00:00 Collecting itsdangerous>=2.0 Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB) Collecting click>=8.0 Downloading click-8.1.3-py3-none-any.whl (96 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 kB 5.4 MB/s eta 0:00:00 Requirement already satisfied: colorama in c:\users\mlath\appdata\roaming\python\python311\site-packages (from click>=8.0->flask) (0.4.6) Collecting MarkupSafe>=2.0 Downloading MarkupSafe-2.1.2-cp311-cp311-win_amd64.whl (16 kB) Installing collected packages: MarkupSafe, itsdangerous, click, Werkzeug, Jinja2, flask Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.2 Werkzeug-2.2.3 click-8.1.3 flask-2.2.3 itsdangerous-2.1.2
By default, the latest available version of the desired package is installed. To specify the version required,
pip3 install flask==2.0.0
To test if the package installation is complete, open Python interpreter and try to import it and check the version. If the package hasn't been successfully installed, you get a ModuleNotFoundError.
>>> import flask >>> print (flask.__version__) 2.2.3 >>> import dummypackage Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'dummypackage'
PIP utility works with −
PyPI (and other indexes) using requirement specifiers.
VCS project urls.
Local project directories.
Local or remote source archives.
Use requirements.txt
You can perform package installation at once by mentioning the list of required packages in a text file named as requirements.txt.
For example, the following requirements.txt file contains list of dependencies to be installed for FastAPI library.
anyio==3.6.2 click==8.1.3 colorama==0.4.6 fastapi==0.88.0 gunicorn==20.1.0 h11==0.14.0 idna==3.4 pydantic==1.10.4 sniffio==1.3.0 starlette==0.22.0 typing_extensions==4.4.0 uvicorn==0.20.0
Now use the -r switch in PIP install command.
pip3 install -r requirements.txt
The PIP utility is used with along with following commands −
pip uninstall
This command is used to uninstall one or more packages already installed.
Syntax
pip3 uninstall package, [package2, package3, . . ]
This will uninstall the packages along with the dependencies.
Example
pip3 uninstall flask
You will be asked confirmation for removal before proceeding.
pip3 uninstall flask Found existing installation: Flask 2.2.3 Uninstalling Flask-2.2.3: Would remove: c:\python311\lib\site-packages\flask-2.2.3.dist-info\* c:\python311\lib\site-packages\flask\* c:\python311\scripts\flask.exe Proceed (Y/n)?
pip list
This command gives a list installed packages, including editables. Packages are listed in a case-insensitive sorted order.
Syntax
pip3 list
Following switches are available with pip list command −
-o, --outdated: List outdated packages
pip3 list --outdated Package Version Latest Type -------- ------- ------- ----- debugpy 1.6.6 1.6.7 wheel ipython 8.11.0 8.12.0 wheel pip 22.3.1 23.0.1 wheel Pygments 2.14.0 2.15.0 wheel setuptools 65.5.0 67.6.1 wheel
-u, --uptodate : List uptodate packages
pip3 list --uptodate Package Version -------- --------- ------- click 8.1.3 colorama 0.4.6 executing 1.2.0 Flask 2.2.3 jedi 0.18.2 Jinja2 3.1.2 python-dateutil 2.8.2 pyzmq 25.0.2 six 1.16.0 Werkzeug 2.2.3
pip show
This command shows information about one or more installed packages. The output is in RFC-compliant mail header format.
Syntax
pip3 show package
Example
pip3 show flask Name: Flask Version: 2.2.3 Summary: A simple framework for building complex web applications. Home-page: https://palletsprojects.com/p/flask Author: Armin Ronacher Author-email: armin.ronacher@active-4.com License: BSD-3-Clause Location: C:\Python311\Lib\site-packages Requires: click, itsdangerous, Jinja2, Werkzeug Required-by:
pip freeze
This command outputs installed packages in requirements format. All the packages are listed in a case-insensitive sorted order.
Syntax
pip3 freeze
The output of this command can be redirected to a text file with the following command −
pip3 freeze > requirements.txt
pip download
This command downloads packages from −
PyPI (and other indexes) using requirement specifiers.
VCS project urls.
Local project directories.
Local or remote source archives.
In fact, pip download does the same resolution and downloading as pip install, but instead of installing the dependencies, it collects the downloaded distributions into the directory provided (defaulting to the current directory). This directory can later be passed as the value to pip install --find-links to facilitate offline or locked down package installation.
Syntax
pip3 download somepackage
pip search
This command searches for PyPI packages whose name or summary contains the given query.
Syntax
pip3 search query
pip config
This command is used to manage local and global configuration.
Subcommands
list − List the active configuration (or from the file specified).
edit − Edit the configuration file in an editor.
get − Get the value associated with command.option.
set − Set the command.option=value.
unset − Unset the value associated with command.option.
debug − List the configuration files and values defined under them.
Configuration keys should be dot separated command and option name, with the special prefix "global" affecting any command.
Example
pip config set global.index-url https://example.org/
This would configure the index url for all commands.
pip config set download.timeout 10
This would configure a 10 second timeout only for "pip download" commands.