Python Language Modularization: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 64: Line 64:
</syntaxhighlight>
</syntaxhighlight>
==Importing from a Relative Path==
==Importing from a Relative Path==
<font color=darkkahki>  
<font color=darkkhaki>  
Clarify this idiom:
Clarify this idiom:


Line 70: Line 70:
  import some_module as blah_some_module
  import some_module as blah_some_module
  import some_other_module as blah_some_other_module
  import some_other_module as blah_some_other_module
</font>


=Library=
=Library=

Revision as of 03:13, 19 January 2022

External

Internal

Overview

Python Script

A script is a module whose aim is to be executed. It has the same meaning as "program" or "application", but it is usually used to describe simple and small program. It contains a stored set of instructions that can be handed over to the Python interpreter. Python scripts have .py extensions.

Top-Level Script

Packages can be run as if they were scripts if the package provides a top-level script __main__.py.

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. Modules and packages are managed by a Python package manager like pip, Conda, Pipenv and Poetry.


Does this apply to module?

import pulumi_aws as aws

aws.ec2.SecurityGroup(....)

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. For example, the urllib package contains several modules: urllib.request, urllib.error, etc. Modules and packages are managed by a Python package manager like pip, Conda, Pipenv and Poetry.

Regular Package

A traditional package, such as a directory containing an __init__.py file.

Namespace Package

A PEP 420 package which serves only as a container for subpackages. Namespace packages may have no physical representation, and have no __init__.py file.

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

Importing All The Code from a Module

import mymodule

Clarify the difference between importing a module and a package.

TO PROCESS: The import system: https://docs.python.org/3/reference/import.html

Importing only a Function

from  urllib.request import urlopen

Importing from a Relative Path

Clarify this idiom:

sys.path.append("../sdk")
import some_module as blah_some_module
import some_other_module as blah_some_other_module

Library

The term library is simply a generic term for a bunch of code that was designed with the aim of being reused by many applications. It provides some generic functionality that can be used by specific applications. When a module or a package is published, people refer to it as a library.

Standard Library

If the published module or package is is available in the Python Module Index, it is referred to as a "standard library".

Python Standard Library

https://docs.python.org/3/library/

Notable Modules and Packages

These modules are available in the Python Standard Library, hence are referred to as "standard libraries".

Python Module Index

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

Python Package Index PyPI

https://pypi.org

It can be searched with pip search.

Organizatorium

  • Each installation of Python may have different modules installed. Python determines the path to its modules by examining the location of the python3 executable.