Full Stack Python logo Full Stack Python

全部主题 | Blog | 时讯 | @fullstackpython | Facebook | 源码 # Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS Posted by Matt Makai on 五月 09, 2016. Last updated 八月 10, 2016.

Ubuntu released the newest Long Term Support (LTS) version of its operating system in April 2016. The update brings Ubuntu to version 16.04 and its latest code name is "Xenial Xerus". 16.04 is the first Ubuntu release to include Python 3 as the default Python installation.

Let's use this newest Ubuntu release along with Python version 3.5 to start a new Django web application project and run it with Green Unicorn (Gunicorn).

Tools We Need

We will need a few tools to complete our project. Don't worry about installing these just yet as we'll get to them as we progress through the tutorial. The tools and their current versions as of May 9, 2016 are:

If you are running Mac OS X or Windows, use virtualization software such as Parallels (this is what I use, but it's Mac OS X-only) or VirtualBox with the Ubuntu .iso file. Either the amd64 or i386 version of 16.04 is fine, but I use amd64 for development and testing in this blog post.

When we boot up for the first time, we should see a desktop screen like this one.

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图2

Open up terminal to proceed with the setup.

System Packages

We can see the python3 version Ubuntu comes with, as well as where its executable is stored.

  1. python3 --version
  2. which python3

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图3

Our Ubuntu installation first needs system packages for Python development. You'll be prompted for your superuser password because restricted system access is required to install packages through apt.

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

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图4

Enter y and let the system package installation process run.

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图5

The basic system packages we need are now installed so we can proceed to our Python-specific dependencies.

Virtualenv

Virtualenv and pip for isolating and handling application dependencies were just installed via system packages so we can now use them to obtain Django and Gunicorn.

Create a directory to store virtualenvs then put a new virtualenv in it.

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

Activate the virtualenv.

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

We should see our prompt change so that we know the virtualenv is properly activated.

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图6

Our virtualenv with Python 3 is activated so we can install whatever dependencies we want, such as Django and Gunicorn.

Django and Gunicorn

Time to install Django and Green Unicorn into our virtualenv.

  1. pip install django gunicorn

No errors is a good sign everything worked for us.

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图7

Create a new Django project named djproject, or whatever you want to name your project. Then change into the directory for the new project.

  1. django-admin startproject djproject
  2. cd djproject

We could run Django with the development server using the python manage.py runserver command. However, start Django up with Gunicorn instead.

  1. gunicorn djproject.wsgi

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图8

Awesome, now we can bring up our shell project in the web browser at the localhost:8000 or 127.0.0.1:8000 address.

Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS - 图9

Ready for development!

Ready for Development

Those are the basics for starting development with Django and Gunicorn on Ubuntu 16.04. If you need an even more in-depth 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 figure out what to do 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.

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


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

Learn more about these concepts

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