简体   繁体   中英

Import [Module] could not be resolved (PylancereportMissingImports), with module in the same folder/directory

The first few lines of the code of evaluation.py :

import os
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
import numpy as np
from dataset import CLSDataset # warning is reported here
from tqdm import tqdm

The structure of the folder:

./
|-dataset.py
|-dictionary.py
|-evaluation.py
|-model.py
|-models/
  |-[some files]
|-__pycache__
|-train.py

Notice that dataset.py is in the same folder as that of evaluation.py and https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings says that The language server treats the workspace root (ie folder you have opened) as the main root of user module imports. But it still throws an warning of "Import dataset could not be resolved".

I tried to add the

{
    "python.analysis.extraPaths": ["./"]
}

on the settings.json of both local and remote files, but it does not help.

In VSCode, go to the main window and do the following:

  1. Do Ctrl+Shift+P (for Windows) and Command+Shift+P (for Mac)
  2. Scroll and go to Python Select Interpreter
  3. Now select whichever python version is installed in your system.
  4. You're good to go!

Do upvote if it helps you!

Dataset is a relative import.

Add an __init__.py file your directory (a file with no content). Then try:

from .dataset import CLSDataset

That being said, Python imports are a tricky business. It looks like we're only getting a glimpse of one part of workspace setup.

(I am surprised that your code runs as-is.)

Consider scaffolding your project with cookiecutter . Also try out the vs code python project tutorials . You don't have to remake your entire project but these will give you a starting point for understanding where your current project goes awry.

I recently had the same error, just try restarting the IDE. You probably installed it thought the code was already open. Or you haven't installed the module at all.

Edit:
I'm sorry I understood it wrong, please send the code.

Edit2:
You need to add the .py at the end of the import if it is a .py file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM