VS Code pylint(导入错误)“无法导入"自定义目录中的子子模块 [英] VS Code pylint(import-error) "Unable to import" subsub-module from custom directory
问题描述
我在几个子目录树中组织了自己的Python脚本,从已经包含在settings-json中的"python.autoComplete.extraPaths"
中的父目录"Scripts"开始:
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"]
除此之外,我还包括一个Python环境文件:
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"
其中包含一行
export PYTHONPATH=/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts:/home/andylu/anaconda3/lib/python3.7/site-packages
所有这些都很好地完成了,之前,我的所有脚本仅在 1个单一目录级别上分发,就像这样:
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
当我在任何python脚本中导入例如 script_one.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
并且pylint正确地正确识别了导入的脚本 上述 VS Code pylint(import-error)
.
and pylint recognized this imported script correctly without throwing the aforementioned VS Code pylint(import-error)
.
现在,情况有所不同.脚本变得如此之多,以至于我将子文件夹 General
拆分为包含另一个子目录级别,以便更清晰地组织脚本:
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
....
使用以下命令启动Python脚本时在以下几行中,我为以下每个导入获取 VS Code pylint(导入错误)
.
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
我不知道为什么pylint突然突然停止识别导入,只是因为我添加了一个附加的子目录级别.我希望这些毫无意义的pylint导入错误消失,因为有效地在执行代码时正确地导入了子子模型.
我什至试图修改 .pylintrc
-文件,该文件位于/home/andylu/anaconda3/pkgs/pylint-2.3.1-py37_0/lib/python3.7/site-packages/pylint/test/regrtest_data/.pylintrc
:
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")'
添加 init-hook
-行也无效.
推荐答案
I found a great workaround for my problem with this answer. It points towards the message control part of the pylint-docs.
实际上,我只需要在自定义导入后面添加注释#pylint:disable = import-error
,
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
这完美地解决了我的问题,因为老实说,我没有通过配置例如一个 .pylintrc
文件,更不用说涉及 PYTHONPATH
和环境文件等的所有尝试了,但无济于事.
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.
简单地说,在VS Code中执行脚本时,我的自定义模块可以正确导入,但是唯一令人讨厌的细节是pylint没有得到它,并向我展示了无用的导入错误.现在,pylint不再显示这种废话了,这就是我想要的:)
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 (在使用 pylance 作为<<代码> VS代码:
一种类似的解决方法(与上述有关 pylint
的问题类似),我发现此问题中:>
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
很有可能与 settings.json
- VS-Code
的文件有关,因为使用 VS-Code
是恒定因子.
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.
这篇关于VS Code pylint(导入错误)“无法导入"自定义目录中的子子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!