Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource) - python

whenever I try to import matplotlib or matplotlib.pyplot in VS Code I get the error in the title:
Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource)
or
Import "matplotlib.pyplot" could not be resolved from source Pylance(reportMissingModuleSource)
The hyperlink of the reportMissingModuleSource sends me to https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules, where it says:
"Diagnostics for imports that have no corresponding source file. This happens when a type stub is found, but the module source file was not found, indicating that the code may fail at runtime when using this execution environment. Type checking will be done using the type stub."
However, from the explanation I don't understand exactly what's wrong and what I should do to fix this, can someone help me with this?

I can reproduce your question when I select a python interpreter where doesn't exist matplotlib:
So, the solution is opening an integrated Terminal then run pip install matplotlib. After it's installed successfully, please reload window, then the warning should go away.

Just changes the interpreter to 2.7.x in left-bottom corner enter image description here

I experienced a similar issue even after installing vs_BuildTools.
Matplotlib could not be resolved from source parlance
What to do:
open the Command Palette (Ctrl+Shift+P)
Type: Python: Select interpreter Here is the interpreter that worked for me!
Navigate to your project.
Select the latest interpreter or check what interpreter

I had the same issue. Sometimes it's back to multiple versions of python on your device. You just need to change the path. Make sure the correct Python interpreter is selected in your IDE.
In vscode Press CTRL + Shift + P or (⌘ + Shift + P on macOS) to open the command palette. Then type Python select interpreter in the search field and choose the right version.

I have the same issue - I did two things and its working now
Check if there is some earlier versions of python installed on your machine - if yes then remove and reinstall the latest one
Second install the Microsoft C++ Build Tools
https://visualstudio.microsoft.com/visual-cpp-build-tools/
Restart the app and run again.

I was getting the same problem and realised the package install path for my active virtual env wasn't listed in sys.path
after appending this location to sys.path my .ipynb was able to import matplotlib

Related

ModuleNotFoundError: No module named 'seaborn' in VSCode

I've tried everything, pip to conda, all types of installations possible.
And all the time I got that the required packages are already satisfied/installed.
But when I try to import it in VScode, it doesn't work. There was a similar question, who had the same problem and I did what people suggested there but it didn't help either. Can someone help out here?
I'm using python 3.8.8 version
thanks in advance for the help :)
enter image description here
Run pip show seaborn in Jupyter to check if it exists in current environment.
If it does, you should see the following screenshot, seanborn stores in environment\lib\site-packages:
If not, please selecting interpreter and opening an integrated Terminal, then install seaborn in it. You may also use pip show seaborn to check its installation location.
For your reference, view select and activate environment.
You may need to try changing your Python (3) interpreter, to do this:
Click the interpreter button in the bottom left (bottom blue ribbon): https://i.stack.imgur.com/4H2AL.png
Try running with the different Python 3 interpreter paths until you find one which works - https://i.stack.imgur.com/wY1Ld.png

Problems with importing external module

I tried to download an external module to test how it works, by simply downloading 'Simplejson' using pip in cmd(Window). I watched my teacher did it, and followed it the exact way but i ran into a problem.
Not sure what i did, because my teacher just told me to follow him, i think we created a virtual environment on python so that we can do more than one projects with various versions of python. Anyway, most of you will know that after creating a virtual environment in my folder that i work for coding, a 3 new folders will appear and 'Simplejson' will be inside Lib\site-packages. Even if i checked that Simplejson is still inside the file, some how i can not import it. Are there any way that i can solve it?
enter image description here
Please refer to the following to check the process of installing and using the module "Simplejson" in VS Code:
Please check whether the python currently used by the VS Code terminal is the same as the one displayed in the lower left corner of the VS Code: (python --version or pip --version)
(If they use different pythons, please open a new VS Code terminal, it will automatically enter the selected environment.)
Then install the module "Simplejson".
Check the installed modules: (pip show Simplejson)
Run:
Reference: Python Environments in VS Code.

VSCode: Using the MS Python extension cannot execute code

