A quick start for python pyramid
Installation of pyramid
1.Install python 2.7/3.x
For windows, please add C:\Python27\Scripts to PATH variable.
For ubuntu: apt-get install python-setuptools
On windows, download virtualenv-1.7.tar.gz, decompress, and run : python setup.py install
create a virtual working directory mysite:
source bin/activate (Scripts\activate.bat)
Then variable $VIRTUAL_ENV will be set; and virtualenv modified $PATH, appended bin/python,
also virtualenv installed pip at ENV/bin; so new python packages would be installed here, such as: pip install html5lib.
clear virtual env:
virtualenv –clear mysite
4. Install Pyramid and Preparation
Preparation, on unix/linux:
After you’ve got your env virtualenv installed, you may install Pyramid itself using the following commands from within the virtualenv (env) directory:
$ bin/easy_install pyramid
$bin/easy_install docutils nose coverage zope.sqlalchemy SQLAlchemy pyramid_tm waitress
Preparation, on Windows
Use your Python’s virtualenv to make a workspace:
c:\> c:\Python26\Scripts\virtualenv mysite
Switch to the pyramidtut directory:
c:\> cd mysite
Use easy_install to get Pyramid and its direct dependencies installed:
c:\mysite> Scripts\easy_install pyramid
Use easy_install to install various packages from PyPI.
c:\mysite> Scripts\easy_install docutils nose coverage zope.sqlalchemy SQLAlchemy pyramid_tm waitress
5. How to create project aimed at deployment
5.1. Making a Project
Your next step is to create a project. Pyramid supplies a variety of scaffolds to generate sample projects. We will use the pyramid_routesalchemy scaffold, which generates an application that uses SQLAlchemy and URL dispatch.
The below instructions assume your current working directory is the “virtualenv” named “pyramidtut”.
On UNIX, for pyramid 1.2:
$ bin/paster create -t pyramid_routesalchemy tutorial
On Windows, for pyramid 1.2:
c:\pyramidtut> Scripts\paster create -t pyramid_routesalchemy tutorial
On unix, for pyramid 1.3:
$ bin/pcreate –list-templates
$ bin/pcreate -s starter tutorial
On Windows, for pyramid 1.3:
scripts\pcreate -s starter tutorial
If you are using Windows, the pyramid_routesalchemy scaffold may not deal gracefully with installation into a location that contains spaces in the path. If you experience startup problems, try putting both the virtualenv and the project into directories that do not contain spaces in their paths.
5.2. Installing the Project in “Development Mode”
In order to do development on the project easily, you must “register” the project as a development egg in your workspace using the setup.py develop command. In order to do so, cd to the “tutorial” directory you created in Making a Project, and run the “setup.py develop” command using virtualenv Python interpreter.
$ cd tutorial
$ ../bin/python setup.py develop
c:\pyramidtut> cd tutorial
c:\pyramidtut\tutorial> ..\Scripts\python setup.py develop
5.3 Running the Tests
After you’ve installed the project in development mode, you may run the tests for the project.
$ ../bin/python setup.py test -q
c:\pyramidtut\tutorial> ..\Scripts\python setup.py test -q
5.4. install new project:
$ cd MyProject
$ ..\Scripts\python.exe setup.py develop
This will install adistributionrepresenting your project into the interpreter’s library set so it can be found by import statements and by other console scripts such aspserve, pshell, proutes and pviews
5.5. Starting the Application
Start the application.
$ ../bin/pserve development.ini
$ ../bin/pserve development.ini –reload
When –reload is passed topserve, changes to any Python module your project uses will cause the server to restart. This
typically makes development easier, as changes to Python code made within a Pyramid application is not
put into effect until the server restarts.
c:\pyramidtut\tutorial> ..\Scripts\pserve development.ini
5.6. Visit the Application in a Browser
In a browser, visit http://localhost:6543/. You will see the generated application’s default page.
One thing you’ll notice is the “debug toolbar” icon on right hand side of the page. You can read more about the purpose of the icon at The Debug Toolbar. It allows you to get information about your application while you develop.
5.7.Exposing Test Coverage Information
You can run the nosetests command to see test coverage information. This runs the tests in the same way that setup.py test does but provides additional “coverage” information, exposing which lines of your project are “covered” (or not covered) by the tests.
To get this functionality working, we’ll need to install a couple of other packages into our virtualenv: nose and coverage:
$ ../bin/easy_install nose coverage
c:\pyramidtut\tutorial> ..\Scripts\easy_install nose coverage
Once nose and coverage are installed, we can actually run the coverage tests.
$ ../bin/nosetests –cover-package=tutorial –cover-erase –with-coverage
c:\pyramidtut\tutorial> ..\Scripts\nosetests –cover-package=tutorial ^
Looks like our package’s models module doesn’t quite have 100% test coverage.