240

I am using the following setup

  • macOS v10.14 (Mojave)
  • Python 3.7.1
  • Visual Studio Code 1.30
  • Pylint 2.2.2
  • Django 2.1.4

I want to use linting to make my life a bit easier in Visual Studio Code. However, for every import I have states "unresolved import". Even on default Django imports (i.e. from django.db import models).

I presume it is because it is not seeing the virtual environment Python files.

Everything works just fine, but it's starting to get annoying.

The interpreter choices I have are all system versions of Python. It does not seem to see my virtual environment Python at all (it is not in the same directory as my workspace, so that part makes sense).

If I set up the python.PythonPath in the settings.json file, it just ignores it and does not list my virtual environment path as an option. I also tried setting it up in my global Python settings, but it also does not show up.

Is there a quick fix to get it working?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jAC
  • 3,155
  • 3
  • 18
  • 29
  • 5
    `pip install pylint-django`, then in vs code settings add this: ```"python.linting.pylintArgs": [ "--load-plugins=pylint_django", ],``` – Vaibhav Vishal Dec 27 '18 at 04:33
  • @VaibhavVishal unfortunately, i still get the same results (added it to my settings.json file) – jAC Dec 27 '18 at 04:48
  • 26
    Its actually a VScode problem that detects import package very late , after restarting it is fixed automatically. – Anupam Haldkar May 22 '20 at 11:18
  • 2
    @VaibhavVishal this is the only solution that worked for me in the entire thread. Thanks. – David Dahan Feb 02 '21 at 09:18
  • My pythonPath was already set correctly to the venv python. If this helps anyone, I actually had to install pylint in my venv: ```python -m pip install pylint```, and then update my pylintPath in VS Code to the venv pylint. – Tim Apr 13 '21 at 17:24
  • Returning to this question in 2023. VS Code has been updated, pythonPath is no longer used. You need to use "python.defaultInterpreterPath". If VS Code isn't detecting the interpreter. Set this inside your workspace settings JSON. – mohdajami May 15 '23 at 07:43

40 Answers40

336

The accepted answer won't fix the error when importing own modules.

Use the following setting in your workspace settings .vscode/settings.json:

"python.autoComplete.extraPaths": ["./path-to-your-code"],

Reference: Troubleshooting, Unresolved import warnings

UPDATE 2023

As mentioned by shim python-language-server is deprecated and new closed source LSP Pylance's setting is configured as follows:

"python.analysis.extraPaths": ["./path-to-your-code"]
Shinebayar G
  • 4,624
  • 4
  • 18
  • 29
  • 5
    I have a mixed workspace, all python code is in a sub folder. Adding a .vscode in the sub folder will not work, this is the only working solution. – daisy Nov 03 '19 at 03:16
  • 2
    best answer for vscode settings.json. (if using workspace.xml the accepted answer might be the way to go, but can't say for sure. i'm also using a mixed workspace) many thanks for solution. – None Dec 30 '19 at 05:16
  • 7
    This should be the answer. – Richard Li Mar 23 '20 at 23:46
  • 2
    This worked for me! The reference link explains perfectly. TL;DR; For user created local scripts in subdirectories, the above setting helps the python interpreter to recognize the subdirectory as its own workspace. Subsequently, recognizing scripts in the workspace and resolving imports when whole modules or individual methods are imported. – Avid Programmer May 03 '20 at 20:33
  • 11
    Hello everyone! I've found that ["./path-to-your-code"] can be ["./**"] in any case where a double star means any sub-folder under the root directory! This is a simpler solution. – alan23273850 Jun 08 '20 at 14:23
  • This worked for me on a mac as well, but I had to put the absolute path from root all the way up to the directory with the files being added in order to import any of the other python files in the directory. – James DeRagon Jun 09 '20 at 21:01
  • This worked for me! The relative path works on windows. But this is a sloppy workaround and shouldn't have to be done. I wish the authors could fix this issue – retrovius Jul 24 '20 at 13:42
  • 2
    Don't forget to reload your vs code window after adding this for the changes to apply. – Adrian Gonzalez Aug 05 '20 at 14:40
  • It works for me and I found that I can remove it after the unresolved import was gone. So, is it the IDE bug ? – WLiu Sep 26 '20 at 12:44
  • @WLiu If you remove, it may not work again if you relaunch VsCode again. It's not entirely bug, rather implementation issue of VSCode. – Shinebayar G Sep 26 '20 at 20:21
  • This helps when you have venv and source separated of course – JEX Oct 03 '20 at 13:44
  • Works for me in venv! – yyFred Nov 11 '20 at 01:29
  • 5
    At some point the name of the setting changed. It's now `"python.analysis.extraPaths"` – shmim Nov 30 '20 at 01:22
  • 2
    Another unique combination to get rid of the linting error (code still worked though): 1. Change Python language server in settings from Pylance to something else, and 2. Add "python.autoComplete.extraPaths": ["./path-to-your-code"] to settings.json – user3661992 Mar 17 '21 at 11:44
  • 3
    I had to use this: `"python.analysis.extraPaths": ["${workspaceFolder}/Source"]` – Get Off My Lawn Apr 14 '22 at 22:09
