SoFunction
Updated on 2024-11-15

Detailed explanation of the implementation principle of import module import in Python

What is a Module

A .py file is a module, i.e., a Module. there are three types of modules: python standard libraries, third-party modules, and application-specific modules.

  • import statement - import module
  • Directory - folder (empty)
  • Package - one more __init__.py than folder
"""
file: 
"""
def add(x, y)
	return x+y
def sub(x, y)
	return x-y
print(‘hello cal')
"""
file: 
"""
import cal 
print((1, 2))

What did import do?

When import imports a module, it first executes the imported file, for example, when we run it, print('hello cal') is also executed, because import cal runs it. So, we write only functions (aka functions) in the module file and not executable statements.

  • Execution of the py file being introduced, even if only one variable is introduced (from cal import add), will run the entire file.
  • Introducing variable names → filename variables

Introduction of multiple modules

import cal, time

Introducing only one method

# Introduce only one method
from cal import add
print(add(1, 2)) #Can be used directlyadd,No need to addcal.

Introducing all methods is not recommended, you don't know what variables have been introduced, and there may be duplicates of the names of variables in this file with the introduced variables.

# Introduce all methods - not recommended, you don't know what variables are being introduced and there may be duplicate names of variables in this file and introduced variables.
from cal import *
#The new variable name overwrites the old variable name
#+++++++++++++++++++++++++++
from cal import *
def add(x, y)
	return x+y+100
print(add(1, 2))
#+++++++++++++++++++++++++++
def add(x, y)
	return x+y+100
from cal import *
print(add(1, 2))
#+++++++++++++++++++++++++++

import search path

import sys
print() #View Path

path contains the paths defined by python itself and the path of the currently executing py file, which means that the path of the currently executing py file is automatically added to it, and import searches for the introduced variables according to these paths. You can also add paths manually

from path import cal
#pathjust likecalPath

The principle of import import module

First of all, import will find the file according to the path, according to the path to find the module to load the module into memory to execute once, the execution is to copy the contents of the module to the current file to execute. import import is to import the module from the disk to the disk file into the memory, the speed is slower, in fact, in the import will have an import cache, the same module in the first import will have a cache when the first time will have a cache, so sometimes you may encounter such a problem, the imported file has been deleted, but the program can still run, this is because the program uses the cache of the imported file has been deleted. In fact, there will be an import cache when importing, the same module will have a cache when importing for the first time, and then importing again will be using the cached import, so sometimes you may encounter such a problem, the imported file has been deleted, but the program can still run, this is because the program is using the cached import module.

from path import mode, which is equivalent to splicing the path once path\, which is the job of from. Path splicing is based on the path of the current executable file.

在这里插入图片描述

When many modules are introduced, there will be many py files in a directory, which are usually put as the file to be executed, which is the entrance of the whole program. And the logic of the main file is called, which contains the main logic of the program, other functions are put into other files as a module. When we run, run, by going to call the main logic. That means only bin is executable, the rest of the file should not be used as an executable file.

As I said before, the path where the currently running program is located will be added to the running file of the whole program, that is to say, the path will only be added to the file, if it is said that the file has the following import relationship

在这里插入图片描述

If and in the same directory, then do not need to add the path can be imported, but if and these two files are not in the same directory (such as in the previous directory), then the import should be added to the path, but so in the execution of the error will be reported because of the indirect import, and only add the path without adding the path, as I said before, it will only include the path of the current runtime path, that is, the path. There are two solutions:

  • The path added in the from path import cal;
  • Add the path to the center;

file Get current file name

(__file__)  # Get current file path
((__file__))  #Get the previous path of the current file

pycharm itself gets the absolute path based on the current filename and returns the absolute path to us via () (file), but when running in the terminal, the terminal does not have this feature, we need to find the absolute path ourselves, and then find the filename according to the absolute path, and back out the previous directory.

p = (__file__) # Get the absolute path to the current file
BASEDIR = ((p))
(BASEDIR)

In fact, these three steps are equivalent to adding the previous directory of the currently running file to the environment variable by means of a relative path. If we add the environment variable by absolute path, when we change the computer or environment, the environment variable will be invalid. If we find out the relative path and add it to the environment variable through the program, we will be able to find the environment variable as long as we copy the whole project to another machine.

Above is a detailed explanation of Python import module import principle of realization of the details, more information about Python import module import please pay attention to my other related articles!