我在几个子目录的树中组织了我自己编写的 Python 脚本,从已经包含在"python.autoComplete.extraPaths"
settings-json 中的父目录“Scripts”开始:
"python.autoComplete.extraPaths": ["/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts",
"/home/andylu/anaconda3/lib/python3.7/site-packages"]
除此之外,我还包含了一个 Python 环境文件:
"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 个单一目录级别上,如下所示:
+---Scripts
| +---General
| | +---script_one.py
| | +---script_two.py
当我在任何 python 脚本中导入时,例如script_one.py
,我启动了脚本
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)
.
现在,情况不同了。脚本变得如此之多,以至于我将子文件夹拆分General
为包含一个额外的子目录级别,以使脚本更清晰地组织:
+---Scripts
| +---General
| | +---Plotting
| | | +---script_one.py
| | | +---script_two.py
| | +---Misc
| | | +---script_three.py
| | | +---script_four.py
....
当使用例如以下行启动 Python 脚本时,我得到VS Code pylint(import-error)
以下每个导入的。
# 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
:
[MASTER]
optimize-ast=no
init-hook='import sys; sys.path.append("/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts")'
添加init-hook
- 行也没有效果。