Installing
To install Pony, type the following command into the command prompt:
pip install pony
Pony can be installed on Python 2.7 or Python 3. If you are going to work with SQLite database, you don’t need to install anything else. If you wish to use another database, you need to have the access to the database and have the corresponding database driver installed:
PostgreSQL: psycopg2 or psycopg2cffi
MySQL: MySQL-python or PyMySQL
Oracle: cx_Oracle
CockroachDB: psycopg2 or psycopg2cffi
To make sure Pony has been successfully installed, launch a Python interpreter in interactive mode and type:
>>> from pony.orm import *
This imports the entire (and not very large) set of classes and functions necessary for working with Pony. Eventually you can choose what to import, but we recommend using import *
at first.
If you don’t want to import everything into global namespace, you can import the orm
package only:
>>> from pony import orm
In this case you don’t load all Pony’s functions into the global namespace, but it will require you to use orm
as a prefix to any Pony’s function and decorator.
The best way to become familiar with Pony is to play around with it in interactive mode. Let’s create a sample database containing the entity class Person
, add three objects to it, and write a query.