How to introduce modules in Python

Updated on technology 2024-05-14
4 answers
  1. Anonymous users2024-02-10

    A python module is a python file to: The end of py, including python object definitions and python statements, can make python segments more logical, easier to use, and easier to understand.

    import statement.

    Custom modules can be introduced by import statements, the operation steps are to import the module first, and then call the functions contained in the module, you can put the custom module into the current directory, which is convenient for interpreter path search, the following is an example of importing a custom module and calling the world function:

    #!/usr/bin/python

    # -*coding: utf-8 -*

    Import the module.

    import hello

    You can now call the functions included in the module.

    world")

    The output is:

    hello world!

    The above example can also be used from....import method to import a specified part from a module to the current namespace, the above functions can be written as:

    #!/usr/bin/python

    # -*coding: utf-8 -*

    from hello import world

    If you want to import all the content in the module into the current namespace, you can use from....import*, as follows:

    #!/usr/bin/python

    # -*coding: utf-8 -*

    from hello import *

    1.Current directory;

    3.If it can't be found, python will look at the default path.

  2. Anonymous users2024-02-09

    Python module, is a python file that starts withpy, which contains the python object definition and python statement.

    Modules allow you to logically organize your Python segments.

    Assigning relevant ** to a module will make your ** more useful and easier to understand.

    Modules can define functions, classes, and variables, and modules can also contain executable **.

    Introduction of modulesOnce the module is defined, we can use the import statement to import the module, and when the interpreter encounters the import statement, if the module is in the current search path, it will be imported.

  3. Anonymous users2024-02-08

    In python with import or from....import or from....import...

    as...to import the corresponding module, which works and uses the same method as the include header file in C. In fact, it is to introduce some mature libraries and mature methods to avoid reinventing the wheel and improve the development speed.

    Python's import method can be used to import modules from the system, or to introduce common modules that we have written, which is very similar to PHP, but the specifics are not quite the same. Because PHP indicates the specific path of the introduced file at the time of introduction, and the file path cannot be written in Python for introduction.

    Here are a few cases of import:

    Python's method of containing modules in subdirectories is relatively simple, and the key is to be able to find the path to the module file in it.

    Here are a few common scenarios:

    1) The main program is in the same directory as the module program

    The following program structure is as follows:

    src--- if you import module mod1 in the program, you can directly use import mod1 or from mod1 import *;

    2) The directory where the main program is located is the parent (or grandparent) directory of the directory where the module is located.

    The following program structure is as follows:

    src---mod2

    - If you want to import module mod2 into the program, you need to create an empty file init in the mod2 folderpy file (you can also customize the output module interface in this file); Then use from import * or import

    3) The main program imports modules in the upper directory or modules under other directories (peers).

    The following program structure is as follows:

    src---mod2

    - sub - if you import modules mod1 and mod2 into the program. First of all, you need to create init under mod2py file (same as (2)), you do not need to create this file under src. Then the call is as follows:

    The following program execution methods are executed in the directory where the program file is located, such as cd sub; Execute python after that

    Rather, it's in cd src; Execute python after thatThere is no guarantee that the Python sub execution in the src directory will be successful.

    import sys

    import mod1

    import

  4. Anonymous users2024-02-07

    Write one, define a function a, a class b, and then use an instance of b inside a. I'm importing from a import a, and then calling a in

    Nothing.

Related questions
8 answers2024-05-14

Let's point out a mistake first: you only have one element in list1, which should be taken out with list1[0]; >>>More

7 answers2024-05-14

Have you been poisoned or installed any software that has not been uninstalled normally, right? >>>More

3 answers2024-05-14

How to connect the network module, teach you to connect the network module on the spot.

9 answers2024-05-14

First of all, you have to understand that there are only two possibilities for this kind of problem:1You didn't install this software properly; 2.Environment variables are not configured. >>>More

10 answers2024-05-14

Use. pyzipfile

The method is rudimentary. >>>More