VS Code pylint(import-error) “Unable to import” subsub-module from custom directory

老子叫甜甜 提交于 2021-01-01 03:58:20

问题


I have organized my self-written Python scripts within a tree of several sub-directories, starting from the parent directory "Scripts" which is already included in "python.autoComplete.extraPaths" within the settings-json:

"python.autoComplete.extraPaths": ["/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts",
                                       "/home/andylu/anaconda3/lib/python3.7/site-packages"]

Apart from that, I've included a Python environment-file:

"python.envFile": "/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Visual_studio_code/vscode_own_scripts.env"

which contains the line

export PYTHONPATH=/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts:/home/andylu/anaconda3/lib/python3.7/site-packages

All of this worked out great before, where all my scripts were distributed just over 1 single directory level, like so:

+---Scripts
|   +---General
|   |   +---script_one.py
|   |   +---script_two.py

When I imported within any python-script e.g. script_one.py, I started the script with

import sys
sys.path.append(
    "/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/"
)

import General.script_one as one

and pylint recognized this imported script correctly without throwing the aforementioned VS Code pylint(import-error).


Now, the situation is different. The scripts had become so many, that I split up the subfolder General to contain an additional sub-directory level in order to get the scripts organized more lucidly:

+---Scripts
|   +---General
|   |   +---Plotting
|   |   |   +---script_one.py
|   |   |   +---script_two.py
|   |   +---Misc
|   |   |   +---script_three.py
|   |   |   +---script_four.py
....

When starting a Python script with e.g. the following lines, I get the VS Code pylint(import-error) for each of following imports.

# Package importing

import sys
sys.path.append(
    "/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/"
)

import General.Plotting.auxiliary_plotting_functions as aux_plot
import General.Plotting.plotting as plot

#%%
# TIME MEASUREMENT for the entire code/function
import General.Misc.timing

I don't know why pylint stopped recognizing the imports all of the sudden, just because I added an additional sub-directory level. I would like these senseless pylint import errors to disappear, since effectively the subsub-models are being imported correctly when executing the codes.

I even tried to modify the .pylintrc - file, which lies under /home/andylu/anaconda3/pkgs/pylint-2.3.1-py37_0/lib/python3.7/site-packages/pylint/test/regrtest_data/.pylintrc :

[MASTER]

optimize-ast=no

init-hook='import sys; sys.path.append("/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts")'

Adding the init-hook - line had no effect either.


回答1:


I found a great workaround for my problem with this answer. It points towards the message control part of the pylint-docs.

Practically, I just had to add the comment # pylint: disable=import-error behind my custom imports like so:

import General.Plotting.auxiliary_plotting_functions as aux_plot  # pylint: disable=import-error

This solves my issue perfectly, as I honestly didn't find an easy solution to this problem via configuring e.g. a .pylintrc file, let alone all my attempts with no avail involving PYTHONPATH and environment-files etc.

Put simply, my custom modules get imported correctly when executing the scripts in VS Code, but the only annoying detail was that pylint didn't get it and showed me useless import-errors. Now, pylint doesn't show this non-sense anymore, that's all I wanted :)

I'm sure there might be a more elegant solution, but so far the aforementioned workaround came in handy. If anyone comes up with a "better" solution, just post it here and I'll change the answer to yours.


PS for those using pylance as alternative linter in VS-Code:

A similar workaround (to the above-mentioned regarding pylint) I found here works fine (appending # type: ignore to the import-statement), which I mentioned in this question as well:

import General.Misc.general_tools as tools  # type: ignore

Most likely it's got something to do with the settings.json - file of VS-Code, since using VS-Code is the constant factor here.




回答2:


Two things. One, add __init__.py files to all of your sub-directories to make them proper packages.

Two, you shouldn't need to directly add the site-packages directory. If that's the Python environment you're using then you need to make sure to select it from within the extension.



来源:https://stackoverflow.com/questions/61204189/vs-code-pylintimport-error-unable-to-import-subsub-module-from-custom-direct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!