[PYTHON] VS Code Pylint is annoying with import! !! !! !!

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). ** **

at first

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.

0. Environment

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. 2019-12-14 (2).png

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

2019-12-14 (3).png

** Whether to change " python.jediEnabled ": false to "python.jediEnabled": true Delete this section itself. ** **

2019-12-14 (4).png

2.Unable to import

2019-12-14 (6).png

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.

2019-12-14 (9).png

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.

Summary

  1. Whether to change "python.jediEnabled": false to "python.jediEnabled": true Delete this section itself.
  2. Create a pylintrc in the workroot folder and give init-hook the folder that contains main.py.

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

VS Code Pylint is annoying with import! !! !! !!
Python with VS Code (Windows 10)
[Python] Lint pylint is not installed is displayed [VS Code]
Debug Python with VS Code
Resolve VS Code unresolved import error
Try running Jupyter with VS Code
Install python with mac vs code
Display Japanese graphs with VS Code + matplotlib
I tried Flask with Remote-Containers of VS Code
Python (Windows 10) Virtual Environment / Package with VS Code
VS Code settings
Debug with VS Code using boost python numpy
Use Python in Anaconda environment with VS Code
Use Docker development container conveniently with VS Code
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Try touching the micro: bit with VS Code + Python
VS Code settings for developing in Python with completion
VS Code says there is an error in cv2
Revive symbol search in Python workspace with VS Code
Enable intellisense for external libraries with Pipenv + VS Code
Remote debug Django environment created with docker-compose with VS Code
Strengthen with code test ⑨
Strengthen with code test ③
I want to get rid of import warnings from Pyright and pylint in VS Code
Strengthen with code test ⑤
Strengthen with code test ④
Strengthen with code test ②
Import tsv with Python
Hamming code with numpy
Strengthen with code test ①
Strengthen with code test ⑧
Strengthen with code test ⑨
Settings when developing App Engine / Python apps with VS Code
Let's run jupyter natively supported by VS Code with python3.8
Try Fortran with VS Code up to debug settings. [Win10]
Settings to debug the contents of the library with VS Code
The story that Python stopped working with VS Code (Windows 10)
Using jupyter notobook with VS Code (mac OS) (personal memo)
Comfortable LaTeX with Windows Subsystem for Linux and VS Code
Build a Python environment with WSL + Pyenv + Jupyter + VS Code