Python packages (With Examples) (2024)

A package is a container that contains various functions to perform specific tasks. For example, the math package includes the sqrt() function to perform the square root of a number.

While working on big projects, we have to deal with a large amount of code, and writing everything together in the same file will make our code look messy. Instead, we can separate our code into multiple files by keeping the related code together in packages.

Now, we can use the package whenever we need it in our projects. This way we can also reuse our code.

Package Model Structure in Python Programming

Suppose we are developing a game. One possible organization of packages and modules could be as shown in the figure below.

Python packages (With Examples) (1)

Note: A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.

Importing module from a package

In Python, we can import modules from packages using the dot (.) operator.

For example, if we want to import the start module in the above example, it can be done as follows:

import Game.Level.start

Now, if this module contains a function named select_difficulty(), we must use the full name to reference it.

Game.Level.start.select_difficulty(2)

Import Without Package Prefix

If this construct seems lengthy, we can import the module without the package prefix as follows:

from Game.Level import start

We can now call the function simply as follows:

start.select_difficulty(2)

Import Required Functionality Only

Another way of importing just the required function (or class or variable) from a module within a package would be as follows:

from Game.Level.start import select_difficulty

Now we can directly call this function.

select_difficulty(2)

Although easier, this method is not recommended. Using the full namespace avoids confusion and prevents two same identifier names from colliding.

While importing packages, Python looks in the list of directories defined in sys.path, similar as for module search path.

Table of Contents

  • Introduction
  • Package Model Structure in Python Programming
  • Importing module from a package
Python packages (With Examples) (2024)
Top Articles
Payzapp is 100% Fraud Application. | DesiDime
Requirements for Opening a Bank Account in Nigeria
San Angelo, Texas: eine Oase für Kunstliebhaber
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Victor Spizzirri Linkedin
Public Opinion Obituaries Chambersburg Pa
#ridwork guides | fountainpenguin
It may surround a charged particle Crossword Clue
Jennifer Hart Facebook
No Limit Telegram Channel
Arkansas Gazette Sudoku
DL1678 (DAL1678) Delta Historial y rastreo de vuelos - FlightAware
Valentina Gonzalez Leak
Shreveport Active 911
Highland Park, Los Angeles, Neighborhood Guide
Daily Voice Tarrytown
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
2016 Hyundai Sonata Refrigerant Capacity
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Spider-Man: Across The Spider-Verse Showtimes Near Marcus Bay Park Cinema
Fraction Button On Ti-84 Plus Ce
Why Is 365 Market Troy Mi On My Bank Statement
Publix Super Market At Rainbow Square Shopping Center Dunnellon Photos
Program Logistics and Property Manager - Baghdad, Iraq
Vegas7Games.com
Big Lots Weekly Advertisem*nt
Pasco Telestaff
Horn Rank
Dairy Queen Lobby Hours
Transformers Movie Wiki
Productos para el Cuidado del Cabello Después de un Alisado: Tips y Consejos
Que Si Que Si Que No Que No Lyrics
Sf Bay Area Craigslist Com
Nacogdoches, Texas: Step Back in Time in Texas' Oldest Town
Scioto Post News
Blue Beetle Movie Tickets and Showtimes Near Me | Regal
Colorado Parks And Wildlife Reissue List
Instafeet Login
Duff Tuff
Dinar Detectives Cracking the Code of the Iraqi Dinar Market
The power of the NFL, its data, and the shift to CTV
QVC hosts Carolyn Gracie, Dan Hughes among 400 laid off by network's parent company
Celsius Claims Agent
Jammiah Broomfield Ig
Phmc.myloancare.com
Backpage New York | massage in New York, New York
Craigslist Sparta Nj
Sleep Outfitters Springhurst
Publix Store 840
Houston Primary Care Byron Ga
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 5683

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.