Installing Python
Python installation is the process of downloading, installing, configuring, and verifying the Python programming language on a computer. Python is widely used for web development, automation, data analysis, artificial intelligence, machine learning, scripting, testing, and many other software development tasks.
For Selenium automation, Python is commonly used because it provides a simple syntax, a large ecosystem of testing libraries, and strong integration with Selenium WebDriver. Before writing Python-based Selenium automation scripts, Python must be installed and configured correctly on the system.
In this chapter, we will learn how to install Python, configure the environment, verify the installation, understand PATH, use pip, create a virtual environment, install Selenium, and troubleshoot common Python installation problems.
1. What is Python?
Python is a high-level, interpreted, general-purpose programming language. It was designed with an emphasis on readability and developer productivity.
Python programs can be written using a simple and readable syntax, making Python suitable for beginners as well as professional developers.
Common Uses of Python
- Web Development
- Software Development
- Test Automation
- Selenium Automation
- Data Analysis
- Artificial Intelligence
- Machine Learning
- Data Science
- API Development
- Web Scraping
- DevOps Automation
- Task Automation
2. Why Python is Required for Selenium Automation?
Selenium WebDriver supports multiple programming languages, including Python, Java, C#, JavaScript, and others. Python is one of the commonly used languages for Selenium automation because its syntax is easy to understand and Selenium provides a Python package that can be installed using pip.
A typical Python Selenium automation environment contains the following components:
Operating System
|
v
Python
|
v
pip
|
v
Selenium Package
|
v
Browser
|
v
WebDriver / Selenium Manager
|
v
Automated Web Application Testing
3. Prerequisites for Installing Python
Before installing Python, make sure that your computer satisfies the basic requirements for the operating system and that you have permission to install software.
Basic Requirements
- A Windows, macOS, or Linux computer
- Internet connection for downloading Python and packages
- Permission to install software when required
- Command Prompt, PowerShell, Terminal, or another command-line application
- Sufficient disk space
4. Downloading Python
Python can be downloaded from the official Python website. The official documentation provides platform-specific installation instructions for Windows, macOS, and Unix-like systems.
For current Windows installations, the Python documentation describes the Python Install Manager, which can be obtained from the official Python website or Microsoft Store.
Official Python documentation:
Python Official Website
Download Python
5. Installing Python on Windows
Windows is one of the most commonly used operating systems for Selenium automation. Python can be installed on Windows using the official Python installation tools.
Step 1: Open the Python Website
Open a browser and visit the official Python website:
https://www.python.org/downloads/
Step 2: Download Python
Download the appropriate Python installation option for your Windows system. Current Python documentation describes the Python Install Manager as the modern installation method for Windows.
Step 3: Start the Installation
Open the downloaded installer or install the Python Install Manager according to the instructions shown by Python.
After installation, the python, py, and pymanager commands may be available depending on the installation configuration.
Step 4: Configure PATH
PATH is an operating system environment variable that tells the command-line shell where executable programs can be found.
If Python is correctly configured in PATH, you can execute Python from Command Prompt or PowerShell without entering the complete installation path.
python --version
If the command works, the terminal can locate the Python executable.
Step 5: Verify Python
Open Command Prompt or PowerShell and execute:
python --version
You can also use:
py --version
A successful installation displays the installed Python version.
6. Understanding Python PATH
PATH is an environment variable maintained by the operating system. It contains directories where executable programs can be searched for when a command is entered in the terminal.
For example, when you type:
python
the operating system searches configured locations to find the Python executable.
PATH Flow
User enters command
|
v
python
|
v
Operating System checks PATH
|
v
Python executable found
|
v
Python interpreter starts
Why PATH is Important
- Allows Python to be executed from any directory.
- Makes command-line usage easier.
- Allows development tools to locate Python.
- Helps package and automation tools work correctly.
- Allows commands such as
python and pip to be used from the terminal.
7. Checking the Python Version
After installing Python, the first verification step is checking the installed version.
python --version
Another command that may be used on Windows is:
py --version
Example output:
Python 3.x.x
The exact version displayed depends on the Python runtime installed on your computer.
8. Opening the Python Interpreter
The Python interpreter allows you to execute Python statements interactively from the terminal.
Open Command Prompt or PowerShell and type:
python
You may see a Python prompt similar to:
>>>
Now execute a simple Python statement:
print("Hello Python")
Expected output:
Hello Python
To exit the Python interpreter, you can use:
exit()
9. Creating Your First Python File
A Python program is commonly stored in a file with the .py extension.
Create a file named:
hello.py
Add the following code:
print("Hello, Python!")
Save the file and open a terminal in the directory containing the file.
Run the program using:
python hello.py
Output:
Hello, Python!
10. Understanding pip
pip is the package installer commonly used to install Python packages from the Python Package Index (PyPI). The Python documentation describes pip as the preferred installer program and shows usage through python -m pip.
Check whether pip is available:
python -m pip --version
You can also try:
pip --version
Why Use python -m pip?
Using python -m pip explicitly associates pip with the Python interpreter being used.
python -m pip install selenium
This is particularly useful when multiple Python versions are installed.
11. Upgrading pip
Keeping pip updated can help when working with modern Python packages.
python -m pip install --upgrade pip
Verify the installed version:
python -m pip --version
12. Installing Selenium with Python
After Python and pip are available, Selenium can be installed using pip.
python -m pip install selenium
The Python packaging documentation uses the python -m pip install PackageName pattern for installing packages and their dependencies.
Verify Selenium Installation
python -m pip show selenium
You can also test the import:
python -c "import selenium; print(selenium.__version__)"
13. Creating a Python Virtual Environment
A virtual environment creates an isolated Python environment for a project. This allows project-specific packages to be installed without unnecessarily affecting the system-wide Python environment.
Python provides the standard venv module for creating virtual environments.
Create a Virtual Environment
python -m venv venv
This creates a directory named venv.
Activate the Virtual Environment on Windows
venv\Scripts\activate
After activation, the terminal generally shows the environment name at the beginning of the command prompt.
Deactivate the Virtual Environment
deactivate
14. Recommended Selenium Project Structure
A Selenium project can be organized using a separate virtual environment and source-code directory.
selenium-project/
│
├── venv/
│
├── tests/
│ ├── test_login.py
│ └── test_home.py
│
├── pages/
│ ├── login_page.py
│ └── home_page.py
│
└── requirements.txt
This structure helps keep test code organized as the automation project grows.
15. Installing Selenium Inside the Virtual Environment
First activate the virtual environment:
venv\Scripts\activate
Then install Selenium:
python -m pip install selenium
Verify the installation:
python -m pip show selenium
16. Creating a Simple Selenium Program
After installing Selenium, create a file named:
test_google.py
Add the following code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()
This program creates a Chrome browser session, opens Google, prints the page title, and closes the browser.
17. Understanding the Selenium Python Flow
Python
|
v
Selenium Package
|
v
WebDriver
|
v
Browser
|
v
Web Application
|
v
Automation Actions
Modern Selenium can use Selenium Manager to assist with browser-driver management, so a simple webdriver.Chrome() setup may work without manually downloading a separate driver executable.
18. Installing Python on macOS
Python provides macOS installers through the official Python distribution. The Python documentation recommends using a current supported Python version where possible.
Basic Steps
- Open the official Python website.
- Go to the Python downloads section.
- Download the appropriate macOS installer.
- Open the downloaded installer package.
- Follow the installation instructions.
- Open Terminal.
- Verify the Python installation.
Verify Python
python3 --version
Check pip
python3 -m pip --version
Create a Virtual Environment
python3 -m venv venv
Activate It
source venv/bin/activate
Install Selenium
python3 -m pip install selenium
19. Installing Python on Linux
Many Linux distributions provide Python as part of the operating system or through their package management system. Care should be taken when modifying the system Python installation because operating-system tools may depend on it. Python documentation recommends virtual environments or per-user installations for many package-installation scenarios.
Check Python
python3 --version
Check pip
python3 -m pip --version
Create Virtual Environment
python3 -m venv venv
Activate Virtual Environment
source venv/bin/activate
Install Selenium
python3 -m pip install selenium
20. Python Installation Verification Checklist
After completing the installation, verify each component.
| Component |
Command |
Purpose |
| Python |
python --version |
Checks Python version |
| Python Launcher |
py --version |
Checks Windows Python launcher |
| pip |
python -m pip --version |
Checks package installer |
| Virtual Environment |
python -m venv venv |
Creates isolated environment |
| Selenium |
python -m pip show selenium |
Checks Selenium package |
21. Testing Python Installation with a Simple Program
Create a file named test_python.py:
name = "Selenium Student"
print("Python installation successful")
print("Welcome,", name)
Run the program:
python test_python.py
Expected output:
Python installation successful
Welcome, Selenium Student
22. Testing Python with Selenium
Once Selenium has been installed, create a simple browser automation program.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print("Page Title:", driver.title)
driver.quit()
If the browser opens successfully and the page title is printed, the basic Python and Selenium environment is working.
23. Common Python Installation Problems
Problem 1: 'python' is not recognized
This usually indicates that the Python command is not available through the current command environment.
Try:
py --version
If py works but python does not, review the Python installation and command configuration.
Problem 2: pip is not recognized
Instead of calling pip directly, use:
python -m pip --version
If pip is not installed in an environment where it should be, Python documentation provides ensurepip as one possible mechanism for bootstrapping pip.
python -m ensurepip --default-pip
Problem 3: Multiple Python Versions
If multiple Python versions are installed, explicitly select the required version using the available Python launcher or version-specific command.
For example, on Windows:
py -3 --version
The Python documentation recommends using the Windows py launcher for scenarios involving multiple Python runtimes.
Problem 4: Selenium Installation Fails
First check Python:
python --version
Then check pip:
python -m pip --version
Then upgrade pip:
python -m pip install --upgrade pip
Finally try:
python -m pip install selenium
24. Python Command vs python3 Command
Depending on the operating system and installation configuration, Python may be invoked using different commands.
| Environment |
Common Command |
| Windows |
python |
| Windows Launcher |
py |
| macOS |
python3 |
| Linux |
python3 |
The exact command can depend on the operating system and local configuration.
25. Python Installation and Selenium Environment
A basic Selenium Python environment can be represented as:
Computer
|
+-- Python
|
+-- pip
|
+-- Virtual Environment
|
+-- Selenium
|
+-- Browser
|
+-- Selenium Manager / Driver
|
+-- Automation Script
26. Installing Selenium Using a Requirements File
For professional projects, dependencies can be stored in a requirements.txt file.
Example:
selenium
Install the dependencies using:
python -m pip install -r requirements.txt
This allows a project environment to be recreated more easily.
27. Checking Installed Python Packages
To display installed packages, use:
python -m pip list
To display information about a specific package:
python -m pip show selenium
To save installed packages into a requirements file:
python -m pip freeze > requirements.txt
28. Python Installation Best Practices
- Download Python from trusted official sources.
- Use a supported Python version for new projects.
- Verify Python after installation.
- Verify pip before installing packages.
- Use virtual environments for individual projects.
- Keep project dependencies isolated.
- Use
python -m pip when package-to-interpreter association matters.
- Keep a requirements file for reproducible project setup.
- Avoid unnecessarily modifying system Python installations on Linux.
- Use a clear project directory structure.
29. Common Mistakes During Python Installation
- Downloading Python from an untrusted website.
- Not verifying the installation.
- Using an incompatible Python version for an existing project.
- Confusing Python and pip installations.
- Installing every package globally instead of using virtual environments.
- Having multiple Python installations without understanding which one is active.
- Ignoring PATH or command configuration problems.
- Not checking whether Selenium was installed into the intended environment.
30. Python Installation for Selenium Automation
For Selenium automation, a clean setup can follow this sequence:
Step 1: Install Python
|
v
Step 2: Verify Python
|
v
Step 3: Verify pip
|
v
Step 4: Create Project
|
v
Step 5: Create Virtual Environment
|
v
Step 6: Activate Virtual Environment
|
v
Step 7: Install Selenium
|
v
Step 8: Create Automation Script
|
v
Step 9: Launch Browser
|
v
Step 10: Execute Automation Test
31. Practical Example: Complete Selenium Environment Setup
Create a project folder:
mkdir selenium-project
cd selenium-project
Create a virtual environment:
python -m venv venv
Activate it on Windows:
venv\Scripts\activate
Install Selenium:
python -m pip install selenium
Create a Python file:
test_browser.py
Add:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print("Title:", driver.title)
driver.quit()
Run:
python test_browser.py
32. Understanding the Python Interpreter
The Python interpreter is the software component that reads and executes Python code.
Python Source Code
|
v
Python Interpreter
|
v
Execution
|
v
Program Output
For Selenium, the Python interpreter executes the automation script, while the Selenium library communicates with the browser automation infrastructure.
33. Understanding Python Packages
A Python package is a collection of reusable Python modules and resources. Packages allow developers to add functionality without implementing everything from scratch.
Selenium is installed as a Python package.
python -m pip install selenium
After installation, Python code can import Selenium modules:
from selenium import webdriver
34. Python Installation vs Selenium Installation
| Python Installation |
Selenium Installation |
| Installs Python interpreter |
Installs Selenium Python package |
| Provides Python runtime |
Provides browser automation APIs |
Verified using python --version |
Verified using python -m pip show selenium |
| Required before installing Selenium |
Required for Python-based Selenium automation |
35. Why Virtual Environments Are Important
Virtual environments provide isolation between projects. For example, one Selenium project can have one set of dependencies while another project can use different dependency versions.
Project A
|
+-- venv
+-- Selenium
+-- Project Dependencies
Project B
|
+-- venv
+-- Selenium
+-- Other Dependencies
This reduces dependency conflicts and helps keep projects reproducible. Python's documentation recommends creating a virtual environment for each project in its Windows setup guidance.
36. Troubleshooting Command Not Found Errors
If the terminal cannot find Python, first determine whether the installation is available through another command.
python --version
On Windows, also try:
py --version
On macOS or Linux, try:
python3 --version
If none of these commands work, review the Python installation and command configuration.
37. Troubleshooting pip Problems
Check pip using the Python interpreter:
python -m pip --version
Upgrade pip:
python -m pip install --upgrade pip
Install Selenium:
python -m pip install selenium
If you are using a virtual environment, make sure that it is activated before installing project dependencies.
38. Python Installation Interview Questions
Q1. What is Python?
Python is a high-level, interpreted, general-purpose programming language widely used in software development, automation, testing, data analysis, and other areas.
Q2. Why is Python used with Selenium?
Python provides simple syntax, a large ecosystem, and a Selenium package that makes browser automation development straightforward.
Q3. What is pip?
pip is a Python package installer used to install and manage Python packages.
Q4. How do you check the Python version?
python --version
Q5. How do you check pip?
python -m pip --version
Q6. How do you install Selenium?
python -m pip install selenium
Q7. What is a virtual environment?
A virtual environment is an isolated Python environment used to manage project-specific packages and dependencies.
Q8. How do you create a virtual environment?
python -m venv venv
Q9. How do you activate a Windows virtual environment?
venv\Scripts\activate
Q10. How do you deactivate a virtual environment?
deactivate
39. Practical Selenium Setup Checklist
- Install Python.
- Verify Python version.
- Verify pip.
- Create Selenium project directory.
- Create virtual environment.
- Activate virtual environment.
- Install Selenium.
- Verify Selenium package.
- Create a Selenium Python script.
- Launch the browser.
- Open a web page.
- Perform an automation action.
- Close the browser.
40. Real-World Selenium Project Setup
A professional Selenium automation project can contain multiple test cases, page objects, utilities, configuration files, reports, and dependency definitions.
selenium-automation/
│
├── venv/
│
├── tests/
│ ├── test_login.py
│ ├── test_search.py
│ └── test_checkout.py
│
├── pages/
│ ├── login_page.py
│ ├── home_page.py
│ └── checkout_page.py
│
├── utils/
│ └── helpers.py
│
├── requirements.txt
│
└── README.md
Python installation is the foundation of this environment because the test scripts and Selenium package run through the Python interpreter.
41. Recommended Learning Sequence
- Understand what Python is.
- Install Python.
- Verify Python.
- Understand PATH.
- Learn basic Python commands.
- Understand pip.
- Create a virtual environment.
- Install Selenium.
- Understand Selenium WebDriver.
- Create the first browser automation script.
- Learn locators.
- Learn WebElement interactions.
- Learn waits.
- Learn Page Object Model.
- Build real-world Selenium projects.
42. Best Practices for a Selenium Python Environment
- Use a supported Python release.
- Use a separate virtual environment for each automation project.
- Install dependencies using pip.
- Maintain a
requirements.txt file.
- Keep test code separated from page-object code.
- Use meaningful project and test names.
- Keep Python and Selenium versions documented for important projects.
- Do not unnecessarily install project dependencies globally.
- Verify the active Python interpreter before troubleshooting package problems.
- Use clean and reproducible project setup procedures.
43. Quick Command Reference
| Task |
Command |
| Check Python |
python --version |
| Check Windows Python Launcher |
py --version |
| Check Python 3 |
python3 --version |
| Check pip |
python -m pip --version |
| Upgrade pip |
python -m pip install --upgrade pip |
| Create virtual environment |
python -m venv venv |
| Activate Windows environment |
venv\Scripts\activate |
| Activate macOS/Linux environment |
source venv/bin/activate |
| Deactivate environment |
deactivate |
| Install Selenium |
python -m pip install selenium |
| Show Selenium information |
python -m pip show selenium |
| List installed packages |
python -m pip list |
| Save dependencies |
python -m pip freeze > requirements.txt |
| Install requirements |
python -m pip install -r requirements.txt |
44. Selenium Training Resource
If you want to learn Selenium in a structured way, you can explore the Selenium training course:
JustAcademy Selenium Training Course
You can also register for a course demo:
Register for Selenium Course Demo
45. Summary
Installing Python is the first major step toward creating a Python-based Selenium automation environment. The installation process includes obtaining Python from an official source, configuring the command environment, verifying Python, checking pip, and creating an isolated virtual environment for automation projects.
After Python is installed, Selenium can be installed using pip. A clean Selenium environment can then be used to create browser automation scripts, execute test cases, implement Page Object Model, manage dependencies, and develop complete automation frameworks.
Install Python
|
v
Verify Python
|
v
Verify pip
|
v
Create Virtual Environment
|
v
Activate Environment
|
v
Install Selenium
|
v
Create Selenium Script
|
v
Launch Browser
|
v
Automate Web Application
46. Learning Outcomes
After completing this topic, you should be able to:
- Explain what Python is.
- Understand why Python is used with Selenium.
- Install Python on Windows.
- Understand the Python installation process on macOS and Linux.
- Verify the Python installation.
- Understand PATH.
- Use the Python interpreter.
- Understand and use pip.
- Create Python virtual environments.
- Install Selenium using pip.
- Verify Selenium installation.
- Create a basic Selenium Python script.
- Troubleshoot common Python installation issues.
- Prepare a clean Python environment for Selenium automation.