---
title: "How to Connect a Database to Python"
description: "It is helpful to understand how to connect a database to Python scripts for serving dynamically generated web pages and collaborative reports. Python is almost always included in Linux distributions..."
url: https://www.inmotionhosting.com/support/website/how-to-use-python-to-connect-to-a-database/
date: 2011-10-13
modified: 2022-02-15
author: "InMotion Hosting Contributor"
categories: ["Website", "Working with Databases"]
type: post
lang: en
---

# How to Connect a Database to Python

![How to Connect a Database to Python](https://www.inmotionhosting.com/support/wp-content/uploads/2022/02/python-mysql-database-connetion-1024x538.jpg)

It is helpful to understand how to connect a database to Python scripts for serving dynamically generated web pages and collaborative reports. Python is almost always included in Linux distributions and used for multiple applications already. You don’t need PHP for this.

Below we’ll cover how to create a Python database connection (MySQL/MariaDB) in the Linux terminal.

## How to Connect a Database to Python 2.7

1. [Log into SSH](https://www.inmotionhosting.com/support/server/ssh/how-to-login-ssh/).
2. From your website root directory, create a Python script file in the “cgi-bin” directory: touch cgi-bin/test-db.py
3. Change the file’s permissions to 755:chmod 755 cgi-bin/test-db.py
4. If you wish to execute Python scripts in web browsers, edit your Apache .htaccess file:nano .htaccess
5. Add the following at the top of the file and save changes: AddHandler cgi-script .py
6. To complete the Python database connection you’ll need to know the database host (“localhost” if on the same system), name, username, and user password.
7. Run Python:python
8. Ensure you have the MySQL Python module installed:import MySQLdb If you receive no notification, that means it is installed. You’ll need to install the module if you receive the error “ImportError: No module named mysqldb.”
9. Exit Python:exit ()
10. If you need to install it, we recommend using your OS repositories. You can also use PIP.Alma / Enterprise Linux: sudo yum install MySQL-pythonUbuntu:sudo apt-get install python-pip python-dev libmysqlclient-devPIP:pip install MySQL-python
11. Edit your Python script:nano cgi-bin/test-db.py
12. Insert the code below to connect to the database and run “SELECT VERSION(),” which shows our current version of MySQL. Replace the database user, password, and database.#!/usr/bin/env pythonimport MySQLdb# connect to the databasedb = MySQLdb.connect("localhost","user","password","database" )# setup a cursor object using cursor() methodcursor = db.cursor()# run an sql questioncursor.execute("SELECT VERSION()")# grab one resultdata = cursor.fetchone()# begin printing data to the screenprint "Content-Type: text/html"printprint """<!DOCTYPE html><html><head><title>Python - Hello World</title></head><body>"""print "Database version : %s " % dataprint"""</body></html>"""# close the mysql database connectiondb.close()
13. Save changes.
14. Run the Python script:python test-db.py The results should show basic HTML markup and your current database version.

You can also visit the Python script URL in the web browser if you updated your web server configuration file. You’ll see the database version line.

Congrats on learning how to connect a database to Python 2.7+. Learn more about [programming with Python](https://www.inmotionhosting.com/support/website/website-design/python-programming/).

If you don’t need cPanel, don’t pay for it. Only pay for what you need with our scalable [Cloud VPS Hosting](https://www.inmotionhosting.com/cloud-vps).

![check mark](https://design.inmotionhosting.com/assets/icons/standard/check-blue.svg)CentOS, Debian, or Ubuntu ![check mark](https://design.inmotionhosting.com/assets/icons/standard/check-blue.svg)No Bloatware ![check mark](https://design.inmotionhosting.com/assets/icons/standard/check-blue.svg)SSH and Root Access
