This article is more like a squeeze of errors. ** Once you have solved the problem, you should also read this article (https://qiita.com/ysk24ok/items/2711295d83218c699276). ** **
VS Code Pylint is annoying with import! !! !! !! What is it? I'm talking about
When writing Python in VS Code When I try to import my own module relative to it, I almost always get an error.
The following directory structure is used in the global environment (without using the virtual environment).
~
-Sample
-main.py
-__init__.py
-sub_methods
-method1.py
-method2.py
-out.py
Each has the following code.
main.py
print("main")
from sub_methods import method1
__init__.py
print("__init__")
import os
originaly = os.getcwd
os.chdir(os.path.dirname(os.path.abspath(__file__)))
from . import main
os.chdir = originaly
method1.py
print("method1")
import method2
#from sub_methods import method2
method2.py
print("method2")
out.py
print("out")
from Sample import main
1.unresolved import Even in this state, there is a warning.
This ʻunresolved import'...'` will come up when you do a relative import if it is deeper than the root of the workspace.
Even if it is said that it is unsolved, it feels like ... To calm this down
Open Ctrl + Shift + P
> Settings.json
** Whether to change " python.jediEnabled ": false
to "python.jediEnabled": true
Delete this section itself. ** **
2.Unable to import
shut up! The current directory is Sample
because I start main directly! !! !! !!
…e? Start from out
from sub_methods import method1
to from .sub_methods import method1
Rewrite to ...?
Do not shift the work root and the current directory ...?
Chit, I'm sorry ... I'm sorry.
Joking aside, if you call method2 from method1 instead of just main,
By all means, you will import with Sample
as the current directory.
method1.py
print("method1")
import method2
#from sub_methods import method2
output
ModuleNotFoundError: No module named 'method2'
Change>
method1.py
print("method1")
#import method2
from sub_methods import method2
output
main
method1
method2
I have no choice but to write it like this, but if I get an error, I can't help but be distracted.
This can be suppressed by making Pylint recognize the ** current directory (here Sample
) as a library folder. ** **
lib
-os
-...
-numpy
-...
...
But if it's the default library recognition, here
lib
-os
-...
-numpy
-...
-main
-sub_methods
-method1
-method2
If you let me recognize it like
You shouldn't get angry if you write from sub_methods method2
like from os import path
.
Therefore, ** Create the following files in the work root folder **
pylintrc
init-hook="./Sample"
Now when you reload lint or restart VS Code, you should no longer see the error.
This article is more like a squeeze of errors. ** Once you have solved the problem, you should also read this article (https://qiita.com/ysk24ok/items/2711295d83218c699276). ** **
Recommended Posts