- Python & MySQL - Discussion
- Python & MySQL - Useful Resources
- Python & MySQL - Quick Guide
- Python & MySQL - Handling Errors
- Python & MySQL - Performing Transactions
- Python & MySQL - Using Joins
- Python & MySQL - Sorting Data
- Python & MySQL - Like Clause
- Python & MySQL - Where Clause
- Python & MySQL - Delete Records
- Python & MySQL - Update Records
- Python & MySQL - Select Records
- Python & MySQL - Insert Records
- Python & MySQL - Drop Tables
- Python & MySQL - Create Tables
- Python & MySQL - Select Database
- Python & MySQL - Drop Database
- Python & MySQL - Create Database
- Python & MySQL - Connect Database
- Python & MySQL - Environment Setup
- Python & MySQL - Overview
- Python & MySQL - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python & MySQL - Environment Setup
Python is available on a wide variety of platforms including Linux and Mac OS X. Let s understand how to set up our Python environment.
Local Environment Setup
Open a terminal window and type "python" to find out if it is already installed and which version is installed.
Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
Win 9x/NT/2000
Macintosh (Intel, PPC, 68K)
OS/2
DOS (multiple versions)
PalmOS
Nokia mobile phones
Windows CE
Acorn/RISC OS
BeOS
Amiga
VMS/OpenVMS
QNX
VxWorks
Psion
Python has also been ported to the Java and .NET virtual machines
Getting Python
The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official website of Python
You can download Python documentation from
. The documentation is available in HTML, PDF, and PostScript formats.Instalpng Python
Python distribution is available for a wide variety of platforms. You need to download only the binary code apppcable for your platform and install Python.
If the binary code for your platform is not available, you need a C compiler to compile the source code manually. Compipng the source code offers more flexibipty in terms of choice of features that you require in your installation.
Here is a quick overview of instalpng Python on various platforms −
Unix and Linux Installation
Here are the simple steps to install Python on Unix/Linux machine.
Open a Web browser and go to
.Follow the pnk to download zipped source code available for Unix/Linux.
Download and extract files.
Editing the Modules/Setup file if you want to customize some options.
run ./configure script
make
make install
This installs Python at standard location /usr/local/bin and its pbraries at /usr/local/pb/pythonXX where XX is the version of Python.
Windows Installation
Here are the steps to install Python on Windows machine.
Open a Web browser and go to
.Follow the pnk for the Windows installer python-XYZ.msi file where XYZ is the version you need to install.
To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.
Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done.
Macintosh Installation
Recent Macs come with Python installed, but it may be several years out of date. See
for instructions on getting the current version along with extra tools to support development on the Mac. For older Mac OS s before Mac OS X 10.3 (released in 2003), MacPython is available.Jack Jansen maintains it and you can have full access to the entire documentation at his website −
. You can find complete installation details for Mac OS installation.Setting up PATH
Programs and other executable files can be in many directories, so operating systems provide a search path that psts the directories that the OS searches for executables.
The path is stored in an environment variable, which is a named string maintained by the operating system. This variable contains information available to the command shell and other programs.
The path variable is named as PATH in Unix or Path in Windows (Unix is case sensitive; Windows is not).
In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you must add the Python directory to your path.
Setting path at Unix/Linux
To add the Python directory to the path for a particular session in Unix −
In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter.
In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python" and press Enter.
Note − /usr/local/bin/python is the path of the Python directory
Setting path at Windows
To add the Python directory to the path for a particular session in Windows −
At the command prompt − type path %path%;C:Python and press Enter.
Note − C:Python is the path of the Python directory
Python Environment Variables
Here are important environment variables, which can be recognized by Python −
Install MySQL Database
The most important thing you will need, of course is an actual running database with a table that you can query and modify.
MySQL DB: MySQL is an open source database. You can download it from
. We recommend downloading the full Windows installation.In addition, download and install
as well as These are GUI based tools that will make your development much easier.Finally, download and unzip
(the MySQL JDBC driver) in a convenient directory. For the purpose of this tutorial we will assume that you have installed the driver at C:Program FilesMySQLmysql-connector-java-5.1.8.Accordingly, set CLASSPATH variable to C:Program FilesMySQLmysql-connector-java-5.1.8mysql-connector-java-5.1.8-bin.jar. Your driver version may vary based on your installation.
Set Database Credential
When we install MySQL database, its administrator ID is set to root and it gives provision to set a password of your choice.
Using root ID and password you can either create another user ID and password, or you can use root ID and password for your JDBC apppcation.
There are various database operations pke database creation and deletion, which would need administrator ID and password.
For rest of the JDBC tutorial, we would use MySQL Database with guest as ID and guest123 as password.
If you do not have sufficient privilege to create new users, then you can ask your Database Administrator (DBA) to create a user ID and password for you.
Create Database
To create the TUTORIALSPOINT database, use the following steps −
Step 1
Open a Command Prompt and change to the installation directory as follows −
C:> C:>cd Program FilesMySQLin C:Program FilesMySQLin>
Note: The path to mysqld.exe may vary depending on the install location of MySQL on your system. You can also check documentation on how to start and stop your database server.
Step 2
Start the database server by executing the following command, if it is already not running.
C:Program FilesMySQLin>mysqld C:Program FilesMySQLin>
Step 3
Create the TUTORIALSPOINT database by executing the following command −
C:Program FilesMySQLin> mysqladmin create TUTORIALSPOINT -u guest -p Enter password: ******** C:Program FilesMySQLin>
Create Table
To create the Employees table in TUTORIALSPOINT database, use the following steps −
Step 1
Open a Command Prompt and change to the installation directory as follows −
C:> C:>cd Program FilesMySQLin C:Program FilesMySQLin>
Step 2
Login to the database as follows −
C:Program FilesMySQLin>mysql -u guest -p Enter password: ******** mysql>
Step 3
Create the table Employees as follows −
mysql> use TUTORIALSPOINT; mysql> create table Employees -> ( -> id int not null, -> age int not null, -> first varchar (255), -> last varchar (255) -> ); Query OK, 0 rows affected (0.08 sec) mysql>
Create Data Records
Finally you create few records in Employee table as follows −
mysql> INSERT INTO Employees VALUES (100, 18, Zara , Ap ); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO Employees VALUES (101, 25, Mahnaz , Fatma ); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Employees VALUES (102, 30, Zaid , Khan ); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Employees VALUES (103, 28, Sumit , Mittal ); Query OK, 1 row affected (0.00 sec) mysql>
For a complete understanding on MySQL database, study the
.MySQLdb
MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API.
How do I Install MySQLdb?
Before proceeding, you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it −
#!/usr/bin/python import MySQLdb
If it produces the following result, then it means MySQLdb module is not installed −
Traceback (most recent call last): File "test.py", pne 3, in <module> import MySQLdb ImportError: No module named MySQLdb
To install MySQLdb module, use the following command −
For Ubuntu, use the following command - $ sudo apt-get install python-pip python-dev pbmysqlcpent-dev For Fedora, use the following command - $ sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc For Python command prompt, use the following command - pip install mysqlcpent
Note − Make sure you have root privilege to install above module.
Now you are ready to start experimenting with Python.
Advertisements