Skip to content

Instantly share code, notes, and snippets.

  • Star 22 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dineshviswanath/af72af0ae2031cd9949f to your computer and use it in GitHub Desktop.
How to install python and Flask on Mac OS X

#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?

  1. Having different version of libraries for different projects
  2. 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

@gopaljaiswal
Copy link

gopaljaiswal commented Nov 10, 2017

screen shot 2017-11-10 at 14 02 48

@neozero25 I added a snapshot. It is working for me. Need to maintain line alignment.

@tubala
Copy link

tubala commented Jan 1, 2018

cd ~/projets *cd ~/projects

@theblindguidedog
Copy link

Hi gopaljaiswal, why have you indented |return 'Hello, Flask!' (and) app.run(debug=True)| so much? As far as I know, this should produce an error.

@horsecoin
Copy link

horsecoin commented Apr 14, 2019

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

  • Serving Flask app "app" (lazy loading)
  • Environment: production
    WARNING: Do not use the development server in a production environment.
    Use a production WSGI server instead.
  • Debug mode: on
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 176-388-923
    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)
Screen Shot 2019-04-13 at 4.23.55 PM.pngScreen Shot 2019-04-13 at 3.32.27 PM.png

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/) ...

@pcsaunak
Copy link

pcsaunak commented Mar 24, 2020

I was trying to test out the deployment using nginx, but could not make it work in OS X, did not try the steps in Ubuntu. Though I am sure that it should work well in Ubuntu as all articles point out the usage in Ubuntu.

  1. Installed nginx in Mac OS X.

  2. Installed gunicorn.

  3. Created a wsgi.py file and started the server using
    gunicorn --bind 127.0.0.1:5000 wsgi:app

  4. Tested the url and I was able to get my home page.

  5. Went to path /usr/local/etc/nginx and edited the nginx.config file and added the below code
    `
    server {
    listen 80;
    listen [::]:80;
    server_name api.example.com;

     location / {
           proxy_pass         "http://localhost:5000";
           proxy_redirect     off;
           proxy_read_timeout 300;
     }
    

    }

`

  1. Restarted the nginx server but I did not land on the home screen.
  2. I also tried turning on the proxy_redirect but that did not work.

@bethmorais
Copy link

Thank you!
A couple of notes for other people:

  • Create the app.py file within the 'hello_flask' folder. It should should be on the same level as bin and include. (That's where I've got it anyway - and it works).
  • In your terminal - provided you've got all the above steps done and your current terminal position looks similar to this : (hello_flask) Alexs-MacBook-Pro-3:hello_flask Lord$, type python app.py. Then run the above localhost:5000 in your browser.

Thanks for the tip! It worked!

@abdiwahab013
Copy link

i just want to add something very important
if it worked for you ... but you exit the terminal again if you come back it will says

File "/Library/Frameworks/Python.framework/Versions/3.8/bin/flask", line 6, in
from flask.cli import main
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/init.py", line 21, in
from .app import Flask
ModuleNotFoundError: No module named 'flask.app'

*but you just need to repeat the activation which is

source bin/activate

then flask will run

o

@AtheerYahya
Copy link

AtheerYahya commented Jun 15, 2020

I have a problem when running html code within app.py. I know where is the problem, the "return" should return something. here is my code:

from flask import Flask

app = Flask(name)
@app.route('/')

def home():
return
"""
<!doctype html>


<title></title>


Hello Feroo!

"""

if name =='main':
app.debug = True
app.run()


if i did return 'hello world' it will work but that what i don't want. I want it to read the html code!

Any help is appreciated!!

P.s. I am using MacOS Catalina!!!!

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