Pony ORM is a Python programming language directory that enables people to work comfortably with objects kept as tuples in a relational database system. It enables you to deal with the information of the databank, in the form of substances/objects. In the database, there are tables having tuples of data. Conversely, when it is possible to view the data obtained from the databank in object form, it is far more useful when writing the code in an advanced-level object-oriented semantic. If you wish to work with Pony ORM, you have to go through the below-appended steps thoroughly.

Install Python

You need to install the newest version of Python on your system to get started with Pony ORM. Visit the official website of Python and Download it quickly by pressing the yellow button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_1.jpg" data-lazy- height="169" src="data:image/svg xml,” width=”737″>

Run the downloaded file as an Administrator from the folder and the following window will pop up. Checkmark the checkboxes and click on the blue highlighted Install Now text to continue the installation of Python.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_2.jpg" data-lazy- height="359" src="data:image/svg xml,” width=”666″>

Python will be installed quickly. Close the setup window to proceed further.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_3.jpg" data-lazy- height="349" src="data:image/svg xml,” width=”662″>

Open the command prompt from the search bar and run the below-given command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_4.jpg" data-lazy- height="107" src="data:image/svg xml,” width=”753″>

Try to install the Pony directory using the pip command in the command prompt as:

If you find an error showing that the pip package is not recognized, it means that the environment variables have not been set for the pip package.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_5.jpg" data-lazy- height="62" src="data:image/svg xml,” width=”672″>

Set Environment Variables

In the search bar, type Python. A list of Python-related apps and files will pop up. Right Click on the main Python application and select Open File Location. You will be navigated to the folder where your newly installed Python has been located, as shown below. You can double click on it to check the Python installed version on your system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_6.jpg" data-lazy- height="157" src="data:image/svg xml,” width=”756″>

Select the Python 3.9 main file, right-click on it, and pick Open File Location. The following new folder will be opened. Click on the path, select and copy the full path, as shown at the top of the image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_7-1.jpg" data-lazy- height="412" src="data:image/svg xml,” width=”752″>

Go to your Windows Search bar at the below right corner of the desktop, and type Edit the System Environment Variables. Click on the utility to open it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_8.jpg" data-lazy- height="45" src="data:image/svg xml,” width=”592″>

The below window named System Properties will be shown. Click on the Environment Variable button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_9.jpg" data-lazy- height="468" src="data:image/svg xml,” width=”413″>

Below the ‘User variables’ tab, select Path, and tap the Edit button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_10.jpg" data-lazy- height="269" src="data:image/svg xml,” width=”616″>

A new window will pop. Tap the New button, paste the copied path, and click on the OK button to continue.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_11.jpg" data-lazy- height="497" src="data:image/svg xml,” width=”524″>

Below the System Variables tab, select Path and tap the Edit button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_12.jpg" data-lazy- height="276" src="data:image/svg xml,” width=”616″>

Tap the New button, paste the copied path, and tap the Ok button to continue.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_13.jpg" data-lazy- height="491" src="data:image/svg xml,” width=”514″>

Now when you run the python command in the command prompt, you will see the newest version of python and the environment variables you have set for Python successfully.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_14.jpg" data-lazy- height="147" src="data:image/svg xml,” width=”804″>

If you want to set variable paths for pip and pony as well, you will have to copy the location of both and paste it into the path of the ‘User Variable’ tab. Now you can see pip package is working successfully in the command prompt.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_15.jpg" data-lazy- height="163" src="data:image/svg xml,” width=”650″>

Install Pony ORM

In the command prompt, try the following pip command to install Pony on your system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_16.jpg" data-lazy- height="198" src="data:image/svg xml,” width=”813″>

Make sure to upgrade pip to install all the packages in the Pony directory. Use this command to upgrade:

>> pip install –upgrade pip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_17.jpg" data-lazy- height="157" src="data:image/svg xml,” width=”733″>

Again, trying the installation command for Pony:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_18.jpg" data-lazy- height="199" src="data:image/svg xml,” width=”953″>

Now, you have to install some extra packages to work on Pony. First of all, try to install any database you need to work along with Pony. You don’t have to configure any other database when you are working with the SQLite database. If you want to try a different database, you should install the matching database driver from the official Pony website. I have installed MySQL using the MySQL driver. Here is the command to install the database:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_19.jpg" data-lazy- height="168" src="data:image/svg xml,” width=”764″>

Import ORM package

Open your Python interpreter to start with Pony ORM. We are using the Visual Studio Code App. Import the ORM package using the Pony directory using any of the statements below:

>> from pony.orm import *

>> from pony import orm

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_20.jpg" data-lazy- height="40" src="data:image/svg xml,” width=”630″>

Create Database Object

Entities are added to the database of Pony. That’s why we have to first construct a database object. In the editor for Python, type:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_21.jpg" data-lazy- height="22" src="data:image/svg xml,” width=”603″>

Define Entities

For our convenience, we have to build two classes. There are two traits of the class Person: name and age, and Car have parameters: make and model, as shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_22.jpg" data-lazy- height="171" src="data:image/svg xml,” width=”617″>

Binding of Database

If you want to bind the stated entities with your database, you have to use the below bind function in your code. In the provider attribute, you can provide any database which you are using. If an in-memory database is established, replace database.sqlite with ‘:memory:’

For SQLite use:

>> db.bind(provided=’dbname’, filename=’database.sqlite’, create_db=True)

For MySQL, PostgreSQL, Oracle, and CockroachDB use:

>> db.bind(provided=’dbname’, user=’ ’, password=’ ’, host=’ ‘, database= ‘ ‘)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_23.jpg" data-lazy- height="26" src="data:image/svg xml,” width=”619″>

Map Entities to Database Tables

We have to invoke the generate mapping() function using the db object. The argument create_tables=True means that if the table doesn’t exist yet, then the CREATE TABLE instruction can be used to create them.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_24.jpg" data-lazy- height="21" src="data:image/svg xml,” width=”603″>

Turn on the Debug Mode

For this purpose, type the below statement:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_25.jpg" data-lazy- height="22" src="data:image/svg xml,” width=”607″>

Create Entity Objects

We have to create objects for both classes while passing values to arguments and save the changes using the commit() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_26.jpg" data-lazy- height="117" src="data:image/svg xml,” width=”612″>

DB Session

Database communication can be performed inside a database session using Pony within the application. The methods that interact long the database must be packed with the db_session() decorator.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_27.jpg" data-lazy- height="156" src="data:image/svg xml,” width=”606″>

You can use db_session() as context manager rather than the decorator.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_28.jpg" data-lazy- height="60" src="data:image/svg xml,” width=”612″>

Write Queries

Try some queries to get information. You can use the slice-operator [:] and for-loop in your query. Try any one of the commands in interactive mode.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_29.jpg" data-lazy- height="77" src="data:image/svg xml,” width=”619″>

Get Objects

You can get objects using the primary indexes.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/What-is-Pony-ORM-and-How-to-Get-Started_30.jpg" data-lazy- height="46" src="data:image/svg xml,” width=”602″>

Conclusion

Hopefully, you have successfully catered to all the problems while using the Pony ORM using this guide.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Author-Image-150×150.jpg600a3003cad03.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.