What is the module in Python? Learn Python 3 by hand

Posted by kustomjs on Tue, 01 Feb 2022 09:58:48 +0100

introduce

In this section, we will learn about Python modules. Including the concept and import method of module, the concept and use of package, the introduction of third-party module, the use of command-line parameters, etc.

Knowledge points

  • Module import
  • package
  • Introduction to default / third party module
  • Command line parameters

modular

So far, all the code we wrote in the Python interpreter was lost when we exited the interpreter. But when people write large programs, they tend to divide the code into several different files for use, debugging and better readability. In Python, we use modules to achieve these purposes. A module is a file that includes Python definitions and declarations. The file name is the module name plus py suffix.

You can use global variables__ name__ Get the module name (a string) of the module.

Now let's see how the module works. Create a bars Py file. The contents of the document are as follows:

"""
Bars Module
============
This is an example module for printing different split lines
"""
def starbar(num):
    """Print * Split line

    :arg num: Line length
    """
    print('*' * num)

def hashbar(num):
    """Print # Split line

    :arg num: Line length
    """
    print('#' * num)

def simplebar(num):
    """Print - Split line

    :arg num: Line length
    """
    print('-' * num)

Now let's start the interpreter and import our module.

>>> import bars
>>>

We must use the module name to access the functions within the module.

>>> bars.hashbar(10)
##########
>>> bars.simplebar(10)
----------
>>> bars.starbar(10)
**********

Import module

There are different ways to import modules. We've seen one. You can even import specified functions from the module. Do it in this way.

>>> from bars import simplebar, starbar
>>> simplebar(20)
--------------------

You can also use from module import * to import all definitions in the module, but this is not the recommended practice.

package

Contain__ init__. The directory of py file can be used as a package, and all the files in the directory py files are all sub modules of this package.

This section will create the following mymodule Directory:

In this example, mymodule is a package name, and bars and utils are the two sub modules inside.

First create the mymodule Directory:

cd /home/shiyanlou
mkdir mymodule

Then, the bars written in the previous section Copy py to the mymodule directory, and then use touch to create a utils Py file.

Use the touch command to create an empty__ init__.py file.

touch mymodule/__init__.py

If__ init__.py file has a file named__ all__ Only the names listed in the list will be made public.

So if the__ init__.py file contains the following contents:

from mymodule.bars import simplebar
__all__ = [simplebar, ]

Then only simplebar will be available for import. If you test in the python 3 interpreter, you need to make sure that the python 3 is executed in a directory of the same level as the mymodule directory, similar to the following operations, otherwise an error message of ImportError: No module named 'mymodule' will appear.

python3
>>>

from mymodule import * can only work on module level objects. Attempting to import functions or classes will result in syntax error.

reference material

Default module

Now when you install Python, you will install different modules. You can use them on demand or install new modules for other special purposes. In the following examples, we will see many of the same examples.

The above example shows how to get a list of all the modules installed in your system. Don't paste them here because it's a big list.

You can also use the help() function in the interpreter to find the documents of any module / class. If you want to know all the methods available for strings, you can do the following:

>>> help(str)

os module

os The module provides functions related to the operating system. You can import it using the following statement:

>>> import os

The getuid() function returns the valid user id of the current process.

>>> os.getuid()
500

The getpid() function returns the id of the current process. getppid() returns the id of the parent process.

>>> os.getpid()
16150
>>> os.getppid()
14847

The uname() function returns different information identifying the operating system. In Linux, the detailed information it returns can be obtained from the uname \-a command. The object returned by uname () is a tuple (sysname, nodename, release, version, machine).

>>> os.uname()
('Linux', 'd80', '2.6.34.7-56.fc13.i686.PAE', '#1 SMP Wed Sep 15 03:27:15 UTC 2010', 'i686')

The getcwd() function returns the current working directory. chdir(path) changes the current directory to path. Then we change the current working directory to / Code and look at the current working directory again.

