Reportmissingmodulesource как исправить

When I am writing

from flask import Flask

One Yellow line is coming up under flask and stating Import “flask” could not be resolved from source Pylance (reportMissingModuleSource) . Also, I am able to do the work with this package also successfully. But the thing is, I am not able to use autosuggesstions for Classes and methods very well.

Further:

  1. I have checked that flask is installed successfully.
  2. Also I visited this thread https://github.com/microsoft/pylance-release/issues/236

And I set up my settings.json in vscode as follows:

"python.analysis.extraPaths": [
    "/media/sarimurrab/New Volume/COURSES/Flask/FlaskMigrateforDatabaseMigrations/2"
]

But Still, unable to resolve the error.

davidism's user avatar

davidism

119k29 gold badges384 silver badges334 bronze badges

asked Jan 19, 2021 at 6:01

SARIM's user avatar

When I did not install the module “flask” in the Python environment currently used in VSCode:

enter image description here

Please use the command “pip --version” to check the source of the module installation tool “pip”, the module is installed at this location:

enter image description here

Then, we can use the command “pip show flask” to check the installation location of the module “flask”: (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)

enter image description here

If the “reportMissingModuleSource” message is still displayed here, please reload VS Code.

(F1, Developer: Reload Window)

enter image description here

answered Jan 20, 2021 at 2:43

Jill Cheng's user avatar

Jill ChengJill Cheng

8,8581 gold badge18 silver badges24 bronze badges

10

Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.

To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.

answered Jan 19, 2021 at 8:19

Marius Kimmina's user avatar

2

I had a similar issue while trying to import flask on vscode.
I fixed it by using anaconda. Simply you install the flask module in your created environment example screenshot.

How to create a virtual env in anaconda:
1. On the left sidebar, click on environments.
2. Click create (at the bottom).
3. At the pop-up window, give your vir.env a name
and select the language version.
4. Once created, you can start installing
different modules in your environment.

I hope that helps!

answered Feb 9, 2021 at 20:51

Icaru5's user avatar

Icaru5Icaru5

911 gold badge1 silver badge5 bronze badges

For Linux Mint and for those who have installed flask, but VSCode doesn’t find it:

  1. check the Flask path: pip show flask (should be smth like
    Location: /home/<username>/.local/lib/python3.8/site-packages
  2. in VSCode click left bottom button and choose the python interpreter, in my case I changed it from python3.9 to python3.8 as we can see it in the flask path.

answered Jul 31, 2021 at 4:50

Roy O'Bannon's user avatar

0

That’s because you have not chosen your path correctly,
type:
pipenv --venv
then it will show you where your virtual env is installed. Check where the packages are installed in your env, and then type what comes to you from the shellscripts or whateverpython, and the packages will work.

greybeard's user avatar

greybeard

2,1537 gold badges28 silver badges63 bronze badges

answered Jun 30, 2021 at 4:20

khaled khalifa's user avatar

I solved my problem by using a “Global” version of Python. Maybe Pylance had not updated to work with the version of Python I was using.
interpreter language screenshot

answered Aug 23, 2022 at 13:59

anthony_leporte's user avatar

I have tried all of these and none of these steps worked for me.
Finally, this worked:

Check your IDE’s interpreter settings: If you are using an IDE like PyCharm or Visual Studio Code, make sure that you have selected the correct interpreter for your virtual environment in the IDE’s settings.

Where do we check this in VSCode:

  1. Cmd + Shift + P
  2. Type “Python: Select Interpreter” in the search bar of the Command Palette and select the option from the list.
  3. If your virtual environment is already listed, select it from the list of interpreters. If not, select the “Enter Interpreter Path” option and manually locate the interpreter executable in your virtual environment.
  4. Interpreter for your virtual environment would be: <path_to_virtual_environment>/bin/python

answered Mar 28 at 6:48

Rachana Reddy's user avatar

I was facing same issue. I tried all solutions from stack-overflow but none worked. But after lot of searching and time waste I found my silly mistake. I had created folder named ‘flask’ and stored my project there. I’m beginner and going through lot of such silly mistakes. Hope it would help, if somebody commits same mistake.

answered Nov 3, 2021 at 12:32

Mangesh Sodnar's user avatar

I think this is a Vscode problem ,just restart the Vscode .

answered Jul 9, 2022 at 21:06

Aghiad Alzein's user avatar

By the following steps, I solved this issue:

1)On the project directory create the .flaskenv file

  1. In the .flaskenv file write the following two lines:

FLASK_ENV=development

FLASK_APP=main.py

Please pay attention that main.py is my main file after writing flask run in the Vscode terminal, additionally, you have to create an env folder or requirement file proviosly.

answered Aug 16, 2022 at 8:49

Jamal Azizbeigi's user avatar

I faced this issue because of using pip install flask instead of pip3 install flask.

answered Apr 28 at 16:39

issha jajoo's user avatar

A few answers (Jill’s, Marius’s, and Roy’s) mention the fact that is necessary to choose the correct Python interpreter to make Pylance function properly. I would like to add the fact that this is still necessary to do when using a Jupyter Notebook with the correct Python kernel already chosen.

