The contents of the Python tutorial (Chapter 6) are itemized.

Previous article: The contents of the Python tutorial (Chapter 5) are itemized

Introduction

Python3 Engineer Certification Basic Exam As a countermeasure, this is a personal memo that summarizes the contents of the Python tutorial (book) in easy-to-memorize bullet points.

Reference material

Python Tutorial: https://docs.python.org/ja/3/tutorial/ Chapter 6: https://docs.python.org/ja/3/tutorial/modules.html Books: https://www.oreilly.co.jp/books/9784873117539/

"Chapter 6 Module" Theme

--Python3 Engineer Certification Basic Exam Score ―― 2/40 questions (5.0%) ☆ ★★★★ (importance: small) --Theme

6. Module

-** Module ** is a file that contains Python definitions and statements. --The file name of the module is "module name.py" --A set of variables that can be accessed from interactive mode or top-level scripts is called ** main module . --The module name is set in the global variable ** \ _ \ _ name__ ** in the module. --What is defined in a module can be ** imported ** into another module or main module. - import fibo * # fibo module is imported - fibo.fib (1000) * #Call the fib () function of the fibo module -* fib = fibo.fib * # Assign the fib () function of the fibo module to a local variable -* print (fibo. \ _ \ _ Name__) * #Print module name'fibo'

6.1 Further about modules

--When you put an executable statement in the module, it will be executed at the following timing. --When you run the module as a script. --When called by an import statement from another module (when the module name is encountered for the first time). --Each module has a private symbol table. --You can also import other modules from a module. --The imported module name is placed in the global symbol table on the imported side. -** from ** allows you to import only the specific name of the module. -* from fibo import fib, fib2 * # Import only the fib () and fib2 () functions of the fibo module -* from fibo import ** # Import names other than underscore _ --import * is rarely used as it often reduces readability.

6.1.1 Run the module as a script

--The execution command of the Python module is as follows. -* python fibo.py Arguments * # Execution command --When you run the Python module, \ _ \ _ name__ will contain "\ _ \ _ main__". -* if \ _ \ _ name__ == "\ _ \ _ main__": * # Execute the code below only when the script is executed

6.1.2 Module search path

--The interpreter searches for imported modules in the following order.

  1. Inside the ** built-in module ** (interpreter built-in module)
  2. List of directories obtained by ** sys.path variable ** --sys.path is initialized in the following location. --Directory with input script --Current directory if no file name is specified --PYTHONPATH (list of directory names) --Default per installation --The directory where the symbolic link is placed is not included in the module search path. --sys.path can be modified programmatically after initialization. --The directory containing the running script is placed at the beginning of the search path. --The search order is before the standard library path. -(Caution) If you put a script with the same name as the module of the standard library, it will be loaded preferentially.

6.1.3 "Compiled" Python files

--Python caches compiled modules in the ** \ _ \ _ pycache__ directory . --The cache name is module.version name.pyc - Version name ** is the compiled file format code, including the Python version number. --Compiled modules are platform independent. --Python compares the source code modification date and cache version to determine if recompilation is necessary. --The cache is not checked in the following cases. --When the module is loaded directly from the command line. --In this case, recompilation is always performed and the result is not saved. --When the source file of the module does not exist. --To support sourceless distribution, the following is required: --Put the compiled module in the source directory. --Do not put the source file itself.

6.2 Standard module

--Python comes with a library of standard modules. --The module configuration is determined by the settings at the time of installation. --There are also interpreter built-in and platform-dependent modules. --The interpreter embedded module is provided for performance needs and to provide access to OS primitives. --Platform-dependent modules include the winreg module for Windows. -** sys module ** is included in all Python interpreters. --sys.ps1 ... Primary prompt string definition ('>>>') --sys.ps2 ... Secondary prompt string definition ('...') --These two are defined only when the interpreter is in interactive mode. --sys.path ... A string list that specifies the module search path for the interpreter --The following directories are the initial values of sys.path. --Directory with input script --Current directory if no file name is specified --Environment variable PYTHONPATH --Built-in default

