tackvector
tackvector

Reputation: 76

Why can't the import be resolved?

I've seen several answers to this question, albeit none of the solutions have worked for my particular situation. I'm trying to get started building an API with Flask. When I try to import Flask-RESTful, I get an error in VS Code. For context, I am using Windows 11. Here are the first two lines of my .py file:

from flask import Flask
from flask_restful import Resource, Api, reqparse

The error I get reads as:

Import "flask_restful" could not be resolved Pylance(reportMissingImports)

Now, to add more context, I've checked to make sure the interpreter path is set using Ctrl+Shift+P to open the Command Palette and selecting the correct (and the only) Python interpreter for the project inside my virtual environment. When I run pip list, I get this output:

(api) C:\Users\<Username>\OneDrive\Documents\PythonProjects\api>pip list
Package                 Version
----------------------- ---------
aiohttp                 3.8.1
aiosignal               1.2.0
alembic                 1.8.0
aniso8601               9.0.1
anyio                   3.6.1
async-timeout           4.0.2
attrs                   21.4.0
bleach                  5.0.1
certifi                 2022.6.15
charset-normalizer      2.1.0
click                   8.1.3
click-log               0.4.0
colorama                0.4.5
deprecation             2.1.0
docutils                0.19
dotty-dict              1.3.0
Flask                   2.1.2
Flask-Migrate           3.1.0
Flask-RESTful           0.3.9
Flask-SQLAlchemy        2.5.1
flask-swagger           0.2.14
frozenlist              1.3.0
gitdb                   4.0.9
GitPython               3.1.27
gotrue                  0.5.0
greenlet                1.1.2
h11                     0.12.0
httpcore                0.14.7
httpx                   0.21.3
idna                    3.3
importlib-metadata      4.12.0
invoke                  1.7.1
itsdangerous            2.1.2
Jinja2                  3.1.2
keyring                 23.6.0
Mako                    1.2.1
MarkupSafe              2.1.1
multidict               6.0.2
packaging               21.3
pip                     22.0.4
pkginfo                 1.8.3
postgrest-py            0.10.2
psycopg2                2.9.3
pydantic                1.9.1
Pygments                2.12.0
pyparsing               3.0.9
python-dateutil         2.8.2
python-gitlab           3.6.0
python-semantic-release 7.28.1
pytz                    2022.1
pywin32-ctypes          0.2.0
PyYAML                  6.0
readme-renderer         35.0
realtime                0.0.4
requests                2.28.1
requests-toolbelt       0.9.1
rfc3986                 1.5.0
semver                  2.13.0
setuptools              58.1.0
setuptools-scm          7.0.4
six                     1.16.0
smmap                   5.0.0
sniffio                 1.2.0
SQLAlchemy              1.4.39
storage3                0.3.4
supabase                0.5.8
supabase-client         0.2.4
tomli                   2.0.1
tomlkit                 0.10.2
tqdm                    4.64.0
twine                   3.8.0
typing_extensions       4.3.0
urllib3                 1.26.10
webencodings            0.5.1
websockets              9.1
Werkzeug                2.1.2
wheel                   0.37.1
yarl                    1.7.2
zipp                    3.8.0

Why would the flask...Flask import work, but not flask_restful? I can see both in the Lib\site-packages folder in my project directory and the output from pip list outside the virtual environment is different, which signals to me that there isn't an issue with the path or directories.

EDIT: I forgot to mention that when I run the code using Ctrl + Alt + N, I get this output:

Traceback (most recent call last):
  File "c:\Users\<Username>\OneDrive\Documents\PythonProjects\api\api.py", line 3, in <module>
    from flask_restful import Resource, Api, reqparse
ModuleNotFoundError: No module named 'flask_restful'

Again, no errors with importing flask, only with flask_restful.

Any help with this will be greatly appreciated! Thank you in advance for your time. I'm happy to provide more info if needed. Thanks.

EDIT: I have updated pip and attempted to simply run the program inside the command prompt. This is what I got. I'm still getting the import error inside VS Code, though. I am going to see if using a different version of Python makes a difference. Thanks everyone for all of your help so far, I appreciate it!

EDIT: Okay, it seems like the issue is a little closer to being solved. So, I updated pip. I retried setting the interpreter path and, which some of you mentioned, it turns out that I'd been doing it wrong. I had to do Ctrl + Shift + P >> Python: Select Interpreter >> Enter interpreter path and select the correct path that way. I did this by going into the project directory, going to the scripts folder, and selecting python.exe.

That solved the issue with Pylance. I no longer see an error in the editor when working on the project. However, the interpreter will not show in the bottom right hand corner of the window. That may just be a bug and I can either look through the issues on GitHub or open a new one some other time I assume.

When I run the code with Ctrl + Alt + N I get a ModuleNotFoundError relating to flask_restful again. But, when I run set flask_app=api.py >> flask run in the terminal, it has changed from a white background in the browser to a black background and displays the message it is intended to display (a simple "Hello, World" as a test).

Should I just keep going until I run into another issue? I also tried python -m api and that worked as well. Should I just ignore the VS Code output window? Also, sorry about the late replies. I appreciate everyone's help and patience.

Upvotes: 4

Views: 9650

Answers (2)

niksy
niksy

Reputation: 445

ok this issue is definitely with the chose python interpreter.

  1. check what is the version of python you are using
  2. check whether you are using wsl on windows or linux or command prompt

now in my case choose the interpreter was not solving the issue. I even tried the virtual environment and giving a path. but python by default was installing the .exe in a hidden folder and i couldn't find the files in the env folder

i was using wsl on windows and visual studio code.

so i ran pip installed outside the env. and also on the powershell/cmd and the terminal. and the error went away.

Upvotes: 1

MingJie-MSFT
MingJie-MSFT

Reputation: 9437

Use the Ctrl+Shift+P command, search for and select Python:Select Interpreter(Or click directly on the python version displayed in the lower right corner), and select the correct interpreter.

enter image description here

Upvotes: 6

Related Questions