Full Stack Python logo Full Stack Python

全部主题 | Blog | 时讯 | @fullstackpython | Facebook | 源码 # How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS Posted by Matt Makai on 五月 10, 2016. Last updated 八月 10, 2016.

Ubuntu's latest Long Term Support (LTS) operating system was released last month, in April 2016. The 16.04 update for Ubuntu is known as "Xenial Xerus" and it's the first Ubuntu release to include Python 3 as the default Python installation.

We can use this new Ubuntu release along with Python version 3.5 to start a new Flask web application project and run it with Green Unicorn (Gunicorn).

Tools We'll Need

We'll need the Ubuntu 16.04 release along with a few other libraries to complete our project. You don't have to install these tools just yet, we will get to them as we progress through the walkthrough. Our requirements and their current versions as of May 10, 2016 are:

If you're running on Mac OS X or Windows, use virtualization software such as Parallels or VirtualBox with the Ubuntu .iso file. Either the amd64 or i386 version of 16.04 is fine. I'm using amd64 for development and testing in this tutorial.

Once you boot up Ubuntu, you should see a screen like this one.

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图2

Open up a terminal window to proceed with the setup.

System Packages

We can see the python3 system version Ubuntu comes with and where its executable is stored using these commands.

  1. python3 --version
  2. which python3

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图3

Our Ubuntu installation requires a few system packages. We will get prompted for the superuser password because restricted system access is needed to install packages through apt.

  1. sudo apt-get install virtualenv python-pip python3-dev

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图4

Enter y to let the system package installation process do its job.

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图5

The packages we need are now installed. We can continue on to install our Python-specific dependencies.

Virtualenv

In the previous section, virtualenv and pip were installed to handle our application dependencies. We can now use them to download and install Flask and Gunicorn.

Create a directory for the virtualenvs. Then create a new virtualenv.

  1. # the tilde "~" specifies the user's home directory, like /home/matt
  2. cd ~
  3. mkdir venvs
  4. # specify the system python3 installation
  5. virtualenv --python=/usr/bin/python3 venvs/flaskproj

Activate the virtualenv.

  1. source ~/venvs/flaskproj/bin/activate

Our prompt will change after we properly activate the virtualenv.

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图6

Our virtualenv is now activated with Python 3. We can install whatever dependencies we want, in our case Flask and Gunicorn.

Flask and Gunicorn

We can finally install Flask and Green Unicorn via the pip command.

  1. pip install flask gunicorn

It's a good sign if we receive no errors like we see in the following screenshot.

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图7

Create a new directory under our home directory that will store our Flask project. Change directory into the new folder.

  1. mkdir ~/flaskproj
  2. cd ~/flaskproj

Create a new file named __init__.py within our flaskproj directory so we can test to make sure Flask is working properly. I prefer to use Vim but Emacs and other development environments work great as well.

Within __init__.py write the following code.

  1. from flask import Flask, Response
  2.  
  3.  
  4. app = Flask(__name__)
  5.  
  6. @app.route("/")
  7. def index():
  8. return Response("It works!"), 200
  9.  
  10. if __name__ == "__main__":
  11. app.run(debug=True)

We could run our app with the Flask development server using the python __init__.py command. Instead run the Flask app with Gunicorn. Go to the directory above the flaskproj folder, in our case we can enter cd ~ then use the gunicorn command:

  1. gunicorn flaskproj:app

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图8

Sweet, we can bring up our shell Flask app in the web browser at the localhost:8000 or 127.0.0.1:8000 address.

How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS - 图9

Now ready for some real Flask development!

Ready for Development

That's a simple setup for developing with Flask and Gunicorn on Ubuntu 16.04. If you need an in-depth step-by-step tutorial to deploy your WSGI-powered web application to a production environment, check out the Full Stack Python Guide to Deployments book.

To determine what to code next for your Python project, read the topics found on the table of contents page.

Questions? Contact me via Twitter @fullstackpython or @mattmakai. I'm also on GitHub with the username mattmakai.

Something wrong with this post? Fork this page's source on GitHub.


#### 在这里注册以便每月能收到一份邮件资料,内容包含本站的主要更新、教程和 Python 书籍的打折码等。

Learn more about these concepts

Flask, Green Unicorn and Ubuntu logos. Copyright their respective owners. Operating Systems Ubuntu WSGI Servers Green Unicorn (Gunicorn) Web Frameworks Flask …or view all topics.


This site is based on Matt Makai's project Full Stack Python, thanks for his excellent work!

此网站由 @haiiiiiyun开源爱好者们 共同维护。 若发现错误或想贡献,请访问: Github fullstackpython.cn 项目