Last modified: Jun 10, 2026
Install Pony ORM in Python
Pony ORM is a Python library that makes database work easy. It uses a simple syntax to write queries. You can use it with SQLite, PostgreSQL, MySQL, and Oracle. This guide will show you how to install it step by step.
What is Pony ORM?
Pony ORM is an object-relational mapper. It lets you work with databases using Python objects. You do not need to write raw SQL. The library is fast and easy to learn. It works well for small and large projects.
Prerequisites
Before you install Pony ORM, you need Python 3.6 or newer. You can check your Python version with this command:
python --version
Make sure you have pip installed. Pip is the Python package manager. You can check it with:
pip --version
If you do not have pip, install it first. On most systems, you can use the package manager. On Ubuntu, run sudo apt install python3-pip.
Step 1: Create a Virtual Environment
It is a good practice to use a virtual environment. This keeps your project dependencies separate. Use the venv module to create one.
python -m venv myenv
Activate the virtual environment. On Windows, run:
myenv\Scripts\activate
On macOS or Linux, run:
source myenv/bin/activate
You will see the environment name in your terminal prompt. This means it is active.
Step 2: Install Pony ORM with Pip
Now you can install Pony ORM. Use the pip install command.
pip install pony
This will download and install the library. It also installs any dependencies. The process takes a few seconds. You should see a success message at the end.
To install a specific version, use pip install pony==0.7.16. Replace the version number with the one you need. Check the official Pypi page for the latest version.
Step 3: Verify the Installation
Open a Python interpreter. Import the library to check if it works.
import pony
print(pony.__version__)
You should see the version number. For example:
0.7.16
If you see an ImportError, something went wrong. Try reinstalling with pip install --upgrade pony.
Step 4: Create a Simple Database
Let's test Pony ORM with a small example. We will create an SQLite database. Create a new file called test_pony.py.
from pony.orm import Database, Required, set_sql_debug
# Create a database object for SQLite
db = Database()
# Define a simple entity (table)
class Person(db.Entity):
name = Required(str)
age = Required(int)
# Bind to an in-memory SQLite database
db.bind(provider='sqlite', filename=':memory:')
# Generate the tables
db.generate_mapping(create_tables=True)
# Turn on SQL debug to see queries
set_sql_debug(True)
# Insert a record
with db.session:
Person(name='Alice', age=30)
# Query all persons
with db.session:
people = Person.select()
for p in people:
print(p.name, p.age)
Run the script:
python test_pony.py
You should see output like this:
GET CONNECTION FROM THE LOCAL POOL
INSERT INTO "Person" ("name", "age") VALUES (?, ?)
('Alice', 30)
This shows Pony ORM is working. It created the table and inserted a row.
Step 5: Install Optional Database Drivers
Pony ORM supports multiple databases. For SQLite, no extra driver is needed. For PostgreSQL, install psycopg2:
pip install psycopg2-binary
For MySQL, install pymysql:
pip install pymysql
For Oracle, you need cx_Oracle. Check the official documentation for setup steps.
Common Installation Issues
If you get a PermissionError, you are not using a virtual environment. Create one and activate it. Then install again.
If you see a ModuleNotFoundError, the library is not installed. Check your pip list with pip list. If Pony is missing, run pip install pony again.
If you have multiple Python versions, use the correct pip. For example, python3 -m pip install pony on Linux.
Using Pony ORM in Your Project
Once installed, you can start using Pony ORM in your code. It is especially useful for web apps and data analysis. If you are new to ORMs, start with simple queries. The select() function is your main tool for reading data.
For more advanced usage, check the official documentation. It covers relations, migrations, and performance tips.
Conclusion
Installing Pony ORM in Python is simple. You need a virtual environment, pip, and one command. Then you can create databases with minimal code. The library is beginner-friendly and powerful. Start with the example above and build from there. Happy coding!