Home > Mobile >  VS Code recognized the import but still shows ModuleNotFoundError
VS Code recognized the import but still shows ModuleNotFoundError

Time:06-06

I am currently developing some packages with multiple subfolders. However, when I try to import one function from a subfolder into another one, the VS code shows green but when I run it, the terminal says ModuleNotFoundError. The folder structure is shown below.

Folder
|-Subfolder_1
|-- __init__.py
|-- fitting.py
|- Subfolder_2
|-- __init__.py
|-- process.py
|- main.py

When I import subfolder_1 to main.py, it works fine, but when I try to import fitting.py to process.py, the problem occurs. I tried to mess around with the settings in vscode but nothing worked. I also tried to just put them in the same subfolder, but when I do that, the process.py functions properly but main.py throws an error for modules not found since I use process.py in the main.py.

If anyone has some idea of how to fix it, it would be greatly appreciated.

CodePudding user response:

If you are using virtual environment then you need activate it.

for Mac OS / Linux

source <path to env>/bin/activate

for Windows

<path to env>\Scripts\activate

CodePudding user response:

You can try adding a path. However, it should be noted that vscode searches for files with the workspace as the root directory.

Here I assume that you have a workspace that contains your directory structure.

You can try:

import sys
sys.path.append(".\Folder\Subfolder_1")
import fitting
  • Related