This is the 23rd day of my participation in Gwen Challenge
Modules and packages
In Python, a.py file is called a Module.
A package is a folder that contains modules with a special __init__.py file, which indicates to Python that the folder is special because it contains Python modules.
Note that the name of the built-in module is not the same as the name of the built-in module.
The code is as follows:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
' a test module '
__author__ = 'Michael Liao'
import sys
def test() :
args = sys.argv
if len(args)==1:
print('Hello, world! ')
elif len(args)==2:
print('Hello, %s! ' % args[1])
else:
print('Too many arguments! ')
if __name__=='__main__':
test()
Copy the code
The py file itself uses standard UTF-8 encoding. Line 4 is a string representing the module’s documentation comment. The first string of any module code is treated as the module’s documentation comment. Line 6 uses the __author__ variable to include the author, so that when you publish the source code, others can see your name.
Installing third-party Modules
In general, third-party libraries are registered at the Official Python site, pypi.python.org
There are image processing library Pillow, Web framework Flask, Scientific computation Numpy, and so on
Installing Anaconda automatically installs dozens of third-party modules, making it easy to use
Module search path
By default, the Python interpreter searches the current directory, all installed built-in modules, and third-party modules in the sys module’s path variable:
import sys
print(sys.path)
Copy the code
There are two ways to add your own search directory:
- Modify sys.path directly to add the directory to search for :(modified at run time, invalid after run)
sys.path.append('/Users/michael/my_py_scripts')
- Setting environment Variables
PYTHONPATH
The contents of the environment variable are automatically added to the module search path. The setting is similar to setting the Path environment variable. Note that you only need to add your own search path. Python’s own search path is not affected.
scope
Classified as public and non-public (private) Generally begins with an underscore _ or a double underscore __
Private functions and variables “should not” be referred to directly, rather than “cannot”, because Python does not have a way to completely restrict access to private functions or variables. However, it is programming convention not to refer to private functions or variables.
Summary:
All external functions that do not need to be referenced are defined as private, and only external functions that need to be referenced are defined as public
This should belong to the object oriented idea, after encapsulating this module to provide the public function or variable for other use
The last
Welcome friends to discuss the question ~
If you think this article is good, please give it a thumbs-up 😝
Let’s start this unexpected meeting! ~
Welcome to leave a message! Thanks for your support! ヾ(≧▽≦*)o go!!
I’m 4ye. We should… next time. See you soon!! 😆