183

In your workspace settings, you can set your Python path like this:

{
    "python.defaultInterpreterPath": "/path/to/your/venv/bin/python",
}
bragboy
  • 34,892
  • 30
  • 114
  • 171
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • 49
    This sort of works. For the Python specific imports it resolves those now but not my own models (i.e. "from users.models import User" still says it cannot resolve it). Thoughts on that? – jAC Dec 27 '18 at 14:17
  • 11
    Not sure. Please try reloading the window of the vs code ( from shell, `code -r`) or just restart the vscode. – ruddra Dec 27 '18 at 14:37
  • Yeah tried all that, still doesn't recognize them for some reason. Not sure what is going on with it. – jAC Dec 27 '18 at 15:53
  • 13
    I am not sure, sometimes it may occur if the [workspace directory](https://code.visualstudio.com/docs/python/python-tutorial#_start-vs-code-in-a-project-workspace-folder) is not set properly. Please make sure `manage.py` is in root of `workspace`. Also please make sure the [pylint is configured properly](https://code.visualstudio.com/docs/python/linting#_pylint) – ruddra Dec 27 '18 at 16:41
  • 8
    you genius! It was because my workspace was not starting at the root level of the project. Once i adjusted that it started work. Thanks so much! – jAC Dec 28 '18 at 14:54
  • 3
    Thanks so much! Helped me get past an ages-old problem! –  Jun 28 '19 at 01:25
  • 1
    @jAC can you elaborate a little more? I can't seem to figure this out. – dillon.harless Aug 30 '19 at 19:41
  • This didn't work for me. If it helps anyone, adding these two lines to user settings did: `"python.analysis.disabled": [ "unresolved-import" ], "python.linting.pylintArgs": ["--load-plugin","pylint_protobuf"] ` – dillon.harless Aug 30 '19 at 19:58
  • The answer with "python.autoComplete.extraPaths" solved my problem with the GitHub link that gives the reason. – Richard Li Mar 23 '20 at 23:48
  • 1
    Reloading window worked for resolving the issue for my own models. – Jonathan May 06 '20 at 09:14
  • the answer defining "python.autoComplete.extraPaths" worked much better. – Jason Harrison Dec 02 '20 at 23:53
  • 1
    in case you have other other configs in your workspace, you can add them in the dict object, just above the last `}` . – user3821178 Dec 15 '20 at 02:18
154

Alternative way: use the command interface!

Cmd/Ctrl + Shift + PPython: Select Interpreter → choose the one with the packages you look for:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ted
  • 13,596
  • 9
  • 65
  • 107
  • 8
    in my case this did not work but hopefully it helps others with this issue. – jAC Jun 05 '19 at 22:34
  • yeah that's the spirit, it's not a complicated solution, it's what happened for me :) – ted Jun 05 '19 at 22:54
  • @Safder not for me. using virtualenv – dillon.harless Aug 30 '19 at 18:56
  • this worked for me. Because I have a directory structure where each python project I have a new `venv`, with this i point it to that specific Python in the `venv` directory for that project – cryanbhu Dec 15 '20 at 06:39
  • In Visual Studio 2019. Try to reopen VS after package installation. Worked for me. – Pedro77 Mar 16 '21 at 02:23
  • the python interpreter is in `workspace.code-workspace`. In windows 10, `python` installed with the app system, my `site-packages` is here 'C:\\Users\\User\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages', so I took the interpreter in `\User\AppData\Local\microsoft..` instead of `program files/windowsapps/python..`. Still no luck, maybe because I have some pip modules in `C:\\Program Files\\WindowsApps\\Python..` as well. – Timo Jul 02 '21 at 19:50
  • 1
    worked for me in 2022 - I"m using venv in my project so I set the interpreter path to `./bin/python` – Stetzon Feb 01 '22 at 00:15
  • 1
    best solution here – Franz Biberkopf Jan 04 '23 at 09:54
57

This issue has already been opened on GitHub:

Python unresolved import issue #3840

There are two very useful answers, by MagnuesBrzenk and SpenHouet.

The best solution for now is to create a .env file in your project root folder. Then add a PYTHONPATH to it like this:

PYTHONPATH=YOUR/MODULES/PATH

And in your settings.json add:

"python.envFile": ".env"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomasz Chudzik
  • 1,867
  • 1
  • 14
  • 21
  • 6
    Thanks, that worked perfectly! `"${workspaceFolder}/.env"` `PYTHONPATH=FolderName` – KowaiiNeko Mar 24 '19 at 02:58
  • 1
    There was a common issue with editable installs when using the Microsoft Python Language Server. However it looks like currently after the new fix described here: https://github.com/microsoft/python-language-server/issues/1137#issuecomment-500069622 the issue has been fixed and any import can be added with ExtraPaths. Look at this TroubleShooting for more help: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings – Tomasz Chudzik Jun 14 '19 at 22:23
  • Thanks @TomaszChudzik - Setting: "python.autoComplete.extraPaths": ["./src"] worked like a charm! – Robert Aug 08 '19 at 03:09
  • Another solution is to add your codebase modules in your virtualenv (using `add2virtualenv YOUR/MODULES/PATH` for example), and select this virtualenv as your python interpreter. – Antwan Oct 04 '19 at 15:45
  • Project root folder in vscode means where the workspace.code-workspace is and all the repos as subdirs? Module path is the path to `site-packages`? – Timo Jul 02 '21 at 19:57
  • Worked. None of the other answers helped much. – William R. Ebenezer Jan 28 '23 at 11:16
41

When I do > reload window that fixes it.

Reference: Python unresolved import issue #3840, dkavraal's comment

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Hesari
  • 1,821
  • 5
  • 25
  • 51
29

You need to select the interpreter associated with the virtual environment.

Enter image description here

Click here (at the bottom status bar):

Enter image description here

And just select the virtual environment you are working with. Done.

Sometimes, even with the interpreter selected, it won't work. Just repeat the process again and it should solve it.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
29

None of the solutions worked except this one. Replacing "Pylance" or "Microsoft" in the settings.json solved mine.

"python.languageServer": "Jedi"
hexr
  • 441
  • 5
  • 6
22

If you have this code in your settings.json file, delete it:

{
    "python.jediEnabled": false
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sps
  • 229
  • 2
  • 3
  • 44
    It's better to add a bit of explanation. – Til Mar 11 '19 at 12:24
  • 3
    Editing this line is a big change in your project. Line: "python.jediEnabled": false disables the old language server and enables the new Microsoft Python Language Server. Look here: https://github.com/Microsoft/vscode-python/issues/2177 I think it's much simpler to just add necessary dependencies to our envFile. It's described in another answer. With the new Microsoft Python Language Server intellisense works much better for me. – Tomasz Chudzik Apr 19 '19 at 10:18
  • For me its the other way around, enabling jedi in settings.json works for me. See more here, https://stackoverflow.com/a/57269144/2877493 – nairb Jul 30 '19 at 10:05
  • @Tiw especially since an other answer suggest the very opposite. – Neinstein Sep 14 '20 at 11:50
21

If you are more visual like myself, you can use the Visual Studio Code configurations in menu FilePreferencesSettings (Ctrl + ,). Go to ExtensionsPython.

In the section Analysis: Disabled, add the suppression of the following message: unresolved-import:

Visual Studio Code settings

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David
  • 321
  • 3
  • 4
  • 4
    The unresolved-import for relative imports is clearly incorrect, this gets rid of it. – Satya Mishra Jun 04 '20 at 16:32
  • 2
    This only fixes the warning, not the knock on issues. If I use the python path method then the import warning disappears, and also "drill in" ("goto definition") and "parameter hints" work. – davidfrancis Dec 21 '20 at 20:18
20

I was able to resolved this by enabling jedi in .vscode\settings.json

"python.jediEnabled": true

Reference from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-456017675

nairb
  • 464
  • 1
  • 6
  • 9
18

I wonder how many solutions this problem have (or have not), I tried most of the above, nothing worked, the only solution that worked is to set the python language server to Jedi, instead of Microsoft in the settings.json file:

"python.languageServer": "Jedi"
Georges D
  • 351
  • 3
  • 11
10

None of the previous answers worked for me. Adding both of the lines below to my settings.json file did, however.

"python.analysis.disabled": [
    "unresolved-import"
],
"python.linting.pylintArgs": ["--load-plugin","pylint_protobuf"]

The first line really just hides the linting error. Certainly not a permanent solution, but de-clutters the screen.

This answer gave me the second line: VS Code PyLint Error E0602 (undefined variable) with ProtoBuf compiled Python Structure

Maybe someone who understands Python more than me can explain that one more.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dillon.harless
  • 427
  • 4
  • 16
10

Okay, so 2 years down the line, I have ran into this annoying problem. All I can seen here are some really complicated workarounds. Here are easy to follow steps for anyone else who might just run into this later on:

  • at the bottom of VS Code where you see the Python version listed, just click there
  • Select Interpreter windows is going to appear
  • click on the first option that says "Select Interpreter Path" and navigate to the folder path which has your Virtual Environment

That's all you need to do and avoid tampering with those settings in VS Code which might get very complicated if not handled with caution.

CaptainBli
  • 4,121
  • 4
  • 39
  • 58
Surveyor Jr
  • 346
  • 2
  • 12
7

My solution

This solution is only for the current project.

  1. In the project root, create folder .vscode

  2. Then create the file .vscode/settings.json

  3. In the file setting.json, add the line (this is for Python 3)

    {
        "python.pythonPath": "/usr/local/bin/python3",
    }
    
  4. This is the example for Python 2

    {
        "python.pythonPath": "/usr/local/bin/python",
    }
    
  5. If you don't know where your Python installation is located, just run the command which python or which python3 on the terminal. It will print the Python location.

  6. This example works for dockerized Python - Django.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rolly
  • 3,205
  • 3
  • 26
  • 35
7

I was facing the same problem while importing the project-related(non standard) modules. Detailed explanation of the problem

Directory structure:

Project_dir:
    .vscode/settings.json
    dir_1
        > a
        > b
        > c
    dir_2
        > x
        > y
        > z

What we want:

Project_dir
    dir_3
        import a
        import y

Here "import a" and "import y" fails with following error:

Import "dir_1.a" could not be resolvedPylancereportMissingImports
Import "dir_2.y" could not be resolvedPylancereportMissingImports

What worked for me:

Appending the top directory which contains the modules to be imported.

In above example add the follwoing "Code to append" in ".vscode/settings.json"

Filename:

.vscode/settings.json

Code to append:

"python.analysis.extraPaths": [dir_1, dir_2]
TheLearner
  • 439
  • 1
  • 7
  • 10
  • 1
    This is what ended up working for me. I had my project with code in a `package_name` folder nested in an outer `package_name` folder. Adding just `package_name` to "extraPaths" (not the absolute path) did the trick. – Ruben Flam-Shepherd Jan 24 '21 at 18:23
  • 2
    Worked for me but I had to put the directory name in quotations. – yem May 10 '21 at 12:54
5

The solution from Shinebayar G worked, but this other one is a little bit more elegant:

Copied from Python unresolved import issue #3840:

Given the following example project structure:

  • workspaceRootFolder
    • .vscode
  • ... other folders
  • codeFolder

What I did to resolve this issue:

  1. Go into the workspace folder (here workspaceRootFolder) and create a .env file
  2. In this empty .env file, add the line PYTHONPATH=codeFolder (replace codeFolder with your folder name)
  3. Add "python.envFile": "${workspaceFolder}/.env" to the settings.json
  4. Restart Visual Studio Code
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lincoln
  • 880
  • 14
  • 25
5

To me the problem was related with the project that I was working on. It took me a while to figure it out, so I hope this helps:

Original folder structure:

    root/
    __init__.py  # Empty

        folder/
            __init__.py # Empty

            sub_folder_b/
                my_code.py
            sub_folder_c/
                another_code.py

In another_code.py:

from folder.sub_folder_b import my_code.py

This didn't trigger the intellisense in Visual Studio Code, but it did execute OK.

On the other hand, adding "root" on the import path, did make the intellisense work, but raised ModuleNotFoundError when executing:

from root.folder.sub_folder_b import my_code.py

The solution was to remove the _init_.py file inside the "folder" directory, leaving only the _init_.py located at /root.

NicoE
  • 4,373
  • 3
  • 18
  • 33
4

This works for me:

Open the command palette (Ctrl + Shift + P) and choose "Python: Select Interpreter".

Doing this, you set the Python interpreter in Visual Studio Code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

Changing Python:Language Server to 'Jedi' worked for me. It was 'Windows' initially.

3

None of the answers here solved this error for me. Code would run, but I could not jump directly to function definitions. It was only for certain local packages. For one thing, python.jediEnabled is no longer a valid option. I did two things, but I am not sure the first was necessary:

  1. Download Pylance extension, change python.languageServer to "Pylance"
  2. Add "python.analysis.extraPaths": [ "path_to/src_file" ]

Apparently the root and src will be checked for local packages, but others must be added here.

chuck_d
  • 121
  • 1
  • 3
3

I am using the following setup: (in Apr 2021)

  • macos big sur
  • vscode
  • Anaconda 3 (for environment)

And I faced this error during starting of the Django. So, I follow these steps and this error is resolved.

Steps are given in these screenshots:

  1. Open settings (workspace)

  2. Follow this screenshot to open Python Path Follow this for Step 2

  3. Now, click Edit in settings.json

  4. Make path like given in this screenshot /opt/anaconda3/bin/python enter image description here

5. Now, save this settings.json file. 6. Restart the vscode

Also, intellisense might not work for some time hold on wait for some time and then restart again then vscode reads file for new path.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Mayur Gupta
  • 303
  • 2
  • 14
2

That happens because Visual Studio Code considers your current folder as the main folder, instead of considering the actual main folder.

The quick way to fix is it provide the interpreter path to the main folder.

Press Command + Shift + P (or Ctrl + Shift + P on most other systems).

Type Python interpreter

Select the path where you installed Python in from the options available.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

For me, it worked, if I setup the paths for python, pylint and autopep8 to the local environment paths.

For your workspace add/change this:

"python.pythonPath": "...\\your_path\\.venv\\Scripts\\python.exe",
"python.linting.pylintPath": "...\\your_path\\.venv\\Scripts\\pylint.exe",
"python.formatting.autopep8Path": "...\\your_path\\.venv\\Scripts\\autopep8.exe",

Save and restart VS Code with workspace. Done!

fcdt
  • 2,371
  • 5
  • 14
  • 26
  • This worked for me as well, more specifically setting the "python.linting.pylintPath" is what fixed the issue. I never had this issue until I upgraded to Python 3.9. My guess is there is a default pylint path they are using. – areed1192 Dec 06 '20 at 03:05
1

I have a different solution: my Visual Studio Code instance had picked up the virtualenv stored in .venv, but it was using the wrong Python binary. It was using .venv/bin/python3.7; using the switcher in the blue status bar.

I changed it to use .venv/bin/python and all of my imports were resolved correctly.

I don't know what Visual Studio Code is doing behind the scenes when I do this, nor do I understand why this was causing my problem, but for me this was a slightly simpler solution than editing my workspace settings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dawngerpony
  • 3,288
  • 2
  • 34
  • 32
1

In case of a Pylint error, install the following

pipenv install pylint-django

Then create a file, .pylintrc, in the root folder and write the following

load-plugins=pylint-django
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

I have faced this problem in three ways. Although for each of them a solution is available in the answers to this question, I just thought to put it all together.

  1. First I got an "Unresolved Import" while importing some modules and I noticed that my installations were happening in global pip instead of the virtual environment.

    This issue was because of the Python interpreter. You need to select the interpreter in Visual Studio Code using Shift + Ctrl + P and then type Select Python Interpreter. Select your venv interpreter here.

  2. The second issue was: The above change did not resolve my issue completely. This time it was because of file settings.json. If you don't have the settings.json file in your project directory, create one and add the following line in that:

        {
            "python.pythonPath": "apis/bin/python"
        }
    

    This will basically tell Visual Studio Code to use the Python interpreter that is in your venv.

  3. The third issue was while importing a custom Python module or file in another program. For this you need to understand the folder structure. As Python in venv is inside bin, you'll need to specify the folder of your module (most of the time the application folder). In my case it was app,

        from app.models import setup_db
    

    Verbally, import setup_db from models.py resides in the app folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

If you are using pipenv then you need to specify the path to your virtual environment.in settings.json file. For example :

{
    "python.pythonPath": 
           "/Users/username/.local/share/virtualenvs/Your-Virual-Env/bin/python"
}

This can help.

Devesh
  • 929
  • 12
  • 10
1

If someone happens to be as moronic as me, the following worked.

Old folder structure:

awesome_code.py
__init__.py
    src/
        __init__.py
        stuff1.py
        stuff2.py

New structure:

awesome_code.py
    src/
        __init__.py
        stuff1.py
        stuff2.py
Astrid
  • 1,846
  • 4
  • 26
  • 48
1

How to avoid warning

Please note that this is just skipping the warning not resolving it. First of all open visual studio code settings in json and add following arguments after "[python]":{}

"python.linting.pylintArgs": ["--reports", "12", "--disable", "I0011"],
"python.linting.flake8Args": ["--ignore=E24,W504", "--verbose"]
"python.linting.pydocstyleArgs": ["--ignore=D400", "--ignore=D4"]

This has helped me to avoid pylint warnings in VSCode.

VS Code settings

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Otabek Butcher
  • 545
  • 8
  • 19
0

I have resolved import error by Ctrl + Shift + P. Type "Preferences settings" and select the option Preferences Open Settings (JSON)

And add the line "python.pythonPath": "/usr/bin/"

So the JSON content should look like:

{
    "python.pythonPath": "/usr/bin/"
}

Keep other configuration lines if they are present. This should import all modules that you have installed using PIP for autocomplete.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashish
  • 1,309
  • 1
  • 11
  • 17
0

My solution was to open Visual Studio Code in a previous directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

In my case I already had a Conda environment activated, but I still wanted local Python modules to be available for autocomplete, peeking definition, etc.

I tried many solutions such as adding a list of Python paths etc., but what finally solved it for me was to create a symbolic link from Conda's lib/python{your version}/site-packages to my local module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kerem T
  • 260
  • 4
  • 6
0

I have one library which errs out when trying to include it using the Jedi language service and works fine without it (i.e. the C# one).

The library is jsonslicer and it does depend on an external C library I installed into /usr/local/lib. Could that have something to do with it?

I installed the Jedi service and the library in my Conda environment and used that environment within Visual Studio. It works fine at runtime and in my terminal, but not when checking for problems in my source files and it shows up as an error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
richtera
  • 91
  • 1
  • 3
0

First make sure that you've installed the plugin, but it's likely that the workspace directory isn't properly set. Just check Pylint and edit the underlying settings.json file.

{
    "python.pythonPath": "/usr/local/bin/python3",
    "git.ignoreLimitWarning": true
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What do you mean by *"check Pylint"*? A check box? That it exists/is installed? Something else? Respond by [editing your answer](https://stackoverflow.com/posts/62387693/edit), not here in comments (and ***without*** "Edit:", "Update:", or similiar). – Peter Mortensen Jul 12 '20 at 14:36
0

Install code-runner and add the code below in the settings.json folder:

    "code-runner.executorMap": {
        "python": "python3 -u",
    }

"python": "(the Python executable with modules or its path) -u",

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hong Z
  • 192
  • 2
  • 5
  • What is "code-runner"? Can you add a reference to it (by [editing your answer](https://stackoverflow.com/posts/62402248/edit), not here in comments)? – Peter Mortensen Jul 12 '20 at 14:38
0

I have the same problem with python 3.8.5 using venv,vscode 1.48.2 I found my solution. In (env folder)/lib/site-packages does not contains the packages. I use this setting (.vscode/settings.json)

   {
        "python.autoComplete.extraPaths": [
            "./**",
        ],
        "python.pythonPath": "env\\Scripts\\python.exe",
        "python.languageServer": "Microsoft"
   }
0

I had the issue that the import of modules that I created were not found. I felt like I tried a number of the methods to ensure the python interpreter selection was correct, but to no avail. I found an answer that worked for me by editing the settings.json Python>Linting>Pylint Args and adding the init-hook...

--init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

This solution was found in PyLint "Unable to import" error - how to set PYTHONPATH?. I did not create and edit pylintrc, but added the above using the VS Code GUI.

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
joltman
  • 41
  • 2
0

I seem to of had this issue because django was installed on my base virtual environment, and not the one I was actually using for the project. This basically caused it to work, but show errors and not autocomplete correctly.

To fix I simply

  1. Opened Anaconda Navigator
  2. Click Environments in left hand menu
  3. Select the virtual environment you are using for the project
  4. On the virtual environment, click the green triangle (once it loads) it and select 'Open Terminal'
  5. Run 'pip install django'

Once this is done you can go back to VS Code and toggle the python environment to the base, then back to the one you want in the bottom left of VS Code.

Errors should disappear and autocomplete should work.

Levitybot
  • 481
  • 5
  • 8
0

if importing a moduel in a subdirectory, add the following setting in .vscode/settings.json:

"python.analysis.extraPaths": [
        "./directory_name"
    ]
Chow Fred
  • 11
  • 1
-1

I solved the problem with command-line python. I installed modules with vs code terminal in project path, but when import the module on windows command line python, that throws an error as this module not defined so I install these modules from the command line and my problem solved.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Parsa
  • 1