Installation of Python
How to install Python on Windows, Linux, and macOS, choosing an IDE, the four ways to run Python code, the CPython execution pipeline, bytecode and the PVM, PEP 8 coding standards, your first program, real-world DevOps examples, and common installation mistakes.
1.1 What Do You Need?
To start writing Python, you need:
- Python 3 installed on your computer.
- A terminal to run commands.
- A text editor such as VS Code.
You can download Python from python.org.
1.2 Install Python on Windows
- Open python.org/downloads.
- Download the latest Python 3 installer for Windows.
- Start the installer.
- Select Add python.exe to PATH.
- Select Install Now.
The PATH option allows you to run Python by typing python in a terminal.
Open PowerShell and check the installation:
python --version
You can also use the Windows Python launcher:
py --version
1.3 Install Python on Ubuntu or Debian
Open a terminal and run:
sudo apt update
sudo apt install python3 python3-pip python3-venv
Check the installation:
python3 --version
pip3 --version
1.4 Install Python on Fedora, RHEL, or CentOS
Run:
sudo dnf install python3 python3-pip
Then check the installation:
python3 --version
1.5 Install Python on macOS
Homebrew is a common way to install Python on macOS. If Homebrew is already installed, run:
brew install python
Check the installation:
python3 --version
pip3 --version
1.6 Verify That Python Works
Run a small Python command:
python3 -c "print('Python is working')"
On Windows, use python or py if python3 is not available:
python -c "print('Python is working')"
You should see:
Python is working
1.7 Write Your First Python File
Create a file named hello.py:
print("Hello, Python!")
Run it from the terminal:
python3 hello.py
On Windows, use:
python hello.py
The output should be:
Hello, Python!
1.8 Choose an Editor
You can write Python in any text editor, but these tools make learning easier:
- VS Code: A lightweight editor with Python support, debugging, and extensions.
- PyCharm: A full Python development environment.
- IDLE: A simple editor included with many Python installations.
- Jupyter Notebook: Useful for learning, experiments, and data analysis.
For this course, VS Code with the official Python extension is a good choice.
1.9 Create a Virtual Environment
A virtual environment keeps the packages for one project separate from other projects.
Create one inside your project folder:
python3 -m venv .venv
Activate it on Linux or macOS:
source .venv/bin/activate
Activate it on Windows PowerShell:
.venv\Scripts\Activate.ps1
After activation, your terminal usually shows (.venv). You can now install packages without affecting other projects.
To leave the virtual environment, run:
deactivate
1.10 Install a Python Package
pip is Python’s package installer. For example, install the requests package inside an activated virtual environment:
python -m pip install requests
Check that it works:
python -c "import requests; print(requests.__version__)"
Using python -m pip makes sure that pip belongs to the Python interpreter you are using.
1.11 What Happens When You Run Python?
When you run a Python file, Python reads the file and executes the instructions.
For now, you only need to remember this:
- You write Python code in a
.pyfile. - The Python interpreter runs the file.
- Python displays the result or an error message.
Python may create a __pycache__ folder while it runs. This folder contains temporary cached files. You normally do not need to edit it.
1.12 Basic Python Style
Python uses indentation to group code. Use four spaces for each indentation level.
name = "Alex"
if name:
print(f"Hello, {name}")
Use clear names:
server_count = 3
Avoid unclear names:
x = 3
Python’s common style guide is called PEP 8. It helps teams write code that looks consistent and is easy to read.
1.13 Common Installation Problems
Python Command Not Found
This usually means Python is not installed or its directory is not in the PATH.
On Windows, reinstall Python and select Add python.exe to PATH. Then close and reopen PowerShell.
On Linux or macOS, try python3 instead of python.
The Wrong Python Version Appears
Several Python versions can exist on one computer. Check the version before installing packages or running a project:
python3 --version
On Windows:
py --list
pip Installs into the Wrong Python
Use this form instead of calling pip by itself:
python -m pip install package-name
PowerShell Does Not Allow Virtual Environment Activation
If PowerShell blocks the activation script, use Command Prompt, or ask an administrator to review the PowerShell execution policy for your machine. Do not change security settings without understanding your organization’s policy.
1.14 Practice Exercise
- Install Python 3.
- Confirm the Python version.
- Create a file named
system_check.py. - Add this code:
import platform
print(f"Operating system: {platform.system()}")
print(f"Python version: {platform.python_version()}")
- Run the file from your terminal.
Interview Questions
- How do you check whether Python is installed?
- What is the difference between
python,python3, andpy? - What is PATH, and why is it important?
- What is a virtual environment?
- Why should you use
python -m pip? - How do you run a Python file from the terminal?
Quick Interview Answer
To install Python, I install Python 3 from the official Python website or the operating system package manager. I verify it with
python --versionorpython3 --version, create a virtual environment for each project, and install packages withpython -m pip. On Windows, I make sure that Python is added to PATH during installation.
Key Takeaways
- Install Python 3, not Python 2.
- Always verify the installation from a terminal.
- Use a virtual environment for each project.
- Use
python -m pipto install packages for the correct interpreter. - Use VS Code or another editor to write Python files.
Chapter practice checkpoint
Run python --version (or python3 --version on systems using that command). Save print("Hello, automation!") in hello.py, then run it from the terminal with python hello.py.
Expected output: Hello, automation! on its own line.
Knowledge check: What is the difference between the interpreter prompt and a saved script?
Check your answer
The prompt evaluates input interactively. A script stores statements in a file that the interpreter executes, making the work repeatable and shareable.You are ready to continue when you can locate your script, run it, change its message, and explain the output.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form