the_pied_shadow
the_pied_shadow

Reputation: 396

How do I fix pylint(import-error) in Visual Studio Code when I try to import custom libraries?

I am working on Python code in python3 that imports several custom libraries. These libraries are located in the file structure. I have no issues with importing these libraries when the code is run from the command window, however pylint is giving me an import-error on all of the custom library imports when I open the files in Visual Studio Code.

I've done a lot of frustrating research into this issue and see that a lot of people have had similar problems. However I couldn't get any of the listed solutions to work but many I didn't understand enough to even try. I have attempted to uninstall and reinstall VScode, pylint, and the python extension itself. I've tried a few things messing with launch.json and settings.json but I've never done anything with json files before and nothing I did seemed to make any difference. I have only one python environment installed. I have tried opening the folder with "File->Open Folder..." instead of using the context window method. The code uses sys.path.append so that the correct directory can be found that has the library and as I've said, this works fine when run from the command window.

Another solution I saw was adding a line

init-hook='import sys; sys.path.append("/path/to/root")'

to a file ~/.pylintrc. I think I've done this and again it didn't solve my issue. However there was little explanation on what this file is, where to find it, or exactly what path it needs. I simply searched for '.pylintrc' in my file explorer and found a file named just that which is where I put the line. The "path to root" I got by navigating to the folder which held all the libraries in sub-folders, then copy/pasted the file path.

Below is an example of how the library is being imported. This works, just not as far as pylint or VScode is concerned.

#Custom libraries
sys.path.append(os.path.join( '../..', 'common', 'appdata'))
import appdata

"Unable to import 'appdata' pylint(import-error)" shows as an error on 'import'

if I try to debug I get the message

"Exception has occurred: ImportError No module named 'appdata'"

I appreciate any help that can be given. Thank you.

Upvotes: 8

Views: 14202

Answers (3)

camposer
camposer

Reputation: 5622

Actually the best way of doing this, as @Brett suggests above, is using a .pylintrc file.

In my case, I added the .pylintrc file to the root of my project with a hook configured as it's commented here: https://stackoverflow.com/a/3065082

Upvotes: 0

TheTheorist2359
TheTheorist2359

Reputation: 23

first some questions (since I can't comment): 1. Are you using multiple versions of python3 (like one time I installed a package through python3.8 but I was using python3.9 so when I tried to import it I got a modulenotfounderror but later I solved it) if so then how many versions do you have installed 2. What is the system path for the libraries(if you don't want to share the path, that's fine you can share an example path)? 3. Do you have a __init__.py file inside the folder(s)in which the custom libraries are in(example: path for module.py: C:users\random\documents\modulecontainer\module.py the __init__.py should be in modulecontainer if module.py is your custom module)?

There are 2 different solutions for this problem depending on what your problem is(the 2 solutions are linked to the questions mentioned above):

Solution 1: Make sure that the module is in a folder that contains an empty file named __init__.py if you don't have it then create __init__.py and place it inside the folder that contains the module(s) using an IDE, in your case VScode. This makes sure that Python searches the files inside the folder to find a file that matches the name of the file that you want to import.

Solution 2: if you have installed a module that isn't in the standard library through a different version of python which is not the version of python you are currently using (example, installing randommodule through python3.8 but trying to import it through python3.9) then you can copy or move the package that is in the other version of python (example, moving randommodule from site-packages inside python38 to site-packages inside python39)

Hope, this helped! If you have anything else you want to add to this question, you can comment or edit the question.

Upvotes: 1

Brett Cannon
Brett Cannon

Reputation: 16100

A .pylintrc file is a configuration file for Pylint. Pylint documents how it finds a pylintrc file.

The problem is that Pylint doesn't execute your code, so your custom manipulation of sys.path isn't known to Pylint. So what you need to do is figure out how to tell Pylint to consider that folder you have in your code as part of sys.path. One way is specifying PYTHONPATH, another is specifying it in a .pylintrc file.

P.S.: sys.path.append(os.path.join( '../..', 'common', 'appdata')) should be sys.path.append(os.path.join( '..', '..', 'common', 'appdata')).

Upvotes: 2

Related Questions