Create a Python Package

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Learn how to create a Python package

Note

This exercise mostly just involves copying files on the file system. There shouldn't be a lot of coding.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") subgraph Lab Skills python/comments -.-> lab-132529{{"`Create a Python Package`"}} python/conditional_statements -.-> lab-132529{{"`Create a Python Package`"}} python/lists -.-> lab-132529{{"`Create a Python Package`"}} python/tuples -.-> lab-132529{{"`Create a Python Package`"}} python/function_definition -.-> lab-132529{{"`Create a Python Package`"}} python/importing_modules -.-> lab-132529{{"`Create a Python Package`"}} python/using_packages -.-> lab-132529{{"`Create a Python Package`"}} python/standard_libraries -.-> lab-132529{{"`Create a Python Package`"}} python/classes_objects -.-> lab-132529{{"`Create a Python Package`"}} python/encapsulation -.-> lab-132529{{"`Create a Python Package`"}} end

Making a Package

In previous exercises, you created the following files that were related to type-checked structures, reading data, and making tables:

  • structure.py
  • validate.py
  • reader.py
  • tableformat.py

Your task is to take all of these files and move them into a package called structly. To do that, follow these steps:

  • Make a directory called structly
  • Make an empty file __init__.py and put it in the structly directory
  • Move the files structure.py, validate.py, reader.py, and tableformat.py into the structly directory.
  • Fix any import statements between modules (specifically, the structure module depends on validate).

Once you've done that, modify the stock.py program so that it looks exactly like this and that it works:

## stock.py

from structly.structure import Structure

class Stock(Structure):
    name = String()
    shares = PositiveInteger()
    price = PositiveFloat()

    @property
    def cost(self):
        return self.shares * self.price

    def sell(self, nshares: PositiveInteger):
        self.shares -= nshares

if __name__ == '__main__':
    from structly.reader import read_csv_as_instances
    from structly.tableformat import create_formatter, print_table
    portfolio = read_csv_as_instances('portfolio.csv', Stock)
    formatter = create_formatter('text')
    print_table(portfolio, ['name','shares','price'], formatter)

Summary

Congratulations! You have completed the Create a Python Package lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like