Vscode Import Error for Python Module

Cannot Import internal modules in VSCode [closed]

setup.py does not work in VSCode, you can modify the PYTHONPATH in VSCode through (terminal.integrated.env.*) and/or within an .env file:

The PYTHONPATH environment variable specifies additional locations
where the Python interpreter should look for modules. In VS Code,
PYTHONPATH can be set through the terminal settings
(terminal.integrated.env.*) and/or within an .env file.

When the terminal settings are used, PYTHONPATH affects any tools that
are run within the terminal by a user, as well as any action the
extension performs for a user that is routed through the terminal such
as debugging. However, in this case when the extension is performing
an action that isn't routed through the terminal, such as the use of a
linter or formatter, then this setting will not have an effect on
module look-up.

When PYTHONPATH is set using an .env file, it will affect anything the
extension does on your behalf and actions performed by the debugger,
but it will not affect tools run in the terminal.

If needed, you can set PYTHONPATH using both methods.

An example of when to use PYTHONPATH would be if you have source code
in a src folder and tests in a tests folder. When running tests,
however, those tests can't normally access modules in src unless you
hard-code relative paths.

To solve this problem, you could add the path to src to PYTHONPATH by
creating an .env file within your VS Code workspace.

PYTHONPATH=src

official docs

Visual Studio Code tells me it can't import python module, yet it runs the code

When importing modules in other folders, VSCode searches for files from the parent folder of the currently running file by default. For example: "import models.c" in "a.py", the parent folder "controllers/" of "a.py" does not have "models", so the terminal displays a warning.

When I removed the same level file "__init__.py" of "main.py", the terminal did not display errors and warnings:

enter image description here

For "a.py", I added the following settings in "launch.json", which adds the project folder path to the system path for VSCode to find:

"env": {
"PYTHONPATH": "${workspaceFolder}"
}

Debug a.py:

enter image description here

Update:

On the premise that the code can run, I use the following settings to turn off the "import-error" displayed by Pylint:

"python.linting.pylintArgs": ["--disable=E0401"],

enter image description here

Import errors on VS code

Use the following statement to add the path of the file that needs to be imported to the system path to help VSCode find it:

import os,sys 
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

The following is part of the project I created that you provided:

enter image description here

Result:

enter image description here

The following points need to be noted:

  1. When importing, the subfolders are connected with.

  2. Please avoid using files and folders with the same name to prevent confusion when VSCode finds modules.

  3. If the result is executable but there is still a wave line, you can add in settings.json: "python.linting.pylintArgs": [ "----extension-pkg-whitelist=1xml" ],

Update:

According to the code of the link you provided, I reproduced the problem you described locally, and the solution is as follows:

  1. Comment out the content "from SLCT import *" of logparser-master\logparser\SLCT_init_.py.

  2. Add import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    to SLCT_demo.py

operation result:

enter image description here

VS code can't import module which is already installed

Have you selected the correct Python Interpreter inside of VSCode?

If you have the Python extension installed, you can go into your command prompt, type in Python: Select Interpreter, select the correct environment and VSCode should be able to recognize it.

In case that's the issue :)



Related Topics



Leave a reply



Submit