It is counterintuitive to choose both Python interpreter and notebook’s Python kernel to make things work. It is even more counterintuitive considering the fact that Python interpreter’s button (on the left bottom of the screen, on status bar) does not necessarily appear when a Jupyter Notebook is open, but when a Python script is open. For instance, in this screenshot, we see the little line under Scikit-learn’s import, indicating a problem with the import (even though the import is successful). However, the correct Python kernel, with Scikit-learn installed, is already chosen. Only opening a Python script we notice that the Python interpreter is the reason of this behavior, because a wrong one is chosen, without Scikit-learn. In some sense, one could think that the reason behind this was a problem with the Python kernel or the Conda environment (it is common to experience this kind of problem when experimenting with Jupyter Notebook and Jupyter Lab). I hope this answer may help those who are searching for solving this problem in the specific context of Jupyter Notebooks inside VS Code. They could ignore the other answers because they could think it is not the case for them.

answered Oct 30, 2021 at 18:54

lucasresck's user avatar

lucasrescklucasresck

891 silver badge5 bronze badges

2

I had been working on a webapp in Django recently and I came across this error.

Import “django.urls” could not be resolved from source (reportMissingModuleSource)

Import “django.contrib” could not be resolved from source (reportMissingModuleSource)

I got an error when I tried to import some Django module. This is a screenshot of my code in error.

I was facing this error while working with Python virtual environments. In my case, it was happening because I have installed Django on my virtual environment and my base environment didn’t contain any module named Django.

How to Fix The Issue :

  1. Now what I understood is that pylance uses the python interpreter to analyze our code and mark the error while we write the code.
  2. Since I have three python environments in my system, hence three different python interpreters are available to my VS Code.

So if VS code uses the Python interpreter from the base environment, it will not recognize the Django module (import Error). Therefore, you are required to change the Python interpreter to the one present in your virtual environment.

In Simple Words :

It is not complicated as you think. If you cant understand just follow the below steps

Step 1: Click on the bottom left of the screen to change python interpreter.

Step 2 : Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project.

If you have anymore doubts or if the issue is still not resolved. Do comment it in your comment session.

Published
February 19, 2021March 7, 2021

Comments

@trallnag

@jakebailey
jakebailey

changed the title
‘reportMissingModuleSource’ warning for a single package import

‘reportMissingModuleSource’ warning for requests.packages.*

Mar 19, 2021

joshuacwnewton

added a commit
to spinalcordtoolbox/spinalcordtoolbox
that referenced
this issue

Oct 11, 2022

@joshuacwnewton

PyCharm's linting (and others) complain about this specific import
as though it were an error, because the `requests.urllib3` module
is created dynamically.

But, we can just import `Retry` from `urillib3` directly, as per:
microsoft/pylance-release#597 (comment)

You will likely encounter the error message ‘Import “flask” could not be resolved from source Pylance (reportMissingModuleSource)‘ when working with Python. If you are facing the same problem, don’t worry, this article will help you overcome this problem.

How did this error occur?

The cause of this error is that the ‘flask’ module is not installed in the required environment. 

This error started appearing as soon as you used the import statement. To illustrate, you can see the example below:

As you can see, we cannot import the module ‘flask’ in this situation.

Explain more understandably: You work on your computer and install many different versions of Python for different projects. The error message tells you that ‘flask’ is not installed in the current Python environment in use.

You need to install the flask module. Here is the procedure we recommend to fix this problem:

  1. First, you need to create a Virtual Environment on your terminal.
  2. Use the pip install flask command to install the flask module. 
  3. Use the key combination: CTRL+SHIFT+P.
  4. Search “Python: Select Interpreter”.
  5. Select your Virtual Environment.

Virtual Environment in Python is a virtual environment. Just like virtual machines (Virtual Machine), Virtual Environment establishes a virtual environment, allowing you to freely perform installation, removal, and installation of different versions of Python packages without fear of affecting the available projects.

To create a Virtual Environment, open the terminal in your project and the following command:

pip install virtualenv

To check if the installation of module ‘virtualenv’ has been successful, you can use the pip list command. The result will show a list of installed packages.

Now you can create a virtual environment and use that virtual environment with the following two commands:

::create a virtual environment called env
python -m venv env
 
::activate the virtual environment in your project
.envscriptsactivate

Using the key combination CTRL+SHIFT+P, search for “Python: Select Interpreter”, then select the python environment you have virtualized for your project.

Finally, run the install command for the ‘flask’ module.

pip install flask

Everything seems to be working smoothly, and the error no longer occurs.

Summary

In conclusion, the error “Import “flask” could not be resolved from source Pylance (reportMissingModuleSource)” has been solved. You must install the ‘flask’ module for the required environment before you can use it. That’s the end of this article. We hope the above information is clear and helpful to you.

Maybe you are interested:

  • No module named ‘flask_restful’ in Python
  • No module named ‘flask_sqlalchemy’ in Python

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.

Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js

I was able to repro this after creating the environment and installing flask. But after I close the solution in VS and re-open it, there are no errors:

image
image

I’m thinking this is an issue with updating after the environment is changed. For example, I just manually UNINSTALLED flask from the virtual env, but I don’t see any errors in the views.py file even though there should be.

Even unloading and reloading the project doesn’t seem to update the error. But closing the solution and re-opening it does. I will look into what needs to be called to update the errors, maybe a call into the language server code.

Добавить комментарий