简体   繁体   中英

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 eg 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 eg 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.

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.

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 eg 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.

Another answer (inspired by @andreas-l):

In the.pylintrc of my afflicted project (how and when the problem started, I don't know0), I found the following 'Messages Control' section and added "import-error" to it:

[MESSAGES CONTROL]

  disable=
    missing-docstring
    import-error

Restarted VSCode and had no more of the erroneous error messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM