1

I'm on Windows 10 and I am using PyCharm to run my application. I'm pretty new to Python and just coding in general.

I typed:

pip install flask

in the cmd prompt but forgot to open cmd prompt under administrator privileges. Could this be the problem? I ended up re-opening the cmd prompt with administrator rights and typed

pip install flask

again and it showed that it was already installed. I'm pretty sure I have flask installed on my system.

When I try to run the python file below which I named test_webapp.py I get the following error:

from flask import Flask

app = Flask(__name__)

@app.route("/")

def hello():

    return "Hello World!"

if __name__ == "__main__":

    app.run()

I get an error:

ModuleNotFoundError: No module named 'flask'

when i type in python --version into the command prompt i get version 3.6.3.

When i type the same code into the Python 3.6 (32-bit) command line, the code works! However, when i try to run it using the IDLE I get a SyntaxError and the number 6 in Python 3.6.3 gets highlighted in red.

Any ideas as to what's going on?

  • 1
    Possible duplicate of [How to fix "ImportError: No module named ..." error in Python?](https://stackoverflow.com/questions/2325923/how-to-fix-importerror-no-module-named-error-in-python) – cwallenpoole Dec 07 '17 at 20:30
  • always put full error message (Traceback) in question (as text, not screenshot). There are other useful informations. – furas Dec 07 '17 at 20:36
  • 1
    you may have two Pythons installed - and `flask` was installed in one Python and you run code with other Python. If you run code as `python test_webapp.py` then you can use `python -m pip install flask` to use the same Python to install module. – furas Dec 07 '17 at 20:39
  • Type `python --version` into cmd, and check that it is the same as the one you are using for your project – rassa45 Dec 07 '17 at 20:46
  • i typed in python --version into the cmd prompt and i get version 3.6.3. as for the command or line of code python -m pip install flask where am i supposed to put this? in the command prompt? as for the code, i am trying to run it in pycharm. i was able to get the webapp to run in Python however, but not the IDLE. i get a SyntaxError when trying to run it in the IDLE. i really don't know what's going on here. – Kareem Khulusi Dec 08 '17 at 18:52

4 Answers4

3

Try to install flask with specific version of python, do something like:

python[version] -m pip install flask

Note: replace [version] with python version (such as python3).

Ahmad Nourallah
  • 337
  • 1
  • 9
  • i am using python 3.6.3. where do i go to type `python3 -m pip install flask`? do i put that in the command prompt? i tried that and i got an error. one thing i forgot to mention is that I am trying to run my webapp in PyCharm, which seems to not work. when i run the code in the Python 3.6(32-bit) runtime environment that comes with an installation of Python 3.6.3 it works! however, when i try to run it in the IDLE i get a SyntaxError and the number 6 in Python3.6.3 gets highlighted in red. any idea what's going on? – Kareem Khulusi Dec 08 '17 at 19:09
1

Sounds like the pip you are using is not installing for the same python you are using.

try which pip and which python

the they should be in the same folder. Might want to make an alias or two if you have a bunch of pythons (sys, 2, 3, conda etc) or just start with a fresh virtual env for your project. <- best imo.

lciamp
  • 675
  • 1
  • 9
  • 19
  • where do i type `which pip` and `which python`? one thing that i forgot to mention is that i am using PyCharm to run my code/webapp. i tried running the code in the Python 3.6 (32-bit) runtime environment and it worked! however, when i tried to run it in the IDLE it didn't work. any idea as to what's going on? – Kareem Khulusi Dec 08 '17 at 19:04
  • sorry i forgot to mention in the IDLE i get a SyntaxError and the number 6 in Python 3.6.3 at the top of the screen gets highlighted in red. – Kareem Khulusi Dec 08 '17 at 19:12
  • @KareemKhulusi you type `which pip` and `which python` in the console. Also, in pycharm in the preferences you can select which python you are running when you compile. You need to make sure you are running the one you installed flask on. It's probably set to the system python (2.6 or 2.7) by default. You probably installed flask on 3. – lciamp Dec 09 '17 at 17:41
1

When you typed pip install flask in the command prompt, it installed flask in the global environment. That is the reason when you run it in command line it works because it used global environment by default.

Pycharm has a virtual environment, therefore you need to install it in that virtual environment. Open Pycharm, then open the terminal within Pycharm and type "pip install flask".

You can read about virtual environment in python to get a clear idea.

0

Check version of python

python --version

And then use accordingly if python 3.x.x then type

python3 app.py

if python 2.x.x then type

python2 app.py