VS Code import resolution error with Pylance (ModuleNotFoundError) - python

Having an import resolution error with pylance not recognizing packages. (Windows 10)
Venv activated on the local directory
Confirmed pip -list that the package is correctly installed
Ran VS code as administrator
Checked the package is installed in the /Lib/site-packages folder for my python installation
Receiving the error:
ModuleNotFoundError: No module named 'abc123'
Inside the file, hovering over the broken import says
"packageFoo" is not accessible
Import "packageFoo" could not be resolved Pylance

I found many similar questions, but not this specific answer.
In vscode, locate the file dropdown on the top toolbar.
Select preferences > Settings (Ctrl +)
In the search bar, search for pylance
Scroll down to Python > Analysis: Extra Paths
Click 'add item' button
Next step you need to identify the installation path for python in windows. In that path there is a 'Lib' folder. Under lib folder there is a 'site-packages' folder. This is the folder for all your pip installed packages.
For me it looked like this:
C:\Users\user_name_here\AppData\Local\Programs\Python\Python310\Lib\site-packages
Copy this path and add it as the item in the pylance vscode settings.
Additionally: You can open the 'Python > Analysis: Indexing' settings below to edit a JSON file. Here you copy and paste the same path, with extra '' - backspace escape characters. Noting there is a comma after the first entry, if it exists.

If you have correctly installed Pylance when you open a .py file, this extension would be activate. Now check settings.json file and add the following sentence:
"python.languageServer": "Default" or "Pylance".
 It is probably that your problem is caused because Pylance is not pointed to the path where is your library installed or another package and the error is originated (ModuleNotFoundError: No module named 'abc123'), then you may need to verify in your settings.json this configuration:
python.analysis.autoSearchPaths : true
python.analysis.stubPath:./typings
python.analysis.extraPaths:["./folder"] (if your project use a specific folder)
python.analysis.autoImportCompletions:true

Make sure there is a .py extension on the module.

Related

How do I get into the environment VS Code is using for pylance?

I'm using pylance to check my Python code. It tells me
Import "astor" could not be resolved
When I switch to the terminal within VS Code:
I'm pretty certain that the issue is that it uses another environment. I'm using pyenv by default and I would like if vscode would use the same environment. But at the very least I need to be able to access the environment it is using to install packages.
Interestingly, the status bar seems to show something else, because in that environment I have astor installed:
I found this link that informs us that we should add an extra path.
These extra roots must be specified to the language server. The
easiest way to do this (with the VS Code Python extension) is to
create a workspace configuration which sets
python.analysis.extraPaths. For example, if a project uses a sources
directory, then create a file .vscode / settings.json in the workspace
with the contents:
{
"python.analysis.extraPaths": ["./sources"]
}
https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
Another easy way to solve this on VSCODE:
ctrl + ','
type "extrapaths"
Down you should have something like "add element" (I have VSCode on Spanish so in my case I have "Agregar elemento")
type './sources/'
Also if you have problems importing local files, you can do the same thing and add your working directory path to solve the problem :) just add 'C: your working directory goes here' in addition to './sources' in the same way ;)
I found one thing resolved my issue same as yours
Go to your working env(mine is pipenv shell), then pip show 'yourmodule' to check your module is installed or not
If its installed copy Location: path
Go to settings ctrl +','
Type extrapaths
Add Item paste the path string you copied and ok.
We have added actual installed path as additional import search resolution path, so this path will be scanned for imports
Now your module should be resolved. This worked for me.

why python not recognizing my project packages in import statements

I'm trying to import my packages from the same project I am using. But I'm getting ImportError: No module named main_package.sub_package
I'm using python2.7
My project structure is
my_project/main_package/sub_package/file_a
my_project/main_package/sub_package/file_b
and the following row:
from main_package.sub_package import file_b
isn't working when we add it in file_a. and isn't working from other packages in main_package.
This does work on other computers.
I tried to add all of their elements to my sys.path.
I also have anaconda installed in my computer, and I tried to use the following interpreters: ~/anaconda/bin/python2.7, ~/anaconda/bin/python, /usr/bin/python2.7.
In the project there should be src directory. In your case my_project/src/main_package/.
Inside IDE (I use PyCharm) right click on it and select "Mark Directory As" -> "Sources Root".

Pycharm unresolved reference in virtualenv

When I started a new project in Pycharm using virtual environment, I got an unresolved reference warning message.
But I have installed django to my virtual environment and this code is working correctly.
How can I fix this? I'm using PyCharm 4.5.2 Pro
Open your project in pycharm and follow the step:
click on file(on top) > settings > project interpreter > add local > select virtualenv
OR
Make sure your project is a root directory, make it is the root directory.
I eventually stumbled across a method that worked for me, but I don't know the underlying problem.
Initially I created a virtualenv with PyCharm in the interpreter options. Annoyed that I was having this problem, I deleted it and just created a virtualenv with the virtualenv command, but the problem remained.
Then I went back to the interpreter selector and picked "add local". I navigated to the python binary in the /bin folder of the virtualenv I created and selected it. After this, the unresolved references were fixed.
I think your problem is like mine. I tested import matplotlib in the python shell, and it works, but in Pycharm it shows an error. This is because Pycharm doesn't know the path of site-packages.
To fix this, you can do one of the following:
Add the path of site-packages (for me C:\Program Files\Python 3.5\lib\site-packages) to PATH.
Go to File -> Settings -> Project Interpreter, then choose Python real path instead of venu path.
In your code add the following:
.
import sys
sys.path.append(r"C:\Program Files\Python 3.5\lib\site-packages")
import matplotlib.pyplot as plt
I think it will help you :)
File > Settings > Project > Project Structure
Double Click on the folder link under Content Root, and do OK.
Check your project root directory. Maybe problem in it.
And did you create virtualenv manually or through PyCharm?

