Reputation: 3435
I am using the following setup
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?
Upvotes: 260
Views: 472769
Reputation: 500
Switching from restricted mode to trusted mode resolves the import issue. See this answer.
Upvotes: 0
Reputation: 5170
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
As mentioned by shmim python-language-server is deprecated and new closed source LSP Pylance's setting is configured as follows:
"python.analysis.extraPaths": ["./path-to-your-code"]
Upvotes: 361
Reputation: 583
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.
Upvotes: 1
Reputation: 408
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:
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.
Upvotes: 11
Reputation: 51948
In your workspace settings, you can set your Python path like this:
{
"python.defaultInterpreterPath": "/path/to/your/venv/bin/python",
}
Upvotes: 192
Reputation: 21
if importing a moduel in a subdirectory, add the following setting in .vscode/settings.json:
"python.analysis.extraPaths": [
"./directory_name"
]
Upvotes: 1
Reputation: 657
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
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.
Upvotes: 0
Reputation: 41
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.
Upvotes: 0
Reputation: 519
I am using the following setup: (in Apr 2021)
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:
Open settings (workspace)
Now, click Edit in settings.json
Make path like given in this screenshot /opt/anaconda3/bin/python
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.
Upvotes: 3
Reputation: 131
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:
python.languageServer
to "Pylance""python.analysis.extraPaths": [ "path_to/src_file" ]
Apparently the root and src
will be checked for local packages, but others must be added here.
Upvotes: 3
Reputation: 459
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]
Upvotes: 7
Reputation: 21
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!
Upvotes: 2
Reputation: 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.
Upvotes: -1
Reputation: 1936
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
Upvotes: 1
Reputation: 61
Changing Python:Language Server to 'Jedi' worked for me. It was 'Windows' initially.
Upvotes: 3
Reputation: 11
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"
}
Upvotes: 1
Reputation: 4853
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
.
Upvotes: 5
Reputation: 451
None of the solutions worked except this one. Replacing "Pylance" or "Microsoft" in the settings.json solved mine.
"python.languageServer": "Jedi"
Upvotes: 29
Reputation: 959
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.
Upvotes: 1
Reputation: 361
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"
Upvotes: 19
Reputation: 3375
This solution is only for the current project.
In the project root, create folder .vscode
Then create the file .vscode/settings.json
In the file setting.json
, add the line (this is for Python 3)
{
"python.pythonPath": "/usr/local/bin/python3",
}
This is the example for Python 2
{
"python.pythonPath": "/usr/local/bin/python",
}
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.
This example works for dockerized Python - Django.
Upvotes: 7
Reputation: 39
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.
Upvotes: 2
Reputation: 11
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.
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.
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.
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.
Upvotes: 1
Reputation: 314
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",
Upvotes: 0
Reputation: 1
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
}
Upvotes: 0
Reputation: 91
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.
Upvotes: 0
Reputation: 15094
You need to select the interpreter associated with the virtual environment.
Click here (at the bottom status bar):
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.
Upvotes: 33
Reputation: 319
If you are more visual like myself, you can use the Visual Studio Code configurations in menu File → Preferences → Settings (Ctrl + ,). Go to Extensions → Python.
In the section Analysis: Disabled, add the suppression of the following message: unresolved-import
:
Upvotes: 20
Reputation: 258
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.
Upvotes: 0
Reputation: 1
My solution was to open Visual Studio Code in a previous directory.
Upvotes: -1