4

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 (i.e. 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.

3
  • Can the code run successfully? Please use the form of tree diagram to describe the structural relationship of related files and folders. Commented May 12, 2021 at 5:47
  • The code can run successfully, the structure of files and folders updated. Commented May 13, 2021 at 12:31
  • Which python language service are you using? Please try to use the language service provided by "Jedi" or "Microsoft". Commented May 14, 2021 at 1:22

3 Answers 3

7

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

restarting the IDE does not help, and the module is just a file in the same folder, so it just cannot be installed.
I had a different situation with similar warning (Import "torch" could not be resolved Pylancereport MissingImports) and restarting the IDE helped me resolve it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.