VS Code reports error "No module named'xxx'" when importing custom package

In the process of learning Python, when using VS Code for programming, I imported a custom Python package and reported an error: "No module named'processdata'"

Insert picture description here

principle:

1. If the imported package can be found under the current file directory, you can directly use from… import…

2. If the imported package cannot be found under the current file directory, you need to add the absolute location of the package to sys.path.

Solution:

The current file is ExtractAndSave.py, then you need to add the following code at the beginning of the file:

import sys,os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # __file__获取执行文件相对路径,整行为取上一级的上一级目录
sys.path.append(BASE_DIR)

Then from processdata import read_dcm.py the error disappears

Guess you like

Origin blog.csdn.net/liqiang12689/article/details/106097937