#๐Ÿ”’ Python Import Warning in VS Code with venv: "Import ... could not be resolved"

40 messages ยท Page 1 of 1 (latest)

slim anchor
#

I am encountering a persistent "Import ... could not be resolved" warning in VS Code, even though my Python code runs correctly. This issue occurs specifically when working within a virtual environment (venv). The warning appears for a module located in the same directory as the importing script.
My project has the following structure:
โ””โ”€โ”€ CODE/
โ””โ”€โ”€ projVector (venv) /
โ””โ”€โ”€ three_dimensional_vector/
โ””โ”€โ”€ vector/
โ”œโ”€โ”€ vector.py
โ””โ”€โ”€ vector_errors.py
```vector.pycontains the lineimport vector_errors(orfrom . import vector_errors`).

Problem:

VS Code displays the warning "Import 'vector_errors' could not be resolved" in vector.py. However, when I execute vector.py using the Python interpreter within my activated venv, the code runs without any import errors.

Steps Taken:

I have tried the following steps to resolve the warning:

  • Verified venv activation: The virtual environment (projVector) is activated in VS Code. The output of sys.path confirms that the venv's site-packages directory is included.

  • Checked sys.path: The sys.path output shows that the directory containing vector.py is listed, so the Python interpreter should be able to find vector_errors.py at runtime.

  • Added __init__.py files: I added empty __init__.py files to both the three_dimensional_vector/ and three_dimensional_vector/vector/ directories.

  • Used explicit relative import: I changed the import statement in vector.py to from . import vector_errors.

  • Checked VS Code Python settings: I have reviewed VS Code settings related to type checking and linting.

  • Reloaded VS Code: I have reloaded the VS Code window and restarted the language server.

  • Switched to global interpreter: The warning disappears when I switch VS Code to use the global Python interpreter, indicating the issue is related to the venv context within VS Code.

sly edgeBOT
#

@slim anchor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

unique bay
slim anchor
#

yes

unique bay
#

OK so... whats the problem?

slim anchor
#

but only resolves when i select global interpreter

#

is ok to use it?

unique bay
#

You should be using it in a venv

#

OK the folder structure is throwing me off.

slim anchor
#

tell me what to use, global intrepeter or venv intrepeter, i thought it would be problem if i use global one

inner belfry
unique bay
inner belfry
#
.
โ””โ”€โ”€ CODE/
    โ””โ”€โ”€ projVector (venv) /
        โ””โ”€โ”€ three_dimensional_vector/
            โ””โ”€โ”€ vector/
                โ”œโ”€โ”€ vector.py
                โ””โ”€โ”€ vector_errors.py
#

for better view

unique bay
#

Yea I was about to guess that

#

Thanks โค๏ธ

slim anchor
#

what that means?

unique bay
#

Is projVector a proper venv folder or just a regular folder?

#

a proper venv folder, usually has bin and lib and site-packages in it

slim anchor
#

its a venv creatd using python -m venv projvector

unique bay
#

A regular folder does not

#

yeah you shouldn't be coding in that folder

#

It should be outside of it

slim anchor
#

but then will it isolate my project ?

unique bay
#

Its isolated if you activate venv

#

For example, this is my project folder. New project. No venv yet

#

Then after creating my venv to .myvenv it looks like this

slim anchor
#

ohh ;O, so like my main folder is "Code" so should i clone the vector in "code" and just activate venv, will that make it isolate?

unique bay
#

Activating it looks like this

#

My venv is activated just by calling .myvenv/Scripts/activate.ps1

#

Automatically, activating a venv isolates your project dependencies from using other dependencies

slim anchor
#

ok thanks, now everything is working

#

chatgpt should have told me this in the very start

#

iclose

#

1close

unique bay
#

Type !close ๐Ÿ™‚

slim anchor
#

!close

sly edgeBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.