1

When I run my flask project on my server, I get the following error:

Traceback (most recent call last):
  File "File.py", line 1, in <module>
    from flask import Flask, request
ImportError: No module named 'flask'

But I have flask installed!

$sudo pip install flask
Requirement already satisfied: flask in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: itsdangerous>=0.21 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: click>=2.0 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: Werkzeug>=0.7 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: Jinja2>=2.4 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2>=2.4->flask)

This is driving me crazy. I've tried what other SO answers have suggested, including using a venv, but they all produce this same error. Anyone have a suggestion?

$ which python
/usr/bin/python
$ which python3
/usr/bin/python3

I'm running python 3.5.2

CaptainForge
  • 1,365
  • 7
  • 21
  • 46

1 Answers1

6

I'm running python 3.5.2

As the text says, Flask is not installed there.

Requirement already satisfied: flask in /usr/local/lib/python2.7/dist-packages

This folder is also the system Python, not the virtualenv, if you are using one. You can set virtualenv to use the system installed packages, but again, that path is not Python3.

The root issue is that using sudo executed pip under a different user account

Either use Python2, or install using pip3 or python3 -m pip and you shouldn't need sudo

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Yes. Using sudo will install the package for root user. Not for the virtualenv user. So removing sudo from installation command should solve the issue. – Nabin Sep 16 '17 at 03:44