Setting Up TurboGears 2 on Windows
Join the DZone community and get the full member experience.Join For Free
turbogears is one of several web frameworks for python that are available. the most popular by far is django. where i work, we chose turbogears because of its integration with sqlalchemy which supports composite keys. at that time, django did not support that feature and i am not sure if it does yet. anyway, i develop almost exclusively on a windows box and have found the turbogears’ documentation on the subject a little confusing. so here’s how i do it.
note: we’ll be using turbogears 2.1 in this tutorial
we’re going to make sure python is set up right first. open a command window by going to
start –> run
and then typing “cmd” and pressing
try typing “python” (without the quotes) in there and hitting your
enter. if you see the python shell, then we’re halfway there. if you
don’t, then we need to modify some settings.
modifying your path
on windows, i have found that it helps a lot to modify my path
settings by adding my python path and the path to the scripts folder to
my path. here’s how you do it:
right-click “my computer” and choose
xp) or you may have to go digging in control panel on windows 7 and do a
“show all control panel items” from the path bar and look for
. you should see something like this:
- go to the advanced tab and press the environmental variable button to see something like the following:
- you will see two types of variables here. in the bottom section are system variables . scroll down to the one labeled path and at the end of that, add the following two lines: ;c:\python26;c:\python26\scripts (adjust as necessary according to your version of python and its location) note that each entry is separated by a semi-colon, so make sure you do that too or this will not work!
once that’s done, we need to make sure you have setuptools installed. go to the python package index and download it if you know you don’t already have it as setuptools will install a little script known as easy_install that will make getting turbogears setup a breeze. just scroll to the end of the page and pick the exe that corresponds to your python version. you cannot install an egg unless you already have this installed, so i don’t know why they have eggs on there to begin with.
once that’s done, open a new command window and try typing in the following: easy_install. you should receive an error like this: “error: no urls, filenames, or requirements specified (see –help)” if you do, then you’ve got it installed and on your path. good job!
the turbogears people recommend installing virtualenv to help keep your primary python installation (pypi) as pristine as possible. it also helps when you need to experiment with different versions of software that you don’t want to break current projects. how does this work? well, virtualenv creates a little virtual workspace for you on your machine where it puts a copy of python and installs all the software into that virtual place for you to play with. it’s a development sandbox! since you now have easy_install, we’ll use that to install it. in your command window, type: easy_install virtualenv
that will cause easy_install to go out on the internet and try to
download and install virtualenv from pypi. it should look something like
if it completed successfully, then we’re ready to create a virtual environment for our turbogears install. change directories to a location that you’d like to do this in. on windows 7, you’ll probably have security issues if you try to do this outside of your documents or similar collections folders, so you should stick with one of those. for this tutorial, we’ll create it in my documents .
once you’re there, type the following: virtualenv –no-site-packages example
this will create a folder that’s labeled “example” with several folders inside it. you should see the following on your screen:
the –no-site-packages command will prevent the virtual environment from inheriting anything from the system python’s site-packages folder. now change directory into your example folder in the command window and type the following: scripts\activate.bat (see screenshot below)
installing & setting up turbogears
now your virtual environment is active and ready to rock. you can
tell because each line is now prefixed with the name of the folder
you’re in, which in this case is “example”. you can install turbogears
at this point as your virtual environment also has setup tools
installed. however, this install is a little different as we will be
specifying a specific url. type the following (you may want to check the
turbogears website to see if the url has been updated since the writing
of this post):
easy_install.exe -i http://www.turbogears.org/2.1/downloads/current/index tg.devtools
depending on the speed of your pc and your connection, this will take a while. turbogears is a mashup of a large number of 3rd party python packages. you can watch it install or you can go get a drink or make a phone call while you wait. once that’s finished, we’re nearly done.
note: all the packages should be installing to your virtual space’s
folder, not your default python’s. if the latter is happening, you probably don’t have the virtual environment activated
creating a turbogears workspace
one of the packages we installed earlier was python paste. turbogears
uses this package to bootstrap a new turbogears application. so in your
activated virtual environment, type the following command:
paster quickstart example
note: if your path to the “example” folder has a space in it, you will receive an error about needing the pywin32 library installed. very annoying.
when you run this, you will need to answer 3 questions. the first is what to name the project. the default is the same name as the folder. note that the project name has to be in lowercase. then it asks if you’d like to use mako templates . what it doesn’t say is that if you choose no (which is the default), you will get genshi templates instead. that’s up to you. mako looks and feels more “pythonic” from what i’ve seen of it, so i’d recommend it unless you already know genshi or something like genshi. the last question is whether or not you want authentication, which will give you a simple admin interface that you can login to, the default of which is “yes”. you can experiment with these or just take the defaults.
the next step is to change directories into your new project directory and then type the following in at the command line:
python setup.py develop
this adds your project to the virtual environment and also downloads a few packages to install alongside it that might be useful in your development. i don’t really know how these help you, but it seems to be a requirement of sorts according to turbogear’s documentation .
the next to last step is to input the following into your command line window:
paster setup-app development.ini
this will initiate your database, which is basically just creating and populating with some default information for the security / authentication you agreed to. if you didn’t install that stuff or don’t want to run this step, you don’t have to. but you won’t be able to login unless you do.
finally, you need to type the following:
paster serve development.ini
now you will have a fully functioning website that’s running on http://localhost:8080/ (or http://127.0.0.1:8080) . if you load up your browser and navigate to that you should see something like this:
Opinions expressed by DZone contributors are their own.
Observability Architecture: Financial Payments Common Observability Elements
Top 10 Pillars of Zero Trust Networks
Power BI Report by Pulling Data From SQL Tables
Does the OCP Exam Still Make Sense?