Installing Python on Windows and Windows Subsystem for Linux (WSL)
This guide will help you install Python on both Windows and Windows Subsystem for Linux (WSL). Follow the steps below for a smooth installation process.
Installing Python on Windows
- Download the Python Installer:
- Go to the Python Downloads page.
- Click on the “Download Python” button. This will download the latest version of Python.
- Run the Installer:
- Locate the downloaded installer file (usually in the Downloads folder).
- Double-click the installer to run it.
- Customize Installation (Optional):
- Check the box that says “Add Python to PATH”.
- Click on “Customize installation” if you want to choose optional features, otherwise, click “Install Now”.
- Complete Installation:
- Follow the prompts and wait for the installation to complete.
- Once the installation is complete, click “Close”.
- Verify Installation:
- Open Command Prompt.
- Type
python --version
and press Enter.
- You should see the version of Python that you installed.
Installing Python on Windows Subsystem for Linux (WSL)
- Install WSL:
- Open PowerShell as Administrator.
- Run the command:
wsl --install
- Restart your computer if prompted.
- Set Up WSL:
- After restarting, open PowerShell again and run:
wsl --set-default-version 2
- Install your preferred Linux distribution from the Microsoft Store (e.g., Ubuntu).
- Launch WSL:
- Open your installed Linux distribution (e.g., Ubuntu) from the Start menu.
- Follow the on-screen instructions to complete the initial setup.
- Update Package List:
- In the WSL terminal, run:
sudo apt update
- Install Python:
- To install Python 3, run:
sudo apt install python3
- Optionally, install pip (Python package installer):
sudo apt install python3-pip
- Verify Installation:
- In the WSL terminal, type
python3 --version
and press Enter.
- You should see the version of Python that you installed.
By following these steps, you will have Python installed on both Windows and WSL, allowing you to run Python scripts in both environments.
Installing Python on macOS
This guide will help you install Python on macOS. Follow the steps below for a smooth installation process.
Installing Python on macOS
- Check the Pre-installed Python:
- macOS usually comes with Python 2.x pre-installed. To check, open Terminal and type:
python --version
- To check for Python 3.x, type:
python3 --version
- Install Homebrew (if not already installed):
- Homebrew is a popular package manager for macOS. To install Homebrew, open Terminal and paste the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Python using Homebrew:
- Once Homebrew is installed, you can use it to install Python 3. Run the following command in Terminal:
brew install python
- This command will install the latest version of Python 3 and
pip
(Python package installer).
- Verify Installation:
- To verify that Python 3 is installed, type:
python3 --version
- To verify that
pip
is installed, type:
pip3 --version
- Set Up PATH (if necessary):
- Homebrew usually sets up the PATH for you. However, if you need to manually set it up, add the following line to your
~/.zshrc
or ~/.bash_profile
(depending on your shell):
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
- Reload the shell configuration by running:
source ~/.zshrc # for Zsh users
source ~/.bash_profile # for Bash users
- Install Virtualenv (Optional but recommended):
- Virtualenv helps you create isolated Python environments. To install it, run:
pip3 install virtualenv
By following these steps, you will have Python installed on macOS, ready for running your Python scripts and projects.
Installing Python on Linux
This guide will help you install Python on various Linux distributions. Follow the steps below for a smooth installation process.
Installing Python on Ubuntu/Debian-based Distributions
- Update Package List:
sudo apt update
- Install Python:
- To install Python 3 and
pip
(Python package installer), run:
sudo apt install python3 python3-pip
- Verify Installation:
- To verify that Python 3 is installed, type:
python3 --version
- To verify that
pip
is installed, type:
pip3 --version
Installing Python on Fedora/RHEL-based Distributions
- Update Package List:
sudo dnf update
- Install Python:
- To install Python 3 and
pip
, run:
sudo dnf install python3 python3-pip
- Verify Installation:
- To verify that Python 3 is installed, type:
python3 --version
- To verify that
pip
is installed, type:
pip3 --version
Installing Python on Arch Linux
- Update Package List:
sudo pacman -Syu
- Install Python:
- To install Python 3 and
pip
, run:
sudo pacman -S python python-pip
- Verify Installation:
- To verify that Python 3 is installed, type:
python --version
- To verify that
pip
is installed, type:
pip --version
Creating a Virtual Environment (Optional but recommended)
- Install Virtualenv:
- To install Virtualenv, run:
pip3 install virtualenv
- Create a Virtual Environment:
- Navigate to your project directory:
cd /path/to/your/project
- Create a virtual environment:
python3 -m venv venv
- Activate the Virtual Environment:
- To activate the virtual environment, run:
source venv/bin/activate
- To deactivate the virtual environment, simply run:
deactivate
By following these steps, you will have Python installed on your Linux distribution, ready for running your Python scripts and projects.
Installing Python in a Docker Container
This guide will help you set up a Docker container with Python installed. Follow the steps below for a smooth installation process.
Prerequisites
- Install Docker:
- Verify Docker Installation:
docker --version
- You should see the Docker version installed on your machine.
Creating a Docker Container with Python
Using a Dockerfile
- Create a Project Directory:
- Create a new directory for your project and navigate to it:
mkdir my-python-app
cd my-python-app
- Create a Dockerfile:
- Create a file named
Dockerfile
in your project directory with the following content:
# Use the official Python image from the Docker Hub
FROM python:3.9
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
- Create a
requirements.txt
File:
- If your application has dependencies, list them in a
requirements.txt
file in your project directory:
flask
requests
- Create a Simple Python Application:
- Create a file named
app.py
in your project directory with a simple Python application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
- Build the Docker Image:
- In your project directory, run:
docker build -t my-python-app .
- Run the Docker Container:
- Once the image is built, run a container based on the image:
docker run -p 4000:80 my-python-app
- Your application will be accessible at
http://localhost:4000
.
Using a Docker Compose File
- Create a
docker-compose.yml
File:
- Create a
docker-compose.yml
file in your project directory with the following content:
version: '3'
services:
web:
image: python:3.9
working_dir: /usr/src/app
volumes:
- .:/usr/src/app
ports:
- "4000:80"
command: python app.py
- Run Docker Compose:
- In your project directory, run:
docker-compose up
- Your application will be accessible at
http://localhost:4000
.
By following these steps, you will have a Docker container running with Python installed, ready for running your Python applications.
VS Code Environment
When using VS Code we recommend installing the following extensions:
Docker
Makes it easy to create, manage, and debug containerized applications.
CLI Tooling
The tooling CLI tooling would be beneficial to have setup:
More details coming soon