-1

Maybe I'm doing something wrong, but after 1 hour of staring at the code I'm not getting smarter.

My problem is with my virtualenv. I set up venv2 and venv3 as folders in my home-directory. I installed Flask on both of them, alongside with other packages.

The problem is that I simply can't run the HelloWorld-Example from Flask.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World'

Example with venv2:

(venv2) #( 7.09.16@14:11 )( dun@Arch64L ):~ which python
/home/dun/venv2/bin/python
(venv2) #( 7.09.16@14:12 )( dun@Arch64L ):~ which python3 
/usr/bin/python3
(venv2) #( 7.09.16@14:12 )( dun@Arch64L ):~ which pip
/home/dun/venv2/bin/pip
(venv2) #( 7.09.16@14:12 )( dun@Arch64L ):~ cd _workspace/py/flask 
(venv2) #( 7.09.16@14:12 )( dun@Arch64L ):~/_workspace/py/flask python2 helloworld.py 
Traceback (most recent call last):
  File "helloworld.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name Flask
(venv2) #( 7.09.16@14:13 )( dun@Arch64L ):~/_workspace/py/flask pip list
click (6.6)
Flask (0.11.1)
itsdangerous (0.24)
Jinja2 (2.8)
MarkupSafe (0.23)
pip (8.1.2)
setuptools (26.1.1)
Werkzeug (0.11.11)
wheel (0.29.0)
(venv2) #( 7.09.16@14:13 )( dun@Arch64L ):~/_workspace/py/flask python2 helloworld.py
Traceback (most recent call last):
  File "helloworld.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name Flask

Example with venv3:

(venv3) #( 7.09.16@14:10 )( dun@Arch64L ):~ which python
/home/dun/venv3/bin/python
(venv3) #( 7.09.16@14:10 )( dun@Arch64L ):~ which pip
/home/dun/venv3/bin/pip
(venv3) #( 7.09.16@14:10 )( dun@Arch64L ):~ pip list
click (6.6)
Django (1.10.1)
Flask (0.11.1)
itsdangerous (0.24)
Jinja2 (2.8)
MarkupSafe (0.23)
PasteDeploy (1.5.2)
pip (8.1.2)
pyramid (1.7.3)
repoze.lru (0.6)
setuptools (26.1.1)
translationstring (1.3)
venusian (1.0)
WebOb (1.6.1)
Werkzeug (0.11.11)
wheel (0.29.0)
zope.deprecation (4.1.2)
zope.interface (4.3.2)
(venv3) #( 7.09.16@14:10 )( dun@Arch64L ):~ cd _workspace/py/flask 
(venv3) #( 7.09.16@14:11 )( dun@Arch64L ):~/_workspace/py/flask python helloworld.py 
Traceback (most recent call last):
  File "helloworld.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name 'Flask'
dun
  • 353
  • 1
  • 2
  • 18

2 Answers2

0

I don't see your file structure, so I can't be sure, but if you have a file called flask.py in the same folder, python will import it instead of the Flask module

Ultcyber
  • 396
  • 1
  • 6
  • Even I thought the same but the file seems to be named `helloworld.py`. Edit: You mean in the same folder. Makes sense. – ham Sep 07 '16 at 12:45
  • So did it help? – Ultcyber Sep 07 '16 at 12:48
  • I am not OP. Only our usernames are similar! – ham Sep 07 '16 at 12:50
  • Ok, sorry, I've mistaken you by accident :) – Ultcyber Sep 07 '16 at 12:50
  • Omg, I had an empty file with the name "flask.py" in this folder. Probably created it yesterday, while I was looking into how virtualenv works. I feel so retarded right now. But thank you guys very much for helping me! – dun Sep 07 '16 at 12:57
  • Sometimes you miss those little things :). Edit: also, it's a good practice (for avoiding situations like this) to avoid naming your files after module names – Ultcyber Sep 07 '16 at 12:59
0

I think this is the answer:

/_workspace/py/flask python helloworld.py

use underscope '_' in names, python thinks you want to import flask from you 'flask python helloworld'.

waniz
  • 53
  • 3