Python is a versatile programming language, and Ubuntu is a popular Linux distribution. Combining these two technologies, you’ll be well on your way to developing powerful applications on a stable platform. In this blog post, we’ll guide you through the process of installing, setting up, and using Python in Ubuntu.
Step 1: Check if Python is Already Installed
Most recent versions of Ubuntu come with Python pre-installed. To verify the installed version(s), open the terminal and enter the following commands:
python --version
python3 --version
If you see a version number, then Python is already installed. If not, proceed to the next step to install it.
Step 2: Install Python
You can install Python using Ubuntu’s package manager, apt. To do this, first update the package list by running:
sudo apt update
Next, install Python using the following command:
sudo apt install python3
Once the installation is complete, verify the installed version by running:
python3 --version
Step 3: Install pip (Python Package Manager)
pip is a package manager for Python, allowing you to easily install and manage Python libraries. To install pip, run the following command:
sudo apt install python3-pip
After the installation is complete, verify the installed version of pip by running:
pip3 --version
Step 4: Writing and Running Python Scripts
To write Python scripts, you can use any text editor you prefer. Some popular choices include Visual Studio Code, Sublime Text, or even the built-in gedit editor.
Once you’ve chosen an editor, create a new file with a “.py” extension, such as “hello_world.py”. Write the following Python code inside the file:
print("Hello, World!")
Save the file and open the terminal. Navigate to the directory containing the script and run the following command to execute the Python script:
python3 hello_world.py
You should see the output “Hello, World!” in the terminal.
Step 5: Installing and Using Python Libraries
With pip, you can easily install Python libraries. For example, let’s install the popular library, Requests:
pip3 install requests
Now you can use the Requests library in your Python scripts. Create a new file called “http_request.py” and add the following code:
import requests
response = requests.get("https://api.ipify.org?format=json")
data = response.json()
print("Your public IP address is:", data["ip"])
Run the script with the following command:
python3 http_request.py
Your public IP address should be displayed in the terminal.
Conclusion
Now that you know how to install and use Python in Ubuntu, you’re ready to start developing powerful applications. Remember to explore the vast ecosystem of Python libraries to help accelerate your development process. Happy coding!