0

I am new to python. I am trying to import Flask a python script. But it is throwing an error.
Here's my python script "tokenize.py"

from flask import Flask, Response

But I am getting the following error when running it:

$ python2.7 tokenize.py

Traceback (most recent call last):
  File "tokenize.py", line 4, in <module>
    from flask import Flask, Response  
  File "/Library/Python/2.7/site-packages/flask/__init__.py", line 17, in <module>
    from werkzeug.exceptions import abort
  File "/Library/Python/2.7/site-packages/werkzeug/__init__.py", line 151, in <module>
    __import__('werkzeug.exceptions')
  File "/Library/Python/2.7/site-packages/werkzeug/exceptions.py", line 67, in <module>
    from werkzeug._internal import _get_environ
  File "/Library/Python/2.7/site-packages/werkzeug/_internal.py", line 13, in <module>
    import inspect
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 39, in <module>
    import tokenize
  File "/Users/abashir/tokenize.py", line 4, in <module>
    from flask import Flask, Response  
ImportError: cannot import name Flask

I am working on MacOS High Sierra Version 10.13.6 (17G6S).

$ python2.7 -V
Python 2.7.10

In pip list:

$ pip list
Package                                Version 
-------------------------------------- --------
...
Flask                                  1.0.2
...

And when I try to re-install Flask

$ pip install Flask
Requirement already satisfied: Flask in /Library/Python/2.7/site-packages (1.0.2)
Requirement already satisfied: Jinja2>=2.10 in /Library/Python/2.7/site-packages (from Flask) (2.10)
Requirement already satisfied: itsdangerous>=0.24 in /Library/Python/2.7/site-packages (from Flask) (1.1.0)
Requirement already satisfied: Werkzeug>=0.14 in /Library/Python/2.7/site-packages (from Flask) (0.14.1)
Requirement already satisfied: click>=5.1 in /Library/Python/2.7/site-packages (from Flask) (7.0)
Requirement already satisfied: MarkupSafe>=0.23 in /Library/Python/2.7/site-packages (from Jinja2>=2.10->Flask) (1.1.0)

What is the problem? and how to solve it?

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • 3
    Rename your file `tokenize.py` to something else. – Sohaib Farooqi Jan 21 '19 at 09:28
  • It will depend on which version of Python your OS is defaulting to. Reason I mention that is because when you run pip install [library], pip will go and find the default python interpreter and use that instead of the one you want it to use. So you may have installed Flask using a completely different python interpreter. Do you have just python 2.7 installed or do you have python 3.x installed as well? – Naufal Jan 21 '19 at 09:30
  • @bro-grammer I renamed it to ashraf.py, and it is still the same issue. – Ashraf Bashir Jan 21 '19 at 09:31
  • You might be better of using a virtualenv . Try `which python2.7` – Nihal Sangeeth Jan 21 '19 at 09:33
  • @Naufal, yes I do have python 3 installed `$ python3 -V` > `Python 3.7.2` – Ashraf Bashir Jan 21 '19 at 09:33
  • @NihalSangeeth `$ which python2.7` > `/usr/bin/python2.7` – Ashraf Bashir Jan 21 '19 at 09:33
  • 1
    I suggested you renaming because line 12 of your Traceback is trying to do `import tokenize` but failing due to name collision. – Sohaib Farooqi Jan 21 '19 at 09:33
  • Try running `python2.7 -c "import flask"`, if no error pops up then the problem lies somewhere in your code. – Sohaib Farooqi Jan 21 '19 at 09:38
  • @Ashraf so when you run "which python" (without 2.7), does it point to python3? – Naufal Jan 21 '19 at 09:52
  • What happened if you run with python3 ? – Tarek Jan 21 '19 at 09:56
  • Run this `python2.7 -c "from flask import Flask, Response"` on the terminal and see what is happening. If same error comes install Flask again `sudo pip install Flask --no-cache-dir --force-reinstall` and check. – Ram Idavalapati Jan 21 '19 at 10:55
  • @bro-grammer `python2.7 -c "import flask"` leads to the same issue – Ashraf Bashir Jan 21 '19 at 11:32
  • @Naufal `$ which python` > /usr/bin/python , `$ which python2.7` > /usr/bin/python2.7 , and `$ which python3` > /usr/local/bin/python3 – Ashraf Bashir Jan 21 '19 at 11:34
  • @Tarek, running it with `python3` causes the same error – Ashraf Bashir Jan 21 '19 at 11:35
  • @Ashraf try the following:"cd /usr/bin" and then do "ls -l *python*" (as you're on MacOS) and you should be able to see what python is redirected to e.g. python -> python2.7 (in my case). Also try "which pip" which will tell you which version of pip you're using. There's an alternative, try running python2.7 -m pip install flask then run your tokenize.py. – Naufal Jan 21 '19 at 11:42
  • @RamIdavalapati running `python2.7 -c "from flask import Flask, Response"` causes same issue. I run `sudo pip install Flask --no-cache-dir --force-reinstall` and it successfully finished `Successfully installed Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0` but still same issue when I try to run the script – Ashraf Bashir Jan 21 '19 at 11:45
  • @Naufal your last comment is extremely interesting, when I browsed to `/usr/bin` and run `python2.7 -c "from flask import Flask, Response"` it works! I do not understand what is happening. – Ashraf Bashir Jan 21 '19 at 11:53
  • @Naufal usr/bin is in PATH `$ echo $PATH` > `/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/Users/abashir/apache-maven-3.6.0/bin` – Ashraf Bashir Jan 21 '19 at 11:55
  • from `/usr/bin` running `$ ls -l python2.7` maps to `../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7` – Ashraf Bashir Jan 21 '19 at 11:58
  • @AshrafBashir you'll need to reset your python PATH to /usr/bin/python2.7 then try running your script. Or, as you've got a local python interpreter, point your PATH to that interpreter then run your tokenize.py. Hopefully that should work. – Naufal Jan 21 '19 at 17:35
  • 1
    @AshrafBashir did you manage to solve your issue? BTW I disagree with the person who marked this as a duplicate question, it's not a duplicate question. – Naufal Jan 24 '19 at 10:47

0 Answers0