6.3 dir () function

-** The dir () function ** returns the name defined by the module in a sorted list of strings. --If you specify a module name as an argument, the name defined by that module is returned. --With no arguments, returns the name defined at the current level. --Names include variables, functions, module names, etc. --Import builtins module to return built-in function name or variable name. -* dir (builtins) * ... Get the built-in name list

6.4 Package

-** Package ** builds Python module names using "dot separated module names". --The substance of the package is a hierarchical structure of directories and a set of modules (.py files) in it. --The directories that make up the package must contain ** \ _ \ _ init__.py files . -Use of \ _ \ _ init__.py file: --Execution of package initialization code - \ _ \ _ all__ Variable ** (described later) set

6.4.1 Import * from package

--The behavior when import * is specified for the package is as follows. -If the \ _ \ _ all__ variable is defined in the \ _ \ _ init \ _ \ . Py file, import the submodule defined by the \ _ \ _ all_ variable. -If the \ _ \ _ all__ variable is not defined, the following will be imported. --Import of the corresponding package --All names defined in the package body -Names defined in the \ _ \ _ init \ _ \ _. Py file and explicitly loaded submodules

6.4.2 Cross-reference in package

--If the package consists of subpackages, there are two ways to refer to the sister package. -** Absolute import ** ... from module name (absolute path) import name -** Relative import ** ... ftom module name (relative path) import name --Use. (Current module hierarchy) or .. (parent hierarchy) as relative paths. --Use absolute import from modules that may be used as the main module. --Because the name is always "\ _ \ _ main \ _ \ _".

6.4.3 Packages that span multiple directories

-** \ _ \ _ path \ _ \ _ variable ** contains a list that includes the directory where \ _ \ _ init \ _ \ _. Py exists by default. --Initialization is done before running \ _ \ _ init \ _ \ _. Py. --By rewriting this variable, you can influence the search for modules and subpackages contained in the package.

Next article: A bulleted list of the contents of the Python tutorial (Chapter 7) (under construction)

Recommended Posts

The contents of the Python tutorial (Chapter 5) are itemized.
The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 2) are itemized.
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
The contents of the Python tutorial (Chapter 10) are itemized.
The contents of the Python tutorial (Chapter 6) are itemized.
The contents of the Python tutorial (Chapter 3) are itemized.
Get the contents of git diff from python
the zen of Python
Template of python script to read the contents of the file
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
[Maya Python] Crush the contents of the script 2 ~ list Notes
Reproduce the execution example of Chapter 5 of Hajipata in Python
About the ease of Python
About the features of Python
Simulation of the contents of the wallet
The Power of Pandas: Python
About the matter that the contents of Python print are not visible in docker logs
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
[Maya Python] Crush the contents of the script 1 ~ Camera Speed Editor
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
A Python script that compares the contents of two directories
How to check if the contents of the dictionary are the same in Python by hash value
The story of Python and the story of NaN
Easy encryption of file contents (Python)
[Python] The stumbling block of import
[Python of Hikari-] Chapter 09-03 Class (inheritance)
First Python 3 ~ The beginning of repetition ~
[Python] [Table of Contents Links] Python Programming
Understand the contents of sklearn's pipeline
[Translation] scikit-learn 0.18 Tutorial Table of Contents
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
See the contents of Kumantic Segumantion
Python Math Series ⓪ Table of Contents
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Introductory table of contents for python3
About the basics list of Python basics
Learn the basics of Python ① Beginners
[python, ruby] fetch the contents of a web page with selenium-webdriver
Output the contents of ~ .xlsx in the folder to HTML with Python
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
Verification of the theory that "Python and Swift are quite similar"
Contents of reading VBA x Python fastest work technique Memo chapter1
[Python] A program that rotates the contents of the list to the left
I checked the contents of docker volume
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module
Check the existence of the file with python
About the virtual environment of python version 3.7
Python tutorial
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
The result of installing python in Anaconda