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

3

11

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.

Cagey answered 14/4, 2020 at 9:7 Comment(0)
C
7

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.

Cagey answered 23/4, 2020 at 9:31 Comment(0)
L
7

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.

Lovejoy answered 15/4, 2020 at 22:59 Comment(6)
I'd had already put an empty __init__.py - file in all sub- and subsub-directories, even in the main directory "Scripts". So that cannot be it. As for your second point, you refer to removing the anaconda3/../site-packages - path from the "python.autoComplete.extraPaths" since these site-packages are already included in my "python.envFile"?Cagey
I'm suggesting the removal of site-packages because that is inherently a part of sys.path of any Python interpreter and Pylint should know that (basically I am not aware of others needing to do that to get Pylint to work).Lovejoy
Alright, thanks for the information. I've removed the site-packages from python.autoComplete.extraPaths and the codes indeed still work. On the other hand, the main issue hasn't been resolved yet as the "Unable to import ..." - messages didn't vanish by putting empty files called __init__.py into every main and sub folder (which I had already done before asking this question here). If you know anything else what I could do to prevent pylint from showing these useless import errors, thanks in advance.Cagey
You can turn off the specific checker in Pylint. You can do it a bunch of ways (e.g. .pylintrc file, per-line, as something you pass into Pylint). The docs on Pylint in the extensoin tell you how to pass CLI arguments. Passed that you will need to read the Pylint docs on how to disable checks.Lovejoy
I found a very easy workaround which supresses these useless import-errors, see my answer. I started to read the Pylint docs and after a while I wondered if there is no easier more direct way to simply make Pylint "shut up" concerning a custom module import, and indeed I found something. A more sophisticated approach might exist, but would cost me too much time to figure out at the moment.Cagey
please create a ticket on githubPinch
C
7

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.

Cagey answered 23/4, 2020 at 9:31 Comment(0)
M
2

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.

Melodeemelodeon answered 15/10, 2021 at 17:47 Comment(2)
That's also a viable solution as a centralized approach from the "settings"-file. The only dangerous thing could be that one forgets about the fact that all sorts of import errors are silenced.Cagey
There are other checks in VScode that surface import errors. For example, vscode was unable to do an import, and had me configure extraPaths as a QuickFix. Even after resolving that, then there was the separate pylint error about the import.Defiant

© 2022 - 2024 — McMap. All rights reserved.