>>> os.getcwd()
'/home/shiyanlou'
>>> os.chdir('Code')
>>> os.getcwd()
'/home/Code'

So now let's use another function provided by the os module to create our own function, which will list all files and directories in a given directory.

def view_dir(path='.'):
    """
    This function prints all files and directories in a given directory
    :args path: Specify the directory. The default is the current directory
    """
    names = os.listdir(path)
    names.sort()
    for name in names:
        print(name, end =' ')
    print()

Use the view in the example_ Dir() function.

>>> view_dir('/')
.bashrc .dockerenv .profile bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

os module also has many very useful functions, you can here Read the relevant content.

Requests module

Requests It is a third-party Python module. Its official website is introduced as follows:

Requests is a non GMO Python HTTP library that humans can safely enjoy.

Warning: unprofessional use of other HTTP libraries can lead to dangerous side effects, including security defects, redundant code, reinventing the wheel, document gnawing, depression, headache and even death.

The third-party module is not the default module, which means you need to install it. We use pip3 to install it.

To install pip3 first:

sudo apt-get update
sudo apt-get install python3-pip

Then install requests with pip3

sudo pip3 install requests

The above command will install the Requests module of Python 3 version in your system.

Get a simple web page

You can use the get() method to get any web page.

>>> import requests
>>> req = requests.get('https://github.com')
>>> req.status_code
200

The text attribute of req stores the HTML page returned by the server. Because the HTML text is too long, it will not be posted here.

Using this knowledge, let's write a program that can download files from the specified URL.

Write the code to the file / home / download py:

import requests

def download(url):
    '''
    From the specified URL Download the file from and store it in the current directory
    url: The URL of the page content to download
    '''
    # Check if the URL exists
    try:
        req = requests.get(url)
    except requests.exceptions.MissingSchema:
        print('Invalid URL "{}"'.format(url))
        return
    # Check whether the website was successfully accessed
    if req.status_code == 403:
        print('You do not have the authority to access this page.')
        return
    filename = url.split('/')[-1]
    with open(filename, 'w') as fobj:
        fobj.write(req.content.decode('utf-8'))
    print("Download over.")

if __name__ == '__main__':
    url = input('Enter a URL: ')
    download(url)

Test the following procedure:


You can see that a gfwlist has been added to the directory Bak file.

You may have noticed if__ name__ == '__ main__': This statement is used only when the current module name is__ main__ The statements in this if block will be executed only when it is executed as a script. In other words, when this file is imported into other files in the form of modules, the statements in the if block will not be executed.

You can modify the above program better. For example, you can check whether the same file name already exists in the current directory. os.path Modules can help you do this.

argparse command line parameter processing module

Do you remember the ls command? You can pass different options as command line parameters.

The module used here is sys, and all parameters passed in from the command line can use sys Argv get. If you want to process parameters, you can use the argparse module. Read this article file study.

TAB completion

First create a file: ~ / Python RC, write the following contents in the file:

import rlcompleter, readline
readline.parse_and_bind('tab: complete')


history_file = os.path.expanduser('~/.python_history')
readline.read_history_file(history_file)

import atexit
atexit.register(readline.write_history_file, history_file)

The next step is ~ / Set the PYTHONSTARTUP environment variable in the bashrc file to point to this file:

export PYTHONSTARTUP=~/.pythonrc

Now, from now on, whenever you open the bash shell, you will have a history of TAB completion and code input in the Python interpreter.

To use in the current shell, source is the bashrc file.

source ~/.bashrc

summary

This section explains what a module is and how to import it, and gives examples of the use of os and Requests modules. The attractive thing about Python is that it has many modules that can be used. For its own modules, you can take a look at Python 3 Official documents , for third-party modules, you can PyPI Look up. Many times you can find the right package to help you finish some work gracefully. For example, the argparse module helps you easily write a user-friendly command-line interface.

Topics: Python Programming