Full Stack Python logo Full Stack Python

全部主题 | Blog | 时讯 | @fullstackpython | Facebook | 源码 # Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS Posted by Matt Makai on 五月 13, 2016. Last updated 八月 10, 2016.

The Ubuntu 16.04 Long Term Support (LTS) Linux operating system was released in April 2016. This latest Ubuntu release is named "Xenial Xerus" and it is the first Ubuntu release to include Python 3, instead of Python 2.x, as the default Python installation.

We can quickly start a new Bottle web application project and run it with Green Unicorn (Gunicorn) on Ubuntu 16.04.

Tools We Need

Our setup requires the Ubuntu 16.04 release along with a few other code libraries. Don't install these tools just yet since we'll get to them as we go through the walkthrough. Our requirements and their current versions as of May 13, 2016 are:

If you are developing on Mac OS X or Windows, make sure to 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 use the amd64 version for my own local development.

A desktop screen like this one appears when you boot up Ubuntu.

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS - 图2

Open a terminal window to install the system packages.

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

Configuring Python 3, Bottle and Gunicorn for Development 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

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS - 图4

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

Configuring Python 3, Bottle and Gunicorn for Development 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 Bottle 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/bottleproj

Activate the virtualenv.

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

Our prompt will change after we properly activate the virtualenv.

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS - 图6

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

Bottle and Gunicorn

We can now install Bottle and Green Unicorn via the pip command.

  1. pip install bottle gunicorn

No errors like we see in the following screenshot is a good sign.

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS - 图7

Use the mkdir command to create a new directory to keep our Bottle project then use the cd (change directory) command to move into the new folder.

  1. mkdir ~/bottleproj
  2. cd ~/bottleproj

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

Within the new app.py file write the following code.

  1. import bottle
  2. from bottle import route, run, Response
  3.  
  4. # a basic URL route to test whether Bottle is responding properly
  5. @route('/')
  6. def index():
  7. return Response("It works!")
  8.  
  9. # these two lines are only used for python app.py
  10. if __name__ == '__main__':
  11. run(host='0.0.0.0', port=8000, debug=True, reloader=True)
  12.  
  13. # this is the hook for Gunicorn to run Bottle
  14. app = bottle.default_app()

We could run our app with the Bottle development server using the python app.py command. Let's instead run our Bottle app with Gunicorn.

  1. gunicorn -w 2 app:app

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS - 图8

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

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS - 图9

Time to develop a full-fledged web application with Bottle!

Ready for Development

Now you have a simple setup to develop Bottle web apps using Gunicorn as the WSGI server on Ubuntu 16.04. If you need a full step-by-step tutorial to deploy your Python web application to a production environment, check out the Full Stack Python Guide to Deployments book.

To decide what to do next with your Python project, check out the Full Stack Python table of contents page.

See something wrong in this post? Fork this page's source on GitHub and submit a pull request.


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

Learn more about these concepts

Bottle, Green Unicorn and Ubuntu logos. Copyright their respective owners. Operating Systems Ubuntu WSGI Servers Green Unicorn (Gunicorn) Web Frameworks Bottle …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 项目