🐍
Python For Starters
  • Overview
  • Python for starters
  • Content
  • Foreword
  • Introduction To Python
    • Python Installation
    • Variables
    • Python Data Types
    • Python Data Structures
    • Python numbers
    • Python strings
    • Python Boolean
    • Python constants
    • Python Comments
    • Type Conversion
    • Control flow in python
  • Python Functions
    • Function Definition
    • Function Parameter
    • Recursive Functions
    • Using Global Variables in Functions
    • Lambda Functions
  • What are Data Structures
    • Python Lists
    • Python Tuples
    • Python Dictionaries
    • Python Sets
    • Iterable in python
    • Python Map, Filter & Reduce Functions
  • Error handling in Python
  • Loop ... Else Clause, Partial Functions & Type Hints
    • Partial Functions
    • Type Hints
  • Python Modules
  • Python Libraries & Packages
    • Python Packages
  • Directory & IO
    • Python IO Module
  • Pyenv
  • Virtual Environments
    • Pipenv
    • Virtualenv
  • Object Oriented Programming in Python
    • Class
    • Class Methods
    • Python Class Inheritance
    • Python Class Polymorphism
    • Python special methods
  • Appendix
  • Contributing
Powered by GitBook
On this page
  1. Python Libraries & Packages

Python Packages

Packages

A Python package is a directory of Python modules. Packages are a way to organize your code by grouping related modules together. For example, you might have a package called mypackage that contains several modules, each of which defines a specific function or class.

Creating a package in python

To create a package in Python, you will need to follow these steps:

  • Create a directory for your package, with a descriptive name that reflects the contents of the package.

  • Inside the package directory, create a file named __init__.py. This file can be empty, or it can contain code that runs when the package is imported.

  • Create one or more Python modules (i.e., files ending in .py) that contain the code you want to include in the package.

  • Optionally, create a setup.py file at the top level of your package. This file can be used to specify metadata about your package, such as its name, version, and dependencies, and to automate the process of installing and distributing your package.

  • Optionally, create a README.md file that describes the contents and purpose of your package, and any other relevant information.

For example, suppose you want to create a package called my_package that contains a module my_module.py. Here's how you might structure your package:

my_package/
├── __init__.py
├── my_module.py
└── setup.py

To use your package in another Python script, you would import it using the following syntax:

import my_package.my_module

You can then access the contents of the package using the dot notation:

my_package.my_module.specific_function()
PreviousPython Libraries & PackagesNextDirectory & IO

Last updated 2 years ago