[100 days proficient in python] Day16: python module_module search directory and processing method when importing module exceptions

Table of contents

1 Search the directory where the module is located

2 The module is not in the search directory

2.1 Add the directory where the module is located to the PYTHONPATH environment variable

2.2 modify sys.path

2.3 Import using absolute path

2.4 Copy the module to any directory in the Python search path

2.5 Summary

3 Exception handling of other imported modules

3.1 The module is not installed

 3.2 Module name misspelled

3.3 The module file is missing

3.4 Module Path Issues


1 Search the directory where the module is located

        In Python, when you import a module, the interpreter searches the directories where the module is located in a specific order. These directories include:

  1. Current working directory: Python first searches the current working directory where the script is run.

  2. PYTHONPATH environment variable: If the PYTHONPATH environment variable is set, Python will search for modules in the order of the directories specified therein.

  3. Python installation directory: A default standard library directory will be set when Python is installed, and the module search will also be performed in this directory.

  4. Third-party library directory: If you use a third-party library, Python will search for modules in the third-party library's installation directory.

The search order is from front to back, and the search stops once a matching module is found.

Python will raise an exception if the specified module is not found ModuleNotFoundError.

You can view Python's module search path with the following code:

import sys

print(sys.path)

The above code will output a list containing the search path, the first element is the current working directory, then the directory specified in the PYTHONPATH environment variable, and then the Python installation directory and third-party library directory.

as follows:

2 The module is not in the search directory

If the module to be imported is not in the search directory, the following errors often appear, such as: ModuleNotFoundError: No module named 'numpy'

In this case, if there is no third-party module package installed, use pip install numpy to install it.

Also for custom modules. like

 

 In this case, you can take the following methods to solve the problem:

2.1 Add the directory where the module is located to the PYTHONPATH environment variable

        Adding the directory where the module is located to the PYTHONPATH environment variable allows the Python interpreter to search for the module in the specified directory.

In the Windows system, you can add the directory to the PYTHONPATH environment variable with the following command, enter in bash:

set PYTHONPATH=%PYTHONPATH%;/path/to/module_directory

On Linux and macOS systems, the directory can be added to the PYTHONPATH environment variable with the following command:

export PYTHONPATH=$PYTHONPATH:/path/to/module_directory

Doing so causes the Python interpreter to automatically search for modules in the specified directory on startup.

2.2 modify sys.path

        Use the method in a Python script sys.path.append()to add the directory where the module is located to the search path.

import sys
sys.path.append('/path/to/module_directory')

         If you know the absolute path of the module, you can also use sys.path.append()Add the absolute path to the Python search path, and then importimport the module with the statement. Doing this also solves the problem of modules not being in the search path.

        Suppose we have a custom module file mymodule.py, located /path/to/module_directoryin a directory, and we know its absolute path is /path/to/module_directory/mymodule.py. Now we want to import this module in another script, but /path/to/module_directorynot in Python's search path.

        We can add to the search path using sys.path.append()will /path/to/module_directoryand then importimport the module via the statement:

import sys

# 添加模块所在目录到搜索路径中
sys.path.append('/path/to/module_directory')

# 现在可以导入模块
import mymodule

# 使用导入的模块中的函数或变量
mymodule.my_function()

In this way, the Python interpreter will search for the module in the specified absolute path, and successfully import mymodulethe module, so that the functions or variables in it can be used. This temporarily adds specific directories to the search path, making importing modules more flexible. But please note that this modification is effective at runtime, when the script ends, the search path will be restored to its original state.

2.3 Import using absolute path

        In Python, modules can be imported using absolute paths. This approach works for module files located in any directory, not limited to Python's search path.

        Suppose we have a custom module file mymodule.py, located /path/to/module_directoryin a directory, and we know its absolute path is /path/to/module_directory/mymodule.py. Now we want to import this module in another script.

(1) Modules can be used impto import modules with absolute paths:

import imp

# 指定模块文件的绝对路径
module_path = '/path/to/module_directory/mymodule.py'

# 使用imp.load_source()函数导入模块
mymodule = imp.load_source('mymodule', module_path)

# 使用导入的模块中的函数或变量
mymodule.my_function()

Through imp.load_source()functions, we can import modules according to the specified absolute path. This method does not depend on Python's search path, so module files from arbitrary locations can be imported.

        It should be noted that impmodules have been marked as deprecated in Python 3.4 and above, and it is recommended to use importlibmodules instead.

        (2) In newer Python versions, you can use importlib.util.spec_from_file_location()the andimportlib.util.module_from_spec() function to implement absolute path import modules:

import importlib.util

# 指定模块文件的绝对路径
module_path = '/path/to/module_directory/mymodule.py'

# 使用importlib.util.spec_from_file_location()创建模块规范
spec = importlib.util.spec_from_file_location('mymodule', module_path)

# 使用importlib.util.module_from_spec()加载模块
mymodule = importlib.util.module_from_spec(spec)

# 将模块规范绑定到模块
spec.loader.exec_module(mymodule)

# 使用导入的模块中的函数或变量
mymodule.my_function()

Using importlibmodules provides a more flexible and powerful way to import modules, especially in Python 3.4 and above.

2.4 Copy the module to any directory in the Python search path

        Copy the module to either the Python standard library directory, the third-party library directory, or the current working directory so that the Python interpreter can find the module.

        However, this method is not recommended, because directly modifying Python's standard library directory or third-party library directory may cause unpredictable problems, and copying modules may make the code difficult to maintain.

2.5 Summary

Note: The above method recommends using the first method or the second method, that is, adding the directory where the module is located to PYTHONPATH or sys.path, which can ensure that the module can be imported correctly in any location. Modifying Python's standard library directories or third-party library directories directly may cause unexpected problems and is not recommended.

3 Exception handling imported by other modules

When you encounter an error that the related module cannot be found when importing a module, there are usually several common causes and solutions:

3.1 The module is not installed

If you try to import a third-party module, but that module has not been installed in your Python environment, it will result in a module not found error. The workaround is to install the module using a package management tool such as pip. For example, install requeststhe module with the following command:

pip install requests

 3.2 Module name misspelled

        Check that the name of the module you imported is spelled correctly. Python is case sensitive to module names.

        Different Python versions: If you are working with different versions of Python, it may result that some modules are only available in certain versions. Make sure you are running the code in the correct Python environment.

3.3 Module files are missing

        Check that the module file exists and make sure it's properly placed under the module search path. Module files must .pyhave an extension.

3.4 Module Path Issues

        If you write a custom module or a third-party module that is not in the search path of the Python interpreter, it will also cause an error that the module cannot be found. For solutions, see 2.1, 2.2, 2.3, and 2.4 above.

        If you still encounter the error that the module cannot be found, please check the above reasons carefully and take corresponding solutions according to the specific situation.

Guess you like

Origin blog.csdn.net/qq_35831906/article/details/131913319