Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 473
Andreas L. Avatar asked Apr 14 '20 09:04

Andreas L.


1 Answers

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.

like image 97
Brett Cannon Avatar answered Sep 20 '22 14:09

Brett Cannon