Python Tutorials - Herong's Tutorial Examples
∟Packages and Package Directories
This chapter provides introductions and tutorial examples about on packages and package directories. Topics include introduction of packages and package directories; importing package directories to create packages; accessing package members; importing package members only; importing sub-modules and sub-directories.
These sections are omitted from this Web preview version. To view the full content,
see information on how to obtain the full version this book.
What Is Package
What Is Sub-Module
What Is Sub-Package
"from package import *" Statement
Using __all__ List in Package
"import" Statement Summary
What Is the __main__.py File
"python -m package|module" Command
Takeaways:
- A package is a special module defined by an external directory,
and may contain sub-packages and sub-modules.
- A sub-module is a module defined by a module file inside a package directory.
- A package directory contains a special Python code file, __init__.py,
define package members: attributes (variable), functions, and classes.
- Package objects are created from module files by
running "import" statements.
- A sub-module is a module defined by a module file inside a package directory.
- A sub-package is a package defined by a sub-directory inside a package (referred as top-package) directory.
- "import package_name" statement loads a package directory into memory
and binds it package_name as a local variable.
- "import package_name as variable_name" statement loads a package directory into memory
and binds it variable_name as a local variable.
- "from package_name import member_name" statement loads a package directory into memory
and binds the given member to member_name as a local variable.
- "from package_name import *" statement loads a package directory into memory
and binds each member to member_name as a local variable.
- The __all__ list can be used in package override which members are to be
imported by the "from package_name import *" statement.
- "import package_name.submodule_name" statement loads a package directory into memory
and binds it package_name as a local variable. The given submodule is also loaded
into the package.
- "import package_name.subpackage_name" statement loads a package directory into memory
and binds it package_name as a local variable. The given subpackage is also loaded
into the package.
- The __main__.py file is a special module file in a package directory
that will be executed when the package directory is invoked by the "python" command line program.
- "python -m package|module" command allows you to load and run a given module
by the "python" command line program as the __main__ module.
Table of Contents
About This Book
Running Python Code Online
Python on macOS Computers
Python on Linux Computers
Built-in Data Types
Variables, Operations and Expressions
Statements - Execution Units
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Classes and Instances
Modules and Module Files
►Packages and Package Directories
"sys" and "os" Modules
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager
Jupyter Notebook and JupyterLab
References
Full Version in PDF/EPUB