Python Using Packages

PythonPythonBeginner
Practice Now

Introduction

Welcome to the magical desert kingdom! The vast expanse of golden sand stretches as far as the eye can see, and at the heart of this kingdom lies a desert tribe led by their wise and noble chieftain. The tribe faces numerous challenges in this harsh environment and seeks the aid of Python to navigate through the treacherous desert landscapes and harness the power of the elements.

In this lab, we will explore the concept of using packages in Python through a scenario in the magical desert kingdom. The goal is to help the desert tribe leader harness the power of Python packages to solve various problems they encounter in the desert.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") subgraph Lab Skills python/using_packages -.-> lab-271603{{"`Python Using Packages`"}} end

Exploring Python Packages

In this step, the goal is to introduce the concept of Python packages to the tribe. We will demonstrate how to use existing packages and install new ones to enrich their Python environment.

Python packages are a way of organizing related Python modules. They can be thought of as directories with Python files and a special __init__.py file inside them. These files are used to manage Python projects, distribute modules and packages, and work with external libraries.

Let's start by checking the available Python packages using the package manager pip. Run the following command in the terminal:

pip list

This will display a list of installed packages and their versions.

Now, we will open a Python script /home/labex/project/package.py to demonstrate the usage of the installed package:

## package.py
import requests

response = requests.get('https://labex.io/api/v2/vm')
print(response.json())

Save the script and run it using the command:

python3 package.py

The information below should be displayed on your terminal:

{'code': 401, 'reason': 'UNAUTHORIZED', 'message': 'Please login and try again', 'metadata': {}}

Creating Custom Packages

In this step, we will guide the tribe in creating their own custom Python package to encapsulate their unique tools and functionalities.

To create a custom Python package, we need to organize our code into a directory and create the necessary files to make it a package.

First, create a directory named desert_utils to serve as our package:

mkdir ~/project/desert_utils

Inside the desert_utils directory, create an empty file named __init__.py to indicate that this directory should be treated as a package:

touch ~/project/desert_utils/__init__.py

Now, let's create a module within the package by creating a file named navigation.py:

touch ~/project/desert_utils/navigation.py

Open navigation.py and add the following example code:

## navigation.py
def navigate_to_oasis():
    print("Navigating to the oasis using celestial guidance.")

Next, let's open a script /home/labex/project/use_desert_utils.py outside the package to demonstrate using the custom package:

## use_desert_utils.py
from desert_utils import navigation

navigation.navigate_to_oasis()

Run the script using the command:

python3 use_desert_utils.py

The information below should be displayed on your terminal:

Navigating to the oasis using celestial guidance.

Summary

In this lab, we delved into the realm of Python packages, covering the usage of existing packages as well as creating our own custom package. The tribe has gained valuable knowledge on utilizing Python packages to enhance their abilities in the desert kingdom.

Other Python Tutorials you may like