Full Stack Python logo Full Stack Python

全部主题 | Blog | 时讯 | @fullstackpython | Facebook | 源码 # Setting Up Python 3, Django & Gunicorn on Linux Mint 17.3 Posted by Matt Makai on 六月 26, 2016. Last updated 七月 22, 2016.

Linux Mint 17.3 "Rosa" is December 2015 release of the polished and widely-used Linux distribution. This Mint release includes both Python 2.7 and 3.4 by default, but in this tutorial we will download and install the latest Python 3.5.1 version to run our Django application.

If you want to use a different Linux distribution such as Ubuntu instead of Mint, check out the tutorial for Ubuntu 16.04 "Xenial Xerus". If Mint is your desired development environment though, let's get started!

Tools We Need

Our setup will use several system packages and code libraries to get up and running. Do not worry about installing these dependencies just yet, we will get to them as we progress through the tutorial. The tools and their current versions as of June 2016 are:

If you are on Mac OS X or Windows, my recommendation is to use virtualization software such as Parallels or VirtualBox with the Linux Mint Cinnamon desktop .iso.

We should see a desktop screen like this one when we boot up the operating system for the first time.

Linux Mint default desktop

Open up terminal to proceed with the configuration.

System Packages

We can see the Python version Linux Mint comes with, as well as where its executable is stored.

  1. python3 --version
  2. which python3

The output of those two commands should be (these are not commands to run):

  1. Python 3.4.3
  2. /usr/bin/python3

Output of 'python --version' and 'which python3' commands.

We really want to use the latest Python release instead of the default 3.4 when starting a new Python project, so let's download and install 3.5.1 now.

Run these commands in the terminal to download Python 3.5.1 source code:

  1. cd ~/Downloads
  2. wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

wget Python source code output.

Extract the Python source code:

  1. tar -xvf Python-3.5.1.tgz

Linux Mint is not configured by default to build the Python source code. We need to update our system package lists and install several packages to make building the Python source code possible. If you have a password on your user account, enter it when prompted to allow the installation to proceed.

  1. sudo apt update
  2. sudo apt install build-essential checkinstall
  3. sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev
  4. sudo apt install libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
  5. sudo apt install python3-dev

Once the packages are installed, we can configure and install Python from source.

  1. cd Python-3.5.1
  2. ./configure
  3. sudo make install

Test that the installation worked properly by starting up the Python REPL:

  1. python3.5

If the REPL starts up properly with Python 3.5.1 in the output then we're good to go.

wget Python source code output.

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

Virtual environment and pip

Python 3.5 comes with the virtual environment and pip applications so we can use them to handle our application dependencies.

Create a directory to store virtual environments then create a virtualenv for our Django project.

  1. # the tilde "~" specifies the user's home directory, like /home/matt
  2. cd ~
  3. mkdir venvs
  4. # specify the system python3 installation
  5. python3.5 -m venv djangoproj

Activate the virtualenv.

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

Our virtual environment is properly activated when we see (djangoproj) prepended to our prompt.

Output from the virtualenv environment activation.

Our virtualenv with Python 3.5.1 is activated so we can install whatever dependencies we want, such as Django and Gunicorn. Our default python command is also set to use the Python 3.5.1 installation instead of the Python 2.7 version that comes with Linux Mint.

Django and Gunicorn

Now we can install Django and Green Unicorn into our virtual environment.

  1. pip install django==1.9.7 gunicorn==19.6

If there are no errors in the pip output then that is a good sign we can proceed.

Django and Gunicorn properly install via the pip command.

Create a new Django project named djangoproj, or whatever you want to name your project. Change into the directory for the new project.

  1. cd ~
  2. django-admin startproject djangoproj
  3. cd djangoproj

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

  1. gunicorn djangoproj.wsgi

Result of running gunicorn djangoproj.wsgi on the command line.

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

Django project running in the Firefox web browser.

Now you're ready for Django development!

Ready for Development

Those are the first few steps for beginning development with Django and Gunicorn on Linux Mint 17.3 "Rosa". If you need an even more in-depth walkthrough for deploying 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 Linux Mint logos. Copyright their respective owners. Operating Systems 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 项目