What it is
Python’s command-line interface (CLI) and interactive REPL (Read-Eval-Print Loop) for executing Python code, scripts, and managing packages.
Installation
Python is typically pre-installed on macOS and many Linux distributions. For Windows, download from python.org.
Linux (Debian/Ubuntu):
sudo apt update
sudo apt install python3 python3-pip
Linux (Fedora):
sudo dnf install python3 python3-pip
macOS (using Homebrew):
brew update
brew install python
Windows: Download the installer from python.org and run it. Ensure "Add Python to PATH" is checked during installation.
Verifying Installation:
python3 --version
pip3 --version
(On Windows, you might use python and pip instead of python3 and pip3 if that’s how they were added to your PATH).
Core Concepts
- REPL (Read-Eval-Print Loop): An interactive environment where you type Python code, it’s executed immediately, and the result is displayed. Great for testing small snippets or exploring.
- Scripts:
.pyfiles containing Python code that can be executed directly. - Modules/Packages: Reusable collections of Python code.
pipis the standard package installer for Python.
Commands / Usage
Running the Python REPL
Start the REPL:
python3
Starts an interactive Python 3 session.
Start REPL with specific version (if multiple installed):
python3.9
Starts an interactive session using Python 3.9.
Exit the REPL:
exit()
Exits the interactive Python session.
quit()
Exits the interactive Python session.
Import modules within the REPL:
import os
Imports the os module, making its functions available.
Executing Python Scripts
Run a Python script:
python3 my_script.py
Executes the Python code contained in my_script.py.
Run a script with command-line arguments:
python3 process_data.py input.csv output.txt --verbose
Executes process_data.py, passing input.csv, output.txt, and --verbose as arguments accessible via sys.argv.
Run a script and treat it as a command (if executable and has shebang):
./my_script.py
Executes my_script.py directly if it has a shebang line (e.g., #!/usr/bin/env python3) and execute permissions (chmod +x my_script.py).
Executing Python Code Directly
Execute a single Python expression:
python3 -c "print(2 + 2)"
Prints the result of the expression 2 + 2.
Execute multiple Python statements:
python3 -c "import sys; print(sys.version)"
Imports the sys module and prints the Python version.
Python Interpreter Options
Print Python version and exit:
python3 -V
Displays the installed Python version and exits.
Print Python version and exit (alternative):
python3 --version
Displays the installed Python version and exits.
Print detailed configuration information and exit:
python3 -V
Displays detailed information about the Python build and environment.
Run Python code from standard input:
echo "print('Hello from stdin')" | python3
Pipes the string "print('Hello from stdin')" to the Python interpreter, which executes it.
Run a script with -m (module execution):
python3 -m http.server 8000
Runs the built-in http.server module as a script, starting a web server on port 8000.
Enable site-specific sitecustomize.py:
python3 -S
Starts Python without importing the site module, which disables automatic import of site-specific modules like sitecustomize.
Don’t add user site-packages directory to sys.path:
python3 -s
Starts Python without adding the user site-packages directory to sys.path.
Add a directory to the Python path:
python3 -I
Starts Python in "isolated mode". The site module is not imported, and environment variables like PYTHONPATH and PYTHONHOME are ignored. This is useful for testing.
Disable Python’s site-specific optimizations:
python3 -O
Disables __debug__ and .pyc file generation.
Enable Python’s site-specific optimizations (and disable .pyc):
python3 -OO
Disables __debug__ and .pyc file generation, and also removes docstrings.
Print trace of statements being executed:
python3 -t
Prints a trace of statements being executed. Useful for debugging.
Warn about tabs instead of spaces:
python3 -tt
Warns about tabs instead of spaces in the input, and also errors on indentation where tabs and spaces are mixed.
Prevent addition of a directory to sys.path:
python3 -P <directory>
Adds <directory> to sys.path at the beginning. This is useful for testing module search paths.
Set the PYTHONHOME environment variable:
PYTHONHOME=/path/to/python python3
Sets the PYTHONHOME environment variable, which affects module searching.
Set the PYTHONPATH environment variable:
PYTHONPATH=/path/to/my/modules python3 my_script.py
Adds /path/to/my/modules to sys.path, allowing my_script.py to import modules from that directory.
Pip Usage (Package Management)
Install a package:
pip3 install requests
Installs the requests library from PyPI.
Install a specific version of a package:
pip3 install numpy==1.23.5
Installs version 1.23.5 of the numpy library.
Upgrade a package:
pip3 install --upgrade pandas
Upgrades the pandas library to the latest version.
Uninstall a package:
pip3 uninstall beautifulsoup4
Uninstalls the beautifulsoup4 library.
List installed packages:
pip3 list
Shows all installed Python packages and their versions.
Show information about an installed package:
pip3 show Flask
Displays details about the installed Flask package.
Install packages from a requirements file:
pip3 install -r requirements.txt
Installs all packages listed in requirements.txt.
Generate a requirements file:
pip3 freeze > requirements.txt
Creates requirements.txt containing a list of all installed packages and their exact versions.
Install packages in a virtual environment:
# Assuming you are inside an activated virtual environment
pip install django
Installs django into the currently active virtual environment.
Common Patterns
Running a script and capturing its output:
output=$(python3 my_script.py arg1)
echo "Script output: $output"
Executes my_script.py with arg1 and stores its standard output in the output variable.
Piping output to a Python script for processing:
cat data.txt | python3 process_lines.py
Reads data.txt, pipes its content line by line to process_lines.py, which can then process each line.
Executing a Python one-liner to manipulate text:
echo "hello world" | python3 -c "import sys; print(sys.stdin.read().upper())"
Takes "hello world", pipes it to Python, which reads it from stdin, converts it to uppercase, and prints it.
Running a Python script with a specific Python version:
python3.8 my_legacy_script.py
Ensures my_legacy_script.py runs with Python 3.8, useful for compatibility testing.
Using python -m to run installed scripts:
python3 -m jupyter notebook
Launches the Jupyter Notebook server using the installed jupyter module.
Creating and activating a virtual environment:
# Create
python3 -m venv myenv
# Activate (Linux/macOS)
source myenv/bin/activate
# Activate (Windows)
.\myenv\Scripts\activate
Creates an isolated Python environment named myenv, and then activates it so that pip and python commands operate within this environment.
Gotchas
pythonvspython3: On many systems,pythonmight still point to Python 2. Always explicitly usepython3andpip3to ensure you are using Python 3.- PATH Issues: If
python3orpip3commands are not found, it likely means Python is not correctly added to your system’s PATH. Reinstall or manually add it. - Virtual Environments: Forgetting to activate a virtual environment before installing packages can lead to clutter in your global Python installation. Always activate your
venvorconda env. - Shebang Line: For scripts intended to be run directly (
./my_script.py), ensure the shebang line (#!/usr/bin/env python3) is correct and the file has execute permissions (chmod +x my_script.py). pipversion: Ensure yourpipis up-to-date (pip3 install --upgrade pip) for the best experience and security.- Order of
sys.path: Python searches for modules in the order they appear insys.path. Be mindful of this if you have identically named modules in different locations.