[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)

5.1 Stand-alone program

--If you are running Python in a text terminal or terminal window, enter the name of the Python program followed by the program file name.

test1.py


print("This stadalone program works!")

result


$ python test1.py
This stadalone program works!

5.2 Command line arguments

test2.py


import sys
print("Program arguments:",sys.argv)

result


$ python test2.py
Program arguments: ['test2.py']

$ python test2.py tra la la
Program arguments: ['test2.py', 'tra', 'la', 'la']

5.3 Module and import statement

-** Module ** is a file that summarizes Python code. --Refer to the code of other modules with the import statement. By doing this, you can use the imported module code and variables in the program.

5.3.1 Module import

--The simplest way to use the import statement is in the form of import module. Here, the module part is the file name of another Python file with the .py extension removed. --After passing the import statement, the main program will be able to access all parts of module.py as long as it is prefixed with module. --If the imported code is used in multiple places, consider importing it outside the function.

Main program


#Import report module
import report

description=report.get_description()
print("Today is weather:",description)

report.py


#module
#get_description()Imports the choice function from the Python standard random module.
def get_description():
    """Returns random weather like a pro"""
    from random import choice
    possibilities=["rain","snow","sleet","fog","sun","who knows"]
    return choice(possibilities)

result


$ python weatherman.py
Today is weather: fog

$ python weatherman.py
Today is weather: sun

$ python weatherman.py
Today is weather: fog

Rewritable


#I'm importing the choice function directly from the random module.
def get_description():
    """Returns random weather like a pro"""
    import random
    possibilities=["rain","snow","sleet","fog","sun","who knows"]
    return random.choice(possibilities)
>>> import random
>>> def get_description():
...     possibilities=["rain","snow","sleet","fog","sun","who knows"]
...     return random.choice(possibilities)
... 
>>> get_description
<function get_description at 0x11035b950>
>>> get_description()
'who knows'
>>> get_description()
'who knows'
>>> 

5.3.2 Importing modules by alias

--Can be imported using an alias.

import report as x

description=x.get_description()
print("Today is weather:",description)

5.3.3 How to import only what you need

--Python allows you to import only one or more parts from a module.

Import with original name


from report import get_description
description = get_description()
print("Today is weather:",description)

do_Import with it


from report import get_description as do_it
description = do_it()
print("Today is weather:",description)

5.3.4 Module search path

--The file used is the first matched file. So if you define a module called random yourself and it is included in the search path before the standard library, you will not be able to access the standard library random.

>>> for place in sys.path:
...     print(place)
... 

practice/lib/python37.zip
practice/lib/python3.7
practice/lib/python3.7/lib-dynload
usr/local/var/pyenv/versions/3.7.5/lib/python3.7
practice/lib/python3.7/site-packages

5.4 Package

--Modules can be organized into a hierarchical structure called ** packages **. --In addition to the following two files, the sources directory requires a file named init.py. The contents can be empty, but Python treats the directory containing this file as a package.

Main program


#The enumerate function can get the value in the order of index number and element. The index can be specified as 1 by specifying 1 as the second argument.
from sources import daily, weekly

print("Daily forecast:",daily.forecast())
print("Weekly forecast:")
for number, outlook in enumerate(weekly.forecast(),1):
    print(number, outlook)

sources/daily.py


#Module 1
def forecast():
    "Fake weather forecast"
    return "like yesterday"

sources/weekly.py


#Module 2
def forecast():
    "Fake weekly weather forecast"
    return ["snow","more snow","sleet","freezing rain","rain","fog","hail"]

result


$ python weather.py
Daily forecast: like yesterday
Weekly forecast:
1 snow
2 more snow
3 sleet
4 freezing rain
5 rain
6 fog
7 hail

Impressions

It was a cold day today.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

Recommended Posts

[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Introduction to Python3 Day 8] Chapter 4 Py Skin: Code Structure (4.1-4.13)
[Introduction to Python3 Day 3] Chapter 2 Py components: Numbers, strings, variables (2.2-2.3.6)
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 5] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.1-3.2.6)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
Introduction to Effectiveness Verification Chapter 1 in Python
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
Understand Python for Pepper development. -Introduction to Python Box-
Python packages and modules
[Chapter 5] Introduction to Python with 100 knocks of language processing
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
Introduction to Python Django (2) Win
Understand Python packages and modules
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 1
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 2
[Introduction to Udemy Python 3 + Application] 58. Lambda
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
Python Basic Course (14 Modules and Packages)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python