Please oh please let me never program in PHP again.
Interested in Python as a web development language or in Pyramid as a web framework? I've recently been introduced to both and given the large difference between
Python I thought writing a quick guide for how to setup a basic site with Pyramid using Python could be helpful!
Virtualenv, development, deployment, and where do I start?
When developing with Python one thing to throw out the window is the concept of a few files that are handled by a server. While this is possible there is a bigger difference in code vs. response with Python so it helps to knock this on the head to begin with.
Basic Python projects come in the form of Python Packages. This is just a structure of related code + files that build 'something'. A package, a set of modules, some related bits and pieces. Every non-trivial Python website (or application) will be a mess of different packages (both part of Python's standard library and third party) which allow you to quickly code and develop lots of awesome apps. Some popular examples would be
PIL (the Python Imaging Library, not unlike
gd_library if you're coming from PHP).
A Python package can seem pretty complicated at first (setuptools, distils, bleh) but it's basically a directory with some code + a few extra files (readme, links to related packages, etc.) and basically allows you to build your own code and link it to other packages without actually having to keep them together. It allows other users to quickly build and run your code, update the latest dependent packages (e.g. PIL pr Pyramid) and it have no direct effect on your code.
To get started you likely already have ’easy_install’. You can quickly test this by running the command.
If it's not there then install that first…
apt-get easy_install or the hard way like this.
Once that's in, let's quickly replace it by with
pip a newer and nicer package management installer which basically does the same thing.
Now never use
easy_install again. Always use
pip. Same thing! But more better! Argue somewhere else.
pip install virtualenv
That's all you need to do to install
virtualenv. This will install it globally on your system. Probably to
/usr/local/bin or somewhere like that. Now that you have it we can create a virtual environment. What is that? It's basically a place on your machine where you can install any Python packages you want, without worrying about any other packages that were installed by other people, apps, or programs. This makes it really easy to mess around and not worry so much on conflicts or dependencies. It's a simple process.
- Create a virtual environment using virtualenv
- Activate the virtual environment you've created before you start work
There are extra tools you can get to increase your efficiency here, but for now we'll go with the basics. Create. Activate.
Starting with Pyramid
Firstly you'll need to setup your Python working environment. I'd recommend using virtualenv as so:
virtualenv ~/dev/virtualenv/shelves . ~/dev/virtualenv/shelves/bin/activate
Now we have a new virtual environment for our code + we have activated it. Now lets install Pyramid using pip which will quickly let us start with some scaffolding.
pip install pyramid
Once you've done that you have pyramid installed into your virtual environment. You can test to make sure this worked a few ways.. let's try a quick Pyramid paster call to see what's available.
That will list all available scaffolding scripts to you at this point. You will likely see:
Available scaffolds: alchemy: Pyramid SQLAlchemy project using url dispatch starter: Pyramid starter project zodb: Pyramid ZODB project using traversal
This is a list of all available scaffolds that pcreate (the Pyramid version of paster) has on offer. To start with a basic Pyramid based python package we'll use pcreate like so:
pcreate -t started shelves
This will show a bunch of commands in your terminal which basically means: We have created a bunch of files with code for you! Sorry for the convenience as they will remind you at the end.
If you want to double check your using the correct virtual environment you can always type:
This will tell you exactly where the binary file being run is. You should see something like
~/dev/virtualenv/shelves/bin/pcreate which is the binary location of the current virtual environment. This depends on where you set it up and a few other things, but should be pretty clear if you know your way around a UNIX machine. If not, don't stress. It's not overly important and will become clear as you mess up and have to do this over and over again. Trust me, we've all been there!
First thing we need to do is install all dependencies for Pyramid that didn't come down. This is really simple and provided for you in the setup.py script included in the scaffold.
python setup.py develop
Extra credit: the setup.py using distutils, a framework for creating Python packages to quickly load dependencies. you can read a lot more about it but for the mean time: it installs everything it thinks it needs to run your code. You will edit this file if you add new dependencies to your app, but it's easy!
Now we're good to start, you can easily run your web app and be happy with with yourself. Job well done. Tutorial over. You can quickly run the basic Pyramid app now:
cd shelves serve development.ini
This will launch your website using WSGI (a simple Python web server, one which you often use during development and which hooks into pserve for really really amazingly awesome deployment)
Starting server in PID 898. serving on http://0.0.0.0:6543
Oooo. Our new website is on your local machine (it uses
0.0.0.0 by default to represent all network hardware for this machine, as opposed to
127.0.0.1 which you might be used to). You can throw that URL into your browser or something like
mac.local:6543. This depends on your setup, so I won't waste any time here. Voila!
If you want to learn how to program something yourself, like a view or a model then you can move on! If not. Au revoir.