Python Language Modularization

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Standalone Program

Module

A module is an organizational unit of Python code. It is a file with the .py extension containing Python code that can be imported inside another Python program. It can define functions, classes and variables. A module can also include runnable code. The file name is the module name with the suffix .py appended. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing, where the code in one module is made available to Python code in another module.

Does this apply to module?

import pulumi_aws as aws

aws.ec2.SecurityGroup(....)

Notable Modules

  • venv
  • What is the difference between a standard library and a module.
  • "webbrowser" standard library.

Package

A package is a Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with a __path__ attribute. Packages allow for a hierarchical structuring of the module namespace using dot notation. In the same way that module avoid collisions between global variable names, packages avoid collision between module name. Packages are managed by a Python package manager like pip, Conda, Pipenv and Poetry.

Python Package Index PyPI

https://pypi.org

It can be searched with pip search.

Regular Package

Namespace Package

Package Metadata

Name: pulumi
Version: 2.11.2
Summary: Pulumi's Python SDK
Home-page: https://github.com/pulumi/pulumi
Author:
Author-email:
License: Apache 2.0
Location: /Users/ovidiu/Library/Python/3.8/lib/python/site-packages
Requires: dill, grpcio, protobuf
Required-by: pulumi-aws, pulumi-kubernetes, pulumi-random, pulumi-tls

Requires

Required-by

Importing a Package

Clarify the difference between importing a module and a package.

import mypackage

Python Standard Library

https://docs.python.org/3/py-modindex.html