Chapter 6.1 : Installing Python and Running Python Scripts in WSL Ubuntu

Chapter 6.1 : Installing Python and Running Python Scripts in WSL Ubuntu

AI Reading

Quick summary of this article

This guide explains how to install Python and run Python programs in WSL Ubuntu on Windows. It covers checking if Python is already installed, installing Python and pip, running code interactively or from script files, and managing packages with pip.

  • Check Python with python3 --version and install it using sudo apt install python3 python3-pip
  • Run Python interactively by typing python3 in the terminal, or run a script file with python3 filename.py
  • Create and edit Python scripts using nano filename.py to write code and save it
  • Install additional Python packages with pip3 install package-name, like the requests library
  • Use the commands python3, pip3 install, and nano as the main tools for Python development in WSL

Chapter 6.1: Installing Python and Running Python Programs in WSL Ubuntu

Python is a powerful and beginner-friendly programming language used in automation, data science, web development, and more. WSL Ubuntu provides a seamless way to install and run Python on your Windows system.

In this chapter, you’ll learn how to install Python, run Python scripts, and manage Python packages using pip in WSL Ubuntu.

🐍 Check If Python is Already Installed

python3 --version

If installed, you’ll see something like:

Python 3.10.12

📥 Installing Python and pip

If Python is not installed or you want to update it, follow these steps:

📌 Update Package List

sudo apt update

📌 Install Python and pip

sudo apt install python3 python3-pip

📌 Verify Installations

python3 --version
pip3 --version

🛠️ Running Python Code

📌 Run Python Interactively (REPL)

python3

Example session:

>>> print("Hello from Python!")
Hello from Python!
>>> exit()

📌 Run a Python Script File

nano hello.py

Add this code:

print("Hello, WSL Ubuntu!")

Save with Ctrl+O, press Enter, and exit with Ctrl+X.
Then run it:

python3 hello.py

📦 Installing Python Packages

📌 Use pip to Install Packages

pip3 install package-name

Example:

pip3 install requests

📌 Sample Script Using Requests

nano api_test.py

Add:

import requests
response = requests.get('https://httpbin.org/get')
print(response.json())

Run it:

python3 api_test.py

🧪 Practice Exercises

# Update and install Python
sudo apt update
sudo apt install python3 python3-pip

# Verify installation
python3 --version
pip3 --version

# Create and run a script
nano test.py
# Add: print("Testing Python in WSL")
python3 test.py

# Install a package and use it
pip3 install requests
nano api_test.py
# Add:
# import requests
# r = requests.get("https://httpbin.org/get")
# print(r.json())
python3 api_test.py

✅ Summary

  • python3 – Launch Python interpreter
  • python3 filename.py – Run a Python script
  • pip3 install – Install Python packages
  • nano filename.py – Create/edit Python files
  • Python is versatile and easy to use in WSL Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *