hello.py file in video Hello, Flask #2

Closed
m2web opened this Issue Oct 10, 2014 · 14 comments

Comments

Projects
None yet
2 participants
@m2web

m2web commented Oct 10, 2014

Miguel,
I am using Ubuntu 12.04 64bit and I am attempting to run the hello.py file in video Hello, Flask. Here is the terminal text:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video/1a$ python hello.py
Traceback (most recent call last):
File "hello.py", line 1, in <module>
from flask import Flask
ImportError: No module named 'flask'

I did have an issue installing pip but was able to do it via:
$ sudo apt-get install python3-setuptools
$ sudo easy_install3 pip
But it appears that this is not sufficient.

@miguelgrinberg

This comment has been minimized.

Show comment
Hide comment
@miguelgrinberg

miguelgrinberg Oct 11, 2014

Owner

Did you run pip install flask after activating the virtual environment as I show in the video?
Also, note that you do not need to install pip, as it gets installed into the virtual environment for you.

Owner

miguelgrinberg commented Oct 11, 2014

Did you run pip install flask after activating the virtual environment as I show in the video?
Also, note that you do not need to install pip, as it gets installed into the virtual environment for you.

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 11, 2014

When I attempted to install flask via pip I got the typical error when you do not have the executable script installed such as "pip command not found." If I remember correctly I was in the virtual environment at the time. In any event, I now have it.
When I run pip install flask from the prompt it shows itself installed:

Requirement already satisfied (use --upgrade to upgrade): flask in /usr/local/lib/python3.2/dist-packages
Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in /usr/local/lib/python3.2/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/local/lib/python3.2/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): itsdangerous>=0.21 in /usr/local/lib/python3.2/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): markupsafe in /usr/local/lib/python3.2/dist-packages (from Jinja2>=2.4->flask)
Cleaning up...

When at the python interpreter:

venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ python3.4
Python 3.4.1 (default, May 25 2014, 22:33:14) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'flask'
>>> quit()

Then back at the prompt I see that there is Flask via pip's freeze:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ pip freeze
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
itsdangerous==0.24
wsgiref==0.1.2

Again when attempting to run the script:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video/1a$ python3.4 hello.py
Traceback (most recent call last):
File "hello.py", line 1, in <module>
from flask import Flask

m2web commented Oct 11, 2014

When I attempted to install flask via pip I got the typical error when you do not have the executable script installed such as "pip command not found." If I remember correctly I was in the virtual environment at the time. In any event, I now have it.
When I run pip install flask from the prompt it shows itself installed:

Requirement already satisfied (use --upgrade to upgrade): flask in /usr/local/lib/python3.2/dist-packages
Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in /usr/local/lib/python3.2/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/local/lib/python3.2/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): itsdangerous>=0.21 in /usr/local/lib/python3.2/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): markupsafe in /usr/local/lib/python3.2/dist-packages (from Jinja2>=2.4->flask)
Cleaning up...

When at the python interpreter:

venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ python3.4
Python 3.4.1 (default, May 25 2014, 22:33:14) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'flask'
>>> quit()

Then back at the prompt I see that there is Flask via pip's freeze:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ pip freeze
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
itsdangerous==0.24
wsgiref==0.1.2

Again when attempting to run the script:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video/1a$ python3.4 hello.py
Traceback (most recent call last):
File "hello.py", line 1, in <module>
from flask import Flask
@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 11, 2014

Interesting...when using python3.2, as that is where flask appears to be installed in my /usr/local/lib/python3.2 and not /usr/local/lib/python3.4, I get the following when attempting to run the hello.py script:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video/1a$ python3.2 hello.py 
Traceback (most recent call last):
File "hello.py", line 1, in <module>
from flask import Flask
File "/usr/local/lib/python3.2/dist-packages/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/usr/local/lib/python3.2/dist-packages/werkzeug/__init__.py", line 154, in <module>
__import__('werkzeug.exceptions')
File "/usr/local/lib/python3.2/dist-packages/werkzeug/exceptions.py", line 111
return u'<p>%s</p>' % escape(self.description)
                  ^
SyntaxError: invalid syntax

Is there a way to target the pip install of the flask module to /usr/local/lib/python3.4?

m2web commented Oct 11, 2014

Interesting...when using python3.2, as that is where flask appears to be installed in my /usr/local/lib/python3.2 and not /usr/local/lib/python3.4, I get the following when attempting to run the hello.py script:

