24

Am new in flask development and trying to import flask in my project but Pylint is giving this error in VSCode E0401:Unable to import 'flask', but I installed flask already in my project but I don't know how to solve this, how can I resolve it.

My virtual environment is installed , and the image which proves is below:

enter image description here

The error am getting code , the underlined from: enter image description here

what am I missing?

user
  • 1,220
  • 1
  • 12
  • 31
  • are you using a virtualenv ? – Derlin Jun 18 '18 at 08:44
  • yes I am using it too I installed it , maybe theres a way of checking it if its installed –  Jun 18 '18 at 08:46
  • @Derlin, when I instll it again they say `Requirement already satisfied: virtualenv in c:\users\hpa\appdata\local\programs\python\python36-32\lib\site-packages (16.0.0)` which shows its already there –  Jun 18 '18 at 08:50
  • Did you ever solve this? – Shaun Apr 05 '19 at 21:38

11 Answers11

35

Solution is to switch Interpreter

Simple solution :

Go to command palette Type-- Python:Select Interpreter

Select virtual environment that you created

answered specifically for vscode can work for other also .

Devanshu
  • 883
  • 10
  • 22
  • 1
    This worked for me. I was following the [gcp Flask tutorial](https://cloud.google.com/appengine/docs/standard/python3/building-app/writing-web-service) and received the same error. On my system (macOS) I had to type `>Python:` to display a list of python commands, then select `Set Interpreter` – suspicious_williams Dec 01 '19 at 21:09
  • Also need to restart the VS Code terminal to pick that up! – montrealist Jun 29 '20 at 19:27
  • To expand on "Select virtual environment that you created": I had to browse to `venv/Scripts/python.exe` (on Windows). – Noumenon Jan 27 '21 at 20:12
14

Go to Command Palette using Ctrl + Shift + P. select python:select interpreter and then choose your appropriate virtualenv.

Gowtham Sridhar
  • 141
  • 1
  • 2
10

I guess VS Code doesn't use the correct virtualenv.

To select a virtualenv using the GUI, see Configuring Python environments.

If you use the terminal and have code in your path, launch VS code from your workspace and the virtualenv will be loaded automatically:

cd python-workspace
code -n python-workspace

(note: the -n is for new window)

If this does not work, ensure that pylint is installed in your virtualenv (i.e. your are not using the global pylint). If it still does not work, have a look at this troubleshooting guide.

Derlin
  • 9,572
  • 2
  • 32
  • 53
4

Due to the fact that you are using a virtual environment, first of all it's required that Pylint is installed inside this virtual env.

Furthermore, you need to add the following entry to your workspace settings to avoid the [pylint] E0401 error:

"python.linting.pylintPath": "/path/to/your/virtualenv/bin/pylint"

This overrides the user settings (global settings) and instructs VSCode to use the Pylint version within your virtual env (instead of the global one). That was probably the point you was missing ;-)

Joey
  • 1,436
  • 2
  • 19
  • 33
3

I have the same problem when I code in VS code.

I open Command Palette by ⇧⌘P. And run the linter to solve this problem like this picture.

enter image description here

Here is the document from VS code.

Jeffrey Wang
  • 177
  • 1
  • 8
1

The python extension picks up the venv automatically, and activates it for new terminals panes: Select Interpreter already selects ./venv/bin/python by default

So selecting interpreter is not the issue. The only way I can get this to work is to install pylint into the user path after activating the venv:

./venv/bin/pip install -U pylint

How this works to solve the E0401 I got in VSCode, don't ask me, I can't explain it. Feels like cheating. I thought the point of using virtualenvs was so that we could avoid having to deal with ambiguities across system/site/user modules.

conny
  • 9,973
  • 6
  • 38
  • 47
0

In my case, the vscode cannot run the lint becouse my flask install with venv environment, so I deactivate the vent and install flask again in the normal environment , the lint will be work~

user3488542
  • 103
  • 10
0

I solved the problem using the command

sudo apt install python3-flask

0

You can just install pylint in your virtualenv using

pip install pylint

(inside your terminal) and restart your VS code.

Suryansh Singh
  • 1,123
  • 7
  • 15
0

Please check if you have created a .pylintrc file at the root of your project.

-1

In my case, I have disabled pylint. and it works

Use Ctrl+Shift+P to open the command pallet in VSCode and type python select linter, then you can disable the linter there.

JonSG
  • 10,542
  • 2
  • 25
  • 36
Dimuthu Lakshan
  • 173
  • 1
  • 8