Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Full Stack Python: Configuring Python 3, Pyramid and Gunicorn on Ubuntu 16.04

$
0
0

Canonical's Ubuntu 16.04 Long Term Support (LTS) Linux operating system, also known as "Xenial Xerus", was released in April 2016. It is the first Ubuntu release to include Python 3 instead of Python 2 as its default Python installation.

The Pyramid web framework has long supported Python 3. With just a few short steps we can start a new Pyramid project and run it with Green Unicorn (Gunicorn) on Ubuntu 16.04.

Required Tools

Our project requires Ubuntu 16.04 plus several code libraries. You do not need to install these tools yet - we will get to them in turn as the walkthrough progresses. Our requirements and their current versions are:

If you are developing on Mac OS X or Windows, you can use virtualization software such as Parallels or VirtualBox with the Ubuntu .iso file. Either the amd64 or i386 version of 16.04 is fine. While creating this I used the amd64 version.

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

Open a new terminal window so we can be ready to install required system packages.

Install System Packages

The precise Python version can be shown using the python command with the --version argument.

python3 --version

We can also view where the python3 program is installed on Ubuntu using the which command.

which python3

Ubuntu requires a few system packages before we can properly install Pyramid and Gunicorn. When we run the apt command to install system packages we will be prompted for the superuser password. Restricted system access is necessary to modify files within the system folders.

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

Press y then return to let the system package installation run.

The required system packages are installed. We can now install the Python-specific dependencies.

Set up Virtualenv

In the previous section, virtualenv and pip were installed to handle our application dependencies.

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

# the tilde "~" specifies the user's home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/pyramidproj

Activate the virtualenv.

source ~/venvs/pyramidproj/bin/activate

Our prompt will change after we properly activate the virtualenv to something like (pyramidproj) matt@ubuntu:~$.

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

Install Python Packages

We can install Pyramid, Gunicorn and Waitress into our virtualenv using the pip command.

pip install pyramid gunicorn waitress

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

Pyramid comes with a project starter template creation tool named pcreate. Run pcreate to generate the boilerplate for a new Pyramid project named "pyramidproj".

pcreate -s starter pyramidproj

Use the cd (change directory) command to move into the new folder.

cd ~/pyramidproj

A slew of new files have been created within the "pyramidproj" directory. These are the basic files you can customize for the web application you want to build. A good resource for understanding and modifying these files is to follow the quick tutorial for Pyramid.

For now, we just want to use Gunicorn to run our starter pyramidproj app. Install pyramidproj into your virtualenv using the python command on setup.py.

python setup.py develop

Now we can run our app with Gunicorn. Pyramid is a paste-compatible framework, so we use the --paste argument to run the WSGI server with the "development.ini" configuration file. In addition, the -b argument tells Gunicorn which port number to bind on when the server starts.

gunicorn --paste development.ini -b :8080

Cool, we can bring up our starter Pyramid project up in the web browser at the localhost:8000 or 127.0.0.1:8000 address.

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

Ready to Develop with Pyramid

Now you have a simple setup to develop Pyramid 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.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>