Selenium + Python automation Test unittest Framework Learning (iv) Python import module and package knowledge points

Source: Internet
Author: User

In writing the script, found to import some modules, often error prompts to import the module failed, here to bad to fill the Python import module knowledge points.

1. File lookup order at module import

In the script, the steps to import the XXX module are as follows:

(1) Create a new module

(2) Insert module into Sys.module

(3) Find the path to the module, looking in order to find the file currently using the Import module in the same level directory, followed by the Python search module path set Sys.path, and finally Python added the path of the default environment variable python path

(4) Module lookup to then load module

Therefore, if the module that needs to be imported is not in the same level directory, name uses Sys.path to add the path to the module

Import Sys

Sys.path ("Folder path of module, package path of module")

2. Module, Package

The module is a file with a. py suffix, and the package is the directory where the module resides, in order to identify the directory where the module is a reference package, you can create a new __init__.py file under the package, either empty or not empty.

When not empty, you can write a module import statement under a package in a file, because the __init__.py file is imported when the module is actually imported.

3. Import Module Statement format

Import Module Name

From Package name Import module name (only one module in the package is imported)

From package name. module Name Import function name (when a specific function in the module needs to be imported)

4..pycache file

The module is executed when it is imported, and the Python interpreter automatically generates a Pycache file in the same folder of the module to speed up the startup of the program.

Selenium + Python automation Test unittest Framework Learning (iv) Python import module and package knowledge points

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.