This is the 17th day of my participation in the August Text Challenge.More challenges in August

1. The description of package

When you have multiple modules in a project, you need to organize them. We put together modules with similar functions to form “packages”.

Essentially, a bag is one that has to have one__init__.pyThe folder

Typical structure is as follows:

Packages can contain modules and subpackages. Similar to the folder can have files, can also have subfolders.

For example, in the seleniumLibrary package file, there is still an __init__.py file

โŒ› Summary: Packages must have an __init__.py file in the directory, which can contain multiple subpackages and modules

2. Create packages

Creating a package manually consists of two main steps:

  1. Create a folder whose name is the package name of the package

  2. Add an __init__.py file to the folder

Create package in Pycharm

  1. Select the path to create the package
  2. Right click: New->Python Package
  3. Python will automatically generate a package for us with an __init__.py file

For example, we create a Juejin package with a folder directory structure

package Juejin
|
|---Juetime
|   |---__init__.py
|   |---module_time.py
|
|---__init__.py
|---module_hello.py

Copy the code

Juejin package module module_time.py module contents are as follows

import time

name = "Juejin.module_time"

def currentdate():

    return  time.strftime("%y/%m/%d",time.localtime())
Copy the code

The Juejin package module module_hello.py contains the following contents:

name = "Juejin.module_hello"

def hello(name):

    print("hello %s,welcome to juejin",name)
Copy the code

3. Import the package

The common modes of importing packages are as follows:

  1. from package import item

    Item can be a package, a module, a function, a class, or a variable

  2. import item1,item2...

    Item must be a package or module, not another

โœจ The essence of importing a package is to import the __init__.py file of the package

๐ŸŽฏ**__init__.py has three core functions: **

  1. As a package identifier, cannot be deleted
  2. Used to implement fuzzy import
  3. The import package is essentially an __init__.py file, which can be initialized in an __init__.py file, and requires uniform code execution.

๐Ÿ“ for example: Package Juejin import module operation

  • Import the module_time module with import

    import Juejin.Juetime.module_time
    Copy the code

    ๐Ÿ“Œ must be referenced with the full name

     Juejin.Juetime.module_time.currentdate()
    Copy the code

  • Use the FROM package import module to import the module_time module

     from Juejin.Juetime import module_time
    Copy the code

    ๐Ÿ“Œ You can directly use the mokke name

    module_time.currentdate()
    Copy the code

  • Using the from… Import imports functions directly

    import Juejin.Juetime.module_time import currentdate
    Copy the code

    ๐Ÿ“Œ Use the function name directly

    currentdate()
    Copy the code

4. Use * to import the package

Python also provides the import * fuzzy import file system to find all the submodules in the package and import them

๐ŸŒŸ but will it take a long time for the program to find the package?

๐Ÿ‘‰ Python’s solution is to provide an explicit package index: define the __all__ variable of the bit-list in _init__.py

__all__ = ["Module1", "Module2", "subPackage1", "subPackage2"]
Copy the code

โœ continues to import the module operation mode with package Juejin

  1. Add the __all__ variable to the __init__.py file in the Juejin package

    __all__ = ["module_hello","Juetime"]
    Copy the code
  2. Third party call

    ```
     from Juejin import *
     module_hello.hello("Bob")
    ```
    Copy the code

๐ŸŒŸ it is recommended not to use the import * method in formal projects

5. In-package references

If it is a reference within a subpackage, the submodules can be introduced relative to each other

  • Peer directory import

    From. import Module nameCopy the code
  • Upper-level Directory Import

    from .. The import module nameCopy the code

๐Ÿ“ข runtime relative import with no known parent package error: Runtime relative import with no known parent package

The reason is that the __name__ of the parent package is equal to __main__ and package = None

Workaround: Use import or from import to import packages

conclusion

In this installment, we’ll look at packages that provide import and from… The import syntax helps us import modules better.

In contrast to the previous module import, the package import also provides an __init__.py file that defines the __all__ variable to import all modules and subpackages within the package.

The above is the content of this issue, welcome to the big people like the comments and corrections, see next time ~แƒฆ(ยด แด— ยท ‘) than the heart ๐ŸŒน๐ŸŒน แด— Stan แƒฆ