ImportError: No module named 'bottle' in PyCharm

I installed bottle on Python 3.4 with pip install. In the terminal, when I do:
$ python3.4
>>>import bottle # shows no import error
>>>
but when I do it in PyCharm, it says:
import bottle ImportError: No module named 'bottle'
in your PyCharm project:
press Ctrl+Alt+s to open the settings
on the left column, select Project Interpreter
on the top right there is a list of python binaries found on your system, pick the right one
eventually click the + button to install additional python modules
validate
In some cases no "No module ..." can appear even on local files. In such cases you just need to mark appropriate directories as "source directories":
The settings are changed for PyCharm 5+.
Go to File > Default Settings
In left sidebar, click Default Project > Project Interpreter
At bottom of window, click + to install or - to uninstall.
If we click +, a new window opens where we can decrease the results by entering the package name/keyword.
Install the package.
Go to File > Invalidate caches/restart and click Invalidate and Restart to apply changes and restart PyCharm.
Settings:
Install package:
I am using Ubuntu 16.04. For me, it was the incorrect interpreter, which was by default using the virtual interpreter from the project.
So, make sure you select the correct one, as the pip install will install the package to the system Python interpreter.
PyCharm 2019.3, my solution is below:
For me, none of the above worked, and curiously even within one file some imports worked, some didn't:
from folder1.folder2.folder3.my_python_file import this_function # worked
from folder1.folder2.folder3.my_python_file import that_function # didn't work
Follow the above advice, but if it doesn't fix it additionally, (in PyCharm) click File >> Repair IDE and confirm all the 6 steps one after another.
I had virtual env site package problem and this solved it:
In the case where you are able to import the module when using the CLI interpreter but not in PyCharm, make sure your project interpreter in PyCharm is set to an actual interpreter (eg. /usr/bin/python2.7) and not venv (~/PycharmProject/venv/...)
I had the same problem, I tried all fixes like installing from the project interpreter and installing from python console, nothing worked. What worked was just going to the project folder from the terminal and installing it from there.

Unresolved import: models

I'm doing my VERY first project using python/django/eclipse/pydev following this guide
http://docs.djangoproject.com/en/dev/intro/tutorial01/
My only addition is the use of Eclipse/pydev.
I'm getting many errors related to "Unresolved imports". I can remove the errors using "remove error markers" and my site runs perfect (I can browse it) but I want to get rid definitively of this problem since errors pop up again after I removed them.
Any ideas?
EDIT
Using Ubuntu 9.1
Check your pythonpath. You need to include the parent directory of django, usually Lib/site-packages.
I'm on Mac OS X, but all I had to do was to add:
/Library/Python/2.6/site-packages
to my System PYTHONPATH (Found in: Preferences > Pydev > Interpreter - Python)
The equivalent for Ubuntu 10.04 would be:
/usr/lib/pymodules/python2.6
I'm guessing that the equivalent for Ubuntu 9.x could be:
/usr/lib/python2.6/site-packages
If it isn't and you're tired of looking, just upgrade to Ubuntu 10.04 and you should be fine.
I was able to get rid of the import errors executing this cmd
sudo ln -s /usr/lib/pymodules/ /usr/lib/python2.6/pymodules
I was lucky, testing different things I could resolve it but I'm not sure why I have to do this and how I could avoid doing it.
"Unresolved imports" occur when Eclipse/Pydev does not know what you want to import. Check your Pydev settings in the Eclipse preferences > Interpreter Python. Your site-packages folder and things you want to import should be there in the Pythonpath.
Also see http://pydev.org/manual_101_interpreter.html
Even if Lib/site-packages is added to the PYTHONPATH, this problem may happen when using modules from egg packages installed using easy_install. The problem with those, and that might actually depend on the easy_install version, is that by default they don't get installed directly inside Lib/site-packages but rather under a folder containing the full package name and having the .egg suffix. For example: Lib/site-packages/django_celery-3.0.23-py2.7.egg
Each module coming from packages installed as above needs an individual entry in PYTHONPATH. If the packages are installed after PyDev has been installed on the system, the system PYTHONPATH needs to be updated in PyDev. That is done automatically by going to Window -> Preferences -> PyDev -> Select your intepreter -> in the python interpreters panel remove and re-add your current python interpreter (usually, this should be the python executable from the system). Then a PyDev restart (File -> Restart) should solve the "Unresolved import" errors.
If you are facing problem of unused imports then I must say use Eclipse as a IDE as it is providing functionality to remove unused imports by pressing keys ctr+shift+O. Or In Eclipse there is plugin also available which is doing the same thing automatically when you save your code.You can get that plugin from eclipse plugin site easily and free of cost.
In my case of the modules were dependent on setuptools-14.3.1 , which was causing all these problems. After installation of setuptools-14.3.1 rest of the modules automatically resolved.
I was having a single import error when working in PyDEV in eclipse.
I was importing it like
from xyz_module import abc
So I clicked "Ctl + 1" and "select Unresolved import error" in eclipse and it created a class
file in a python file.
It turned out that I had created an application called "xyz_module" and a "xyz_module.py" file (with the same name that is) and this was causing an import error. I changed the python file to a new name and this resolved the error.
I had the same error and none of the answers worked for me as there was no PYTHONPATH options under Window -> Preferences -> PyDev -> Python.
Instead I added PYTHONPATH setting in the menu:
Project -> PyDev-PYTHONPATH -> External Libraries -> Add Source folder
I added the path of site-packages like
/home/Documents/hcx/venv/lib/python3.5/site-packages
Now PyDev stopped complaining.

Categories

Resources