I have been using VSCode with the Microsoft Python extension for a couple of months now. However just today I found that the green button I had in the top right that executed my code is gone. I have tried uninstalling the python extension and reinstalling, I have deleted and redownloaded VSCode, I have tried installing code runner. None of these have fixed the issue.
This is the image of my VSCode, which may help to solve the issue:
When I tried to select the Python: select interpreter I got an error in the bottom right that says,
"Command 'Python: Select Interpreter' resulted in an error (command 'python.setInterpreter' not found)"
I have never experienced an issue like this before so any help is greatly appreciated. Thank you!
Image of the plugin I have installed.
Try the following. If that does not help create a new bug.
Do the following to remove stale versions of the extension:
1.Uninstall Python extension (if you have pylance uninstall it first) in visual studio code.
2.Close all instances of VS Code or close visual studio code.
3.Go to, %USERPROFILE%/.vscode/extensions (on windows) or ~/.vscode/extensions on Windows/Linux/Mac.
4.Go to Run and give %USERPROFILE%/.vscode/extensions (on windows). It will open extensions folder.
5.Delete any folder with the name starting with ms-python.python*
6.Start VS Code, and install Python extension again(also pylance if you uninstalled it in step 1).
So I had faced the problem and couldn't find solution until I stared at VS code and found at the bottom left corner that it was in restricted mode, you have to run it in trust mode and it will fix the problem.
If the option is not coming, there is an option to run the program on the left panel in vs code which looks something like this:
Then you need to click debug and run or whatever the option is, if that is blurred out then you've not saved the file, first save it and then again do the same options, and then it will run.
I figured it out!!! The most recent update for the Microsoft Python plugin on VSCode seems to be bugged or something, I'm not sure. However I installed an older version and everything works now, including bringing back the green run button. Thank you all so much for the help!
Yep it's an issue with the most recent releases of vscode. The quick fix right now is just to roll back to an older version. You can easily do this by pressing CTRL+SHIFT+X in vscode to get to the extensions menu, and then clicking the Python extension, and then clicking the little arrow to the right of "uninstall". Here, you can just click "Install Another Version".
For me v2020.10.332292344 did the trick.
https://github.com/microsoft/vscode-python/issues/14959
Many others are facing the issue right now too.
https://github.com/microsoft/vscode-python/issues/14977
I am using WSL2, and i encountered this error today. My VScode used to work fine until my version was automatically upgraded to 1.60. When i checked my extensions, it said that my Python extension is not installed in WSL2, so i clicked on install and the problem was solved.
It may also come from the restricted mode. Please make sure you're in a trusted window.
"Command 'Python: Select Interpreter' resulted in an error (command 'python.setInterpreter' not found)"
The error happened on the newer version 3.10.6 I installed on my new system.
I uninstalled the new python version. Then went to Microsoft store and installed the older version 3.9 directly from the store. When I tried adding the interpreter again on vscode, it was successful.
reloading or upgrading python extension worked for me.
Kindly install the Python interpreter from the following site:
https://www.python.org/downloads/
Then vs code will ask for permission to run the interpreter.
and then its done

PyCharm OpenCV- autocomplete with import cv2.cv2, no errors with import cv2

