PIP is a package manager for Python packages, or modules. If you are familiar with Node.js, PIP is to Python what NPM is to JavaScript.
1. What is PIP?
PIP stands for “Pip Installs Packages”. It is a command-line utility that allows you to install, reinstall, or uninstall PyPI (Python Package Index) packages with a simple command.
A package contains all the files you need for a module. Modules are Python code libraries you can include in your project.
2. Install PIP
If you have Python version 3.4 or later installed, PIP is included by default.
To check if PIP is already installed, open your command line or terminal and type:
pip --version
If it returns a version number, you are good to go!
3. Install packages (pip install)
Downloading a package is very easy. Open your command line interface and tell PIP to download the package you want.
Let’s install the popular requests package, which is used for making HTTP requests:
pip install requests
Once installed, you can import the package in your Python script:
import requests
response = requests.get('https://api.github.com')
print(response.status_code)
4. Uninstall packages
To uninstall a package, use the uninstall command.
pip uninstall requests
PIP will ask you to confirm the removal. Type y and press enter.
5. List installed packages
You can see all the packages you have installed on your system (or in your virtual environment) by using the list command.
pip list
This will output a table of package names and their current version.
6. Requirements file
When sharing a project (e.g., uploading to GitHub), you need to tell other developers which packages your project depends on. Instead of listing them manually, you generate a requirements.txt file.
Create requirements.txt (pip freeze)
The freeze command outputs installed packages in a requirements format. You can pipe this output into a file:
pip freeze > requirements.txt
Install from requirements.txt
When another developer clones your project, they can install all the required packages at once by running:
pip install -r requirements.txt
Discussion
Loading comments...