Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dineshviswanath/af72af0ae2031cd9949f to your computer and use it in GitHub Desktop.
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

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

@julianacastilloaraujo
Copy link

Thank you so much!!

  • Finally, a good example for Mac OS
Captura de pantalla 2024-03-01 a la(s) 2 35 17 p  m

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