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

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

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

from flask import Flask

app = Flask(__name__)

def index():
	return 'Hello, Flask!'

if __name__ == '__main__':

Open the web browser with http://localhost:5000

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.

Copy link

tubala commented Jan 1, 2018

cd ~/projets *cd ~/projects

Copy link

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

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 port 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

  • 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 (Press CTRL+C to quit)
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 176-388-923 - - [13/Apr/2019 16:23:52] "GET / HTTP/1.1" 200 - - - [13/Apr/2019 16:23:52] "GET /favicon.ico HTTP/1.1" 404 -

it works with my edits of the all the indents lol
#!/bin/bash python3
from flask import Flask
app = Flask(name)
def index():
return 'Hello, Flask!!!!!'

if name == 'main':
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

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

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 (redis server test script redis-py test file)

2019-04-13 15:28:05 ☆ nickademous in ~/projects/hello_flask
± |master ↑1 ↓2 S:387 U:367 ?:444 ✗| → python3

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 port 8000 ( ...

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 file and started the server using
    gunicorn --bind 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;

     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.

Copy link

Thank you!
A couple of notes for other people:

  • Create the 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 Then run the above localhost:5000 in your browser.

Thanks for the tip! It worked!

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/", line 21, in
from .app import Flask
ModuleNotFoundError: No module named ''

*but you just need to repeat the activation which is

source bin/activate

then flask will run


Copy link

AtheerYahya commented Jun 15, 2020

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

from flask import Flask

app = Flask(name)

def home():
<!doctype html>


Hello Feroo!


if name =='main':
app.debug = True

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