Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to import loop method in Python

Shulou Source: shulou.com Published: 2022-06-02 05:32:53 09月19日 Update

How to import loop method in Python, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

From cli4vof import cli4vof

# command line interface utility function

Def cli_util ():

Pass

# overly massive handlers for the command line interface

Def omh5cli ():

:

Cli4vof ()

:

Omh5cli ()

Assume that most controllers use the (actually empty) utility functions here. The OMH of the command line interface is encapsulated in the omh5cli () function. If we want to add a new command, it will be called.

Now that this module is growing, some smart engineers will decide to put new commands into isolated modules, providing only hooks to access new things in the original module. This makes it easier to manage the code, and if you find bug in the new additions, you don't have to search through a few megabytes of Python files.

In our example, an excited manager asked us to add a "very good feature". Instead of integrating new content into omh5cli.py, we will create a new cli4vof.py script:

Import omh5cli

# command-line interface for a very outstanding feature

Def cli4vof ():

Omh5cli.cli_util ()

As mentioned earlier, utility functions are required for every command, and since the code cannot be copied from the main controller, we import the main module and add a call to omh, omh5cli () in our controller.

The problem is that the main controller omh5cli imports our cli4vof module (the function to get the new command), while cli4vof imports omh5cli (to get the utility function). Module imports fail because Python attempts to import a module that was not previously fully imported:

$python omh5cli.py

Traceback (most recent call last):

File "omh5cli.py", line 3, in? From cli4vof import cli4vof

File "/ usr/prod/cli4vof.py", line 3, in?

Import omh5cli

File "/ usr/prod/omh5cli.py", line 3, in?

From cli4vof import cli4vof

ImportError: cannot import name cli4vof

Notice the circular import of cli4vof shown in the trace return message. The problem is that to call utility functions, cli4vof must import omh5cli. If it does not need to do so, then omh5cli will successfully import cli4vof and the program will execute normally. But here, omh5cli tries to import cli4vof, and cli4vof tries to import omh5cli. In the end, no one will finish the import, causing an error. This is just an example of an import loop. In fact, there will be more complex situations in practical applications.

Solving this problem almost always removes one of the import statements. You will often see the import statement at the end of the module. As a beginner, you just need to get used to them. If you encountered the import statement at the bottom of the module before, now you know why. In our example, we cannot move import omh5cli to the end because the omh5cli () name has not been loaded when cli4vof () is called.

$python omh5cli.py

Traceback (most recent call last): File "omh5cli.py", line 3, in? From cli4vof import

Cli4vof

File "/ usr/prod/cli4vof.py", line 7, in?

Import omh5cli

File "/ usr/prod/omh5cli.py", line 13, in?

Omh5cli ()

File "/ usr/prod/omh5cli.py", line 11, in omh5cli cli4vof ()

File "/ usr/prod/cli4vof.py", line 5, in cli4vof omh5cli.cli_util ()

NameError: global name 'omh5cli' is not defined

Our solution is to move the import statement inside the cli4vof () function:

Def cli4vof ():

Import omh5cli

Omh5cli.cli_util ()

This is the answer to the question about how to import the circular method in Python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Modules functions questions commands methods loops tools statements examples code content maker just controller more master try help control answer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Linux Shulou Tech Info MariaDB