I'm just getting started with PyCharm, python, and OpenCV, and I'm trying to set up my environment. I've installed all the necessary packages and I import OpenCV like so:
import cv2
However, this does not autocomplete and shows warnings that the method may be missing when called, BUT if I import like so:
import cv2.cv2
autocomplete does work, but running produces the following error:
Traceback (most recent call last):
File "C:/Users/dunnj/PycharmProjects/TransformApps/transformapps/blackwhite.py", line 1, in <module>
import cv2.cv2 as cv2
AttributeError: 'module' object has no attribute 'cv2'
My Configuration:
PyCharm 2021.2.3 on macOS 11.6
Python 3.9.7 running in a Virtual Environment(VE)
opencv-python 4.5.4.58 installed into the VE via pip using the PyCharm Terminal window
Steps that worked for me to get autocompletion working:
tldr: Update python interpreter settings to point to <full path to venv>/lib/python3.9/site-packages/cv2
In preferences, Select Python Interpreter
Click the setting icon ( gear on right of box that display your Python Interpreter and select Show All
A list of all your configured Interpreters is show with your current interpreter already hi-lighted.
With your interpreter still highlighted, click the Icon that shows a folder and subfolder at the top. Tool tip should say "Show Paths for Selected Interpreter.
Click the + button and add the following path:
<full path to the venv>/lib/python3.9/site-packages/cv2
The .../python3.9... will be different if you are using a different Python Version.
Click Ok until you are back to the main IDE window.
This has worked in three different Virtual environments for me so far. For two of those, I had to restart the IDE for the completions to show up. The remaining one did not require a restart and worked immediately.
Credit to ingolemo from r/learnpython. I was stuck on this for ages and it drove me mad so I'm here sharing.
My OpenCV was installed by using the wrapper opencv-python package
The sys.modules hacking that that module is doing is the source of the
problem. Pycharm doesn't exactly import modules in order to know
what's inside of them, so messing with the imports dynamically like
that confuses pycharm greatly. It's not pycharm's fault, the
opencv-python maintainer should have used a star import rather than
that messy import hack. You should be able to work around the problem
using the technique you stumbled upon. All you have to do is catch and
ignore the error under normal operation:
import cv2
# this is just to unconfuse pycharm
try:
from cv2 import cv2
except ImportError:
pass
just execute the following commands in your project working environment.
pip uninstall opencv-python
pip install opencv-python==4.5.4.60
The proposed import solution did not work for me.
I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.
I solved this way:
Select project interpreter
Click on the settings button next to it and then clicking on the Show paths for selected interpreter
added the directory containing the cv2 library (in my case in the Conda Python library path - e.g. miniconda3/lib/python3.7/site-packages/cv2/python-3.7). In general check the site-packages/cv2/python-X.X directory)
Following Workaround 2 from the JetBrains issue tracker (https://youtrack.jetbrains.com/issue/PY-54649) helped me:
In PyCharm open from menue FILE - SETTINGS
Go to PROJECT:<your_project_name> and select PYTHON INTERPRETER
Click on the gear symbol next to the interpreter path and select SHOW ALL.
Make sure the correct interpreter is selected.
Click on that icon that looks like a folder tree (on the top)
Click on the "+" icon
Select the folder where the opencv package is located
normally (if you installed it via package manager) you will find it in:
<your_project_path>\venv\Lib\site-packages\cv2
Click OK (twice)
Wait for updating skeletons
Installing Jedi solved this problem for me.
You can use pip install jedi in terminal
You can find more info about jedi here: https://pypi.org/project/jedi/
i had the same problem.
i used
import cv2 as cv2
and after that both importing meth
try
try:
import cv2.__init__ as cv2
except ImportError:
pass
If you are using virtualenv, then mark the virtualenv directory as excluded in your project structure in Pycharm project settings.
Encountered this before.
find "cv2.cp38-win_amd64.pyd" in "Lib\site-packages\cv2" path.
Copy it to "DLLs" path.
Work for system python and anaconda environments(need to do this in conda envs path)
PS.
"site-packages" path can be found by "pip --version"
"DLLs" path is located at "Lib\site-packages....\DLLs"

Getting PyCharm to import sklearn

Beginner here.
I’m trying to use sklearn in pycharm. When importing sklearn I get an error that reads “Import error: No module named sklearn”
The project interpreter in pycharm is set to 2.7.10 (/anaconda/bin/python.app), which should be the right one.
Under default preferenes, project interpreter, I see all of anacondas packages. I've double clicked and installed the packages scikit learn and sklearn. I still receive the “Import error: No module named sklearn”
Does anyone know how to solve this problem?
To make sure you have Scikit-learn package installed on your PyCharm IDE, go to File Menu>Settings and search for Interpreter. Select Project Interpreter, and if you dont see Scikit-learn in the list of packages, click the + sign on the right end. It brings us another window with a search bar, where you can type 'Scikit-learn' and install (see screenshots). I hope this helps.
Screenshots:
,
please notice that, in the packages search 'Scikit-learn', instead 'sklearn'
go to terminal- type python -m pip install scikit-learn
then hit enter.
give it some seconds. It will be done.
Sample Image
Double check your python interpreter. Check whether you have select correct interpreter at the first when you create the project.
Or else you can check your interpreter from the project view under External Libraries.
In your case if you didn't select 2.7.10 (/anaconda/bin/python.app) as your interpreter for the project at the beginning still the program give the same error Import error: No module named sklearn.
Same error occurs to me i have fixed by selecting File Menu-> Default Settings-> Project Interpreter -> Press + button and type 'sklearn' Press install button. Installation will be done in 10 to 20 seconds.
If issue not resolved please check you PyCharm Interpreter path. Sometimes your machine have Python 2.7 and Python 3.6 both installed and there may be some conflict by choosing one.
I had exactly the same problem. I'm using PyCharm with Anaconda3 & Python 3.7, and I've installed other packages into/via PyCharms just fine (such as numpy, scipy, and others). But although scikit-learn (which contains sklearn, that was initially confusing) seems fully installed on my system, including "import sklearn" working outside of PyCharm, I could not get the "import sklearn" to succeed inside PyCharm.
I finally got a python-expert friend to help me. He verified everything was correctly installed on my system and verified that PyCharm was somehow messing up.
We finally determined that the venv (virtual environment) was not including scikit-learn (sklearn) even though I had imported it properly into the Project Interpreter in PyCharms.
Solution: Delete and recreate the VENV, specifically ticking the box Inherit Global Site Packages
See here: https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
for how to create a new virtual environment and get to that parameter.
SOLVED:
reinstalled Python 3.7.9 (not the latet)
installed numpy 1.17.5 (not the latest)
installed scikit-learn (latest)
sklearn works now!
For Mac OS:
PyCharm --> Preferences --> Project Interpreter --> Double Click on pip (a new window will open with search option) --> mention 'Scikit-learn' on the search bar --> Install Packages --> Once installed, close that new window --> OK on the existing window
and you are done.

Categories