(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video/1a$ python3.2 hello.py 
Traceback (most recent call last):
File "hello.py", line 1, in <module>
from flask import Flask
File "/usr/local/lib/python3.2/dist-packages/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/usr/local/lib/python3.2/dist-packages/werkzeug/__init__.py", line 154, in <module>
__import__('werkzeug.exceptions')
File "/usr/local/lib/python3.2/dist-packages/werkzeug/exceptions.py", line 111
return u'<p>%s</p>' % escape(self.description)
                  ^
SyntaxError: invalid syntax

Is there a way to target the pip install of the flask module to /usr/local/lib/python3.4?

@miguelgrinberg

This comment has been minimized.

Show comment
Hide comment
@miguelgrinberg

miguelgrinberg Oct 11, 2014

Owner

Mark, the supported versions of Python are 2.7 and 3.3+. Flask itself does not run on 3.2, so you will find an endless list of problems with that version. Your Python 3.2 interpreter is probably one that you've got from Ubuntu, which is still on 3.2. That's not good, so if you don't need it for anything else I recommend that you remove it from your system, it's too old.

You did not seem to have the virtual environment activated when you executed pip install flask, and that is why Flask got installed in your global Python interpreter (the path you have is /usr/local/lib/python3.2/dist-packages/flask). This should not have worked for a regular user, so I assume you went and did it with sudo, unlike how I do it in the video. This is incorrect, the method I show in the video should work, without the need to install Flask with sudo.

Once you remove 3.2 from your system you can use 2.7 (which should also be installed), or build 3.4 from source. If you go with 2.7, then you need to install virtualenv with sudo apt-get install python-virtualenv.

Assuming you use 2.7, then clone the GitHub repository, then create the virtualenv with virtualenv venv, then activate the virtualenv with source venv/bin/activate, and then run pip install flask. Now Flask will go to the virtualenv, which you fully own, so you do not need a privileged user to do it.

With the virtualenv activated you can run python hello.py in the 1a directory and Flask should be imported fine.

I hope this helps.

Owner

miguelgrinberg commented Oct 11, 2014

Mark, the supported versions of Python are 2.7 and 3.3+. Flask itself does not run on 3.2, so you will find an endless list of problems with that version. Your Python 3.2 interpreter is probably one that you've got from Ubuntu, which is still on 3.2. That's not good, so if you don't need it for anything else I recommend that you remove it from your system, it's too old.

You did not seem to have the virtual environment activated when you executed pip install flask, and that is why Flask got installed in your global Python interpreter (the path you have is /usr/local/lib/python3.2/dist-packages/flask). This should not have worked for a regular user, so I assume you went and did it with sudo, unlike how I do it in the video. This is incorrect, the method I show in the video should work, without the need to install Flask with sudo.

Once you remove 3.2 from your system you can use 2.7 (which should also be installed), or build 3.4 from source. If you go with 2.7, then you need to install virtualenv with sudo apt-get install python-virtualenv.

Assuming you use 2.7, then clone the GitHub repository, then create the virtualenv with virtualenv venv, then activate the virtualenv with source venv/bin/activate, and then run pip install flask. Now Flask will go to the virtualenv, which you fully own, so you do not need a privileged user to do it.

With the virtualenv activated you can run python hello.py in the 1a directory and Flask should be imported fine.

I hope this helps.

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 11, 2014

Miguel,
First, thank you for your responsiveness and willingness to help somewhat of a python newbie. It is appreciated!
I have removed python3.2 from my system as well as the install of pip that I did via a:

$ sudo apt-get install python-pip

Moreover, I have set the alias for the version of python to be python3.4 via:

alias python=python3.4

in ~/.bashrc. Next, I removed the oreilly-intro-to-flask-video directory and then re-cloned it from the git repo. Yet, I am back to where I was when I manually install pip. When in the virtual enviroment, and a enter the pip command

(venv) mark@mark-hp-ubuntu:~/python/oreilly-intro-to-flask-video$ pip install flask
The program 'pip' is currently not installed.  You can install it by typing:
sudo apt-get install python-pip

When I look in the venv/local/bin directory, I see pip, pip3, and pip3.4. Is there a config change that I need to make to ensure that when in venv the venv/local/bin/pip is accessed?

m2web commented Oct 11, 2014

Miguel,
First, thank you for your responsiveness and willingness to help somewhat of a python newbie. It is appreciated!
I have removed python3.2 from my system as well as the install of pip that I did via a:

$ sudo apt-get install python-pip

Moreover, I have set the alias for the version of python to be python3.4 via:

alias python=python3.4

in ~/.bashrc. Next, I removed the oreilly-intro-to-flask-video directory and then re-cloned it from the git repo. Yet, I am back to where I was when I manually install pip. When in the virtual enviroment, and a enter the pip command

(venv) mark@mark-hp-ubuntu:~/python/oreilly-intro-to-flask-video$ pip install flask
The program 'pip' is currently not installed.  You can install it by typing:
sudo apt-get install python-pip

When I look in the venv/local/bin directory, I see pip, pip3, and pip3.4. Is there a config change that I need to make to ensure that when in venv the venv/local/bin/pip is accessed?

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 11, 2014

Found a work around via https://stackoverflow.com/questions/15370872/why-do-distribute-and-pip-install-to-my-virtualenvs-local-bin.
I inserted PATH="$VIRTUAL_ENV/local/bin:$PATH" at line 45 of the venv/bin/activate script.
Now when running:

(venv) mark@mark-hp-ubuntu:~/python/oreilly-intro-to-flask-video/1a$ python hello.py 
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

And I can browse to http://localhost:5000/ and see the Hello World greeting. :-)

m2web commented Oct 11, 2014

Found a work around via https://stackoverflow.com/questions/15370872/why-do-distribute-and-pip-install-to-my-virtualenvs-local-bin.
I inserted PATH="$VIRTUAL_ENV/local/bin:$PATH" at line 45 of the venv/bin/activate script.
Now when running:

(venv) mark@mark-hp-ubuntu:~/python/oreilly-intro-to-flask-video/1a$ python hello.py 
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

And I can browse to http://localhost:5000/ and see the Hello World greeting. :-)

@miguelgrinberg

This comment has been minimized.

Show comment
Hide comment
@miguelgrinberg

miguelgrinberg Oct 11, 2014

Owner

Hmm. Glad you made it work, but I still would like to ensure you have your virtual environment correctly set up if you don't mind. This may save you some troubles down the road.

Can you tell me what command did you use to create the virtualenv? If you are using Python 3.4 you should have used something like python3.4 -m venv venv.

Owner

miguelgrinberg commented Oct 11, 2014

Hmm. Glad you made it work, but I still would like to ensure you have your virtual environment correctly set up if you don't mind. This may save you some troubles down the road.

Can you tell me what command did you use to create the virtualenv? If you are using Python 3.4 you should have used something like python3.4 -m venv venv.

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 11, 2014

If I remember correctly that is what I did was python3.4 -m venv venv.

m2web commented Oct 11, 2014

If I remember correctly that is what I did was python3.4 -m venv venv.

@miguelgrinberg

This comment has been minimized.

Show comment
Hide comment
@miguelgrinberg

miguelgrinberg Oct 12, 2014

Owner

Okay, then you are probably okay. Just wanted to make sure Flask went into the virtualenv and not into /usr/local/lib/....

I'm not sure why you needed to change the PATH variable, however. The StackOverflow question that you linked is for Python 3.3, which has a different implementation of virtual environments. When you use 3.4 you do not need to install anything on your virtualenv, you get it ready to go (this is not true for 3.3).

If you feel like doing an exercise, then deactivate your virtualenv with deactivate, then rename your venv folder to something like venv-old just to keep it around just in case and then try to recreate your virtualenv from scratch. The commands to do that are:

$ python3.4 -m venv venv
$ source venv/bin/activate
(venv) $ pip install flask
(venv) $ cd 1a
(venv) $ python hello.py

This is, I think, all you need to do to get the system setup for the first exercise in the video. If you run into any trouble you can restore the venv-old folder.

Owner

miguelgrinberg commented Oct 12, 2014

Okay, then you are probably okay. Just wanted to make sure Flask went into the virtualenv and not into /usr/local/lib/....

I'm not sure why you needed to change the PATH variable, however. The StackOverflow question that you linked is for Python 3.3, which has a different implementation of virtual environments. When you use 3.4 you do not need to install anything on your virtualenv, you get it ready to go (this is not true for 3.3).

If you feel like doing an exercise, then deactivate your virtualenv with deactivate, then rename your venv folder to something like venv-old just to keep it around just in case and then try to recreate your virtualenv from scratch. The commands to do that are:

$ python3.4 -m venv venv
$ source venv/bin/activate
(venv) $ pip install flask
(venv) $ cd 1a
(venv) $ python hello.py

This is, I think, all you need to do to get the system setup for the first exercise in the video. If you run into any trouble you can restore the venv-old folder.

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 12, 2014

I did the following with same results concerning installing pip:

mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ mv venv venv_old
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ ls
1a  2b  2e  2h  3c  4b  6a  8a  9a  LICENSE           venv_old
1b  2c  2f  3a  3d  4c  6b  8b  9b  README.md
2a  2d  2g  3b  4a  5a  7a  8c  9c  requirements.txt
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ python3.4 -m venv venv
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ ls
1a  2b  2e  2h  3c  4b  6a  8a  9a  LICENSE           venv
1b  2c  2f  3a  3d  4c  6b  8b  9b  README.md         venv_old
2a  2d  2g  3b  4a  5a  7a  8c  9c  requirements.txt
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ source venv/bin/activate
(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ pip install flask
The program 'pip' is currently not installed.  You can install it by typing:
sudo apt-get install python-pip

I then removed the new venv, renamed the venv_old to venv, and am good to go. Not sure if this is an Ubuntu 64bit 12.04 issue or not.

m2web commented Oct 12, 2014

I did the following with same results concerning installing pip:

mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ mv venv venv_old
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ ls
1a  2b  2e  2h  3c  4b  6a  8a  9a  LICENSE           venv_old
1b  2c  2f  3a  3d  4c  6b  8b  9b  README.md
2a  2d  2g  3b  4a  5a  7a  8c  9c  requirements.txt
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ python3.4 -m venv venv
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ ls
1a  2b  2e  2h  3c  4b  6a  8a  9a  LICENSE           venv
1b  2c  2f  3a  3d  4c  6b  8b  9b  README.md         venv_old
2a  2d  2g  3b  4a  5a  7a  8c  9c  requirements.txt
mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ source venv/bin/activate
(venv) mark@mark-netbook:~/python/oreilly-intro-to-flask-video$ pip install flask
The program 'pip' is currently not installed.  You can install it by typing:
sudo apt-get install python-pip

I then removed the new venv, renamed the venv_old to venv, and am good to go. Not sure if this is an Ubuntu 64bit 12.04 issue or not.

@miguelgrinberg

This comment has been minimized.

Show comment
Hide comment
@miguelgrinberg

miguelgrinberg Oct 12, 2014

Owner

Your Python 3.4 behaves like a 3.3 version. How did you install 3.4 on your system? I have an Ubuntu machine here running 14.04, I installed 3.4 by compiling the source code.

Owner

miguelgrinberg commented Oct 12, 2014

Your Python 3.4 behaves like a 3.3 version. How did you install 3.4 on your system? I have an Ubuntu machine here running 14.04, I installed 3.4 by compiling the source code.

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 12, 2014

I did not build from source but got it from a Debian package:

$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python3.4

Is that something I should do instead?

m2web commented Oct 12, 2014

I did not build from source but got it from a Debian package:

$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python3.4

Is that something I should do instead?

@miguelgrinberg

This comment has been minimized.

Show comment
Hide comment
@miguelgrinberg

miguelgrinberg Oct 12, 2014

Owner

This is very interesting. I installed the ppa version of Python 3.4 (which is not an official release, BTW), and was able to reproduce your problem. With this version pip gets installed in venv/local/bin, and PATH is never modified to include that directory.

When you install Python 3.4 from source code the structure of the virtualenvs is different, all the executables go into venv/bin, and there is no local directory.

So it seems this is a bug in this unofficial package that you installed. As long as you know the workaround you will be fine, I think. If you prefer to install Python from source, here are the commands that I used:

$ wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz
$ tar xvzf Python-3.4.2.tgz 
$ cd Python-3.4.2/
$ ./configure
$ make
$ sudo make install

If you decide to do this, then first remove your ppa based Python to avoid collisions.

Owner

miguelgrinberg commented Oct 12, 2014

This is very interesting. I installed the ppa version of Python 3.4 (which is not an official release, BTW), and was able to reproduce your problem. With this version pip gets installed in venv/local/bin, and PATH is never modified to include that directory.

When you install Python 3.4 from source code the structure of the virtualenvs is different, all the executables go into venv/bin, and there is no local directory.

So it seems this is a bug in this unofficial package that you installed. As long as you know the workaround you will be fine, I think. If you prefer to install Python from source, here are the commands that I used:

$ wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz
$ tar xvzf Python-3.4.2.tgz 
$ cd Python-3.4.2/
$ ./configure
$ make
$ sudo make install

If you decide to do this, then first remove your ppa based Python to avoid collisions.

@m2web

This comment has been minimized.

Show comment
Hide comment
@m2web

m2web Oct 12, 2014

Will do. My next step will step will be to:

$ sudo apt-add-repository --remove ppa:fkrull/deadsnakes

Remove /usr/local/lib/python3.4, then pull the tar down, and do a make and install. In short, hopefully others will see this and not have to go this route! Thanks again Miguel.

m2web commented Oct 12, 2014

Will do. My next step will step will be to:

$ sudo apt-add-repository --remove ppa:fkrull/deadsnakes

Remove /usr/local/lib/python3.4, then pull the tar down, and do a make and install. In short, hopefully others will see this and not have to go this route! Thanks again Miguel.

@m2web m2web closed this Oct 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment