How to connect Python to Oracle database?
Oracle is a popular DBMS and this post walks you through how to set up the connectivity between it and Python.
Step 1 - Find out the bit version of your Python
Type “python” in the cmd window and it shows whether your Python is 32 or 64 bit.
If you use Anaconda, type “conda info” to find out its bit version as well.
Step 2 - Download and unzip Oracle Instant Client
After we find out the bit version of Python, we need to download the corresponding Oracle Instant Client package from Oracle’s website. Since I have Windows OS and 64-bit Python installed, I will download the package for Microsoft Windows (x64).
Extract the downloaded folder to “C:\Oracle” and get the folder path after extraction.
Step 3 - Add path to the system environment variable
Access the environment variable and add the above folder path to the system path as shown in the screenshots below. You might need to reboot the computer after this step if you get an error when testing the Python script in Step 5.
Step 4 - Install cx_Oracle Python library
We next install the cx_Oracle library in Python by typing “python -m pip install cx_Oracle --upgrade” in the command window. If you'd like to learn more about this library, see its website.
Step 5 - Test the Python script
If everything is set up correctly, when you run the code below, it shouldn’t show any error. This means that now you are ready to access your Oracle database via Python.