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:
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:
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"],
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:
Result:
The following points need to be noted:
When importing, the subfolders are connected with
.
Please avoid using files and folders with the same name to prevent confusion when VSCode finds modules.
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:
Comment out the content "
from SLCT import *
" of logparser-master\logparser\SLCT_init_.py.Add
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
toSLCT_demo.py
operation result:
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
How to Get Maximum Length of Each Column in the Data Frame Using Pandas Python
How to Map True/False to 1/0 in a Pandas Dataframe
How to Check Whether a Number Is Divisible by Another Number
How to Delete Tkinter Widgets from a Window
How to Install Pip for a Specific Python Version
Converting a List into Comma Separated and Add Quotes in Python
Printing the Number of Days in a Given Month and Year [Python]
Easiest Way to Ignore Blank Lines When Reading a File in Python
How to Start a Background Process in Python
How to Transfer Data from One Worksheet into Another Using Python in the Same Workbook
How to Convert Number 1 to a Boolean in Python
Python Tkinter Return Value from Function Used in Command
How to Constantly Run Python Script in the Background on Windows
Comparing Items in Lists Within Same Indices Python
Bold Formatting in Python Console
Efficient Way of Having a Function Only Execute Once in a Loop