#Starting Python Web development in Mac OS X#
Objective: Getting started with Python Development Operating System: Mac OS X Python version installed: 3.5 (5th December 2015)
Downoad the lastest Python from https://www.python.org/downloads/
Mac OS uses default 2.x version out of box. To check whether, python has been installed successfully. try the following command.
python3 -V
Python 3.5.0
Above step ensure that Python 3.5 has been installed successfully.
This is the high level outline of this post: Mas OS X -> Python 3.5 -> Virtaulenv -> Flask --> app.py(first Hello world )
Installing virtaulenv: (Step 1 of Why use virtualenv?
- Having different version of libraries for different projects
- Solves the elevated privillege issue as virtualenv allows you to install with user permission
sudo pip3 install virtualenv
virtualenv --version
13.1.2
Now lets create the first flask app
mkdir ~/projects
cd ~/projects
Now we will create a virtualenv
virtualenv hello_flask
cd hello_flask
If you list the contents of the hello_flask directory, you will see that it has created several sub-directories, including a bin folder (Scripts on Windows) that contains copies of both Python and pip. The next step is to activate your new virtualenv.
source bin/activate
Installing Flask in your virtaulenv
pip install Flask
Hello, Flask
Create a new file called app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, Flask!'
if __name__ == '__main__':
app.run(debug=True)
Open the web browser with http://localhost:5000
I had to:
python3 -m http.server
2019-04-13 15:29:45 ☆ nickademous in ~/projects/hello_flask
± |master ↑1 ↓2 S:387 U:367 ?:444 ✗| → python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
then open site in new terminal and
2019-04-13 16:23:10 ☆ nickademous in ~/projects/hello_flask
± |master ↑1 ↓2 S:387 U:367 ?:444 ✗| → python3 app.py
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
127.0.0.1 - - [13/Apr/2019 16:23:52] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Apr/2019 16:23:52] "GET /favicon.ico HTTP/1.1" 404 -
it works with my edits of the app.py all the indents lol
#!/bin/bash python3
from flask import Flask
app = Flask(name)
@app.route('/')
def index():
return 'Hello, Flask!!!!!'
if name == 'main':
app.run(debug=True)
Heres my full log:
2019-04-13 14:21:04 ☆ nickademous in ~
virtualenv notes from my mac!
sudo pip3 install virtualenv
—————
The directory '/Users/nick/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/nick/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
———
Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.3
virtualenv --version
16.4.3
2019-04-13 14:28:19 ☆ nickademous in ~
± |master ↑1 ↓2 S:387 U:367 ?:443 ✗| → mkdir ~/projects
2019-04-13 14:30:03 ☆ nickademous in ~
± |master ↑1 ↓2 S:387 U:367 ?:443 ✗| → cd ~/projects
2019-04-13 14:30:20 ☆ nickademous in ~/projects
± |master ↑1 ↓2 S:387 U:367 ?:443 ✗| → virtualenv hello_flask
Using base prefix '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/nick/projects/hello_flask/bin/python3.7
Also creating executable in /Users/nick/projects/hello_flask/bin/python
Installing setuptools, pip, wheel...
done.
cd hello_flask
source bin/activate
Installing collected packages: click, MarkupSafe, Jinja2, itsdangerous, Werkzeug, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.2 click-7.0 flask-1.0.2 itsdangerous-1.1.0
2019-04-13 14:42:03 ☆ nickademous in ~/projects/hello_flask
pip3 install redis
python3 hello.py (redis server test script redis-py test file)
——————
http://osxdaily.com/2018/07/30/start-web-server-python-3/
2019-04-13 15:28:05 ☆ nickademous in ~/projects/hello_flask
± |master ↑1 ↓2 S:387 U:367 ?:444 ✗| → python3 app.py
2019-04-13 15:29:45 ☆ nickademous in ~/projects/hello_flask
± |master ↑1 ↓2 S:387 U:367 ?:444 ✗| → python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...