Commands Pip

1/1/1970

Commands Pip

Python

# Check python version (py ~ python, -v ~ --version)
python --version
# or
py -v
 
# Run python program
py app.py
#or
python app.py
 
# shows  paths where the `python` ,exe found on system 
where python # window
#or
which python # Linux/MacOS
# Install package within a python script
import subprocess
subprocess.run(['pip', 'install', 'opencv-wrapper'])

Pip Installs Packages (pip)

pip is the package installer for Python. It allows you to install and manage additional libraries and dependencies from PyPI or other sources that are not part of the standard Python library. or pip is a package management system used to install and manage software packages written in Python. It stands for "Pip Installs Packages."

Note:

Manually Install/Upgrade pip

python -m ensurepip
python -m pip install --upgrade pip

Install Package

# insttall package
pip install package_name
 
# Install multiple packages
pip install package_name1 package_name2 
 
# Install Specific Version
pip install package_name==version # pip install django==3.2.5

Upgrade Package

# Upgrade NumPy:
pip install --upgrade package_name

Uninstall Package

# Uninstall Package
pip uninstall package_name

List Packages

# List All installed packages
pip list
 
# List Outdated Packages:
pip list --outdated
 
# show package information
pip show package_name

Note: - Inside a virtual environment, both commands will show the packages installed within that virtual environment (and not the global environment) and Outside a virtual environment (i.e., in the global environment), they show the global packages.

Requirements.txt Related command

# Displays a list of installed packages in `<package_name>==<version>` format.
pip freeze
 
# Install from requirements.
pip install -r requirements.txt
 
# Generates `requirements.txt` containing a list of all installed Python packages in your environment, (env+global)
pip freeze > requirements.txt
 
# scans your project and creates `requirements.txt` with only used packages.
pip install pipreqs
pipreqs . --force 
 
# Note: `pipreqs` does not delete or uninstall packages.

Note:


Virtual env

Virtual environments are a fundamental tool in Python development, allowing you to manage dependencies and project environments more effectively. They ensure that your projects remain isolated and that your development setup remains clean and organized.

Note: The venv module is not a standalone command; it must be run as a module/script using the -m option.

Create Virtual Environment using venv (Built-in Module)

# Create a Virtual Environment in /myenv
python -m venv myenv 
 
# Activate (Window)
myenv\Scripts\activate
# or
.\myenv\Scripts\activate
 
# Now  install packages in Virtual env
pip install package_name 

Create Virtual Environment using virtualenv (Third-Party Package)

# install virtual env
pip install virtualenv
 
# Create Virtual env
virtualenv myenv
 
# Activate, Intsall, Deactivate using same method

Deactivate Virtual Environment

# deactivate the current virtual environment
deactivate

Delete Virtual Environment

# Delete the existing virtual environment folde
rmdir /s /q venv 

Flask

# install Flask
pip install Flask
# Create app.py and Run the application
python app.py

Note : Jinja2 templating is Included with flask so no explicitly install required