Install cx_Oracle
Use Python’s Pip package to install cx_Oracle from PyPI:
python -m pip install cx_Oracle --upgrade
This will download and install a pre-compiled binary if one is available for your architecture. If a pre-compiled binary is not available, the source will be downloaded, compiled, and the resulting binary installed.
Install Oracle Client
Using cx_Oracle requires Oracle Client libraries to be installed. These provide the necessary network connectivity allowing cx_Oracle to access an Oracle Database instance. Oracle Client versions 12.2, 12.1 and 11.2 are supported.
- If your database is remote, then download the free Oracle Instant Client “Basic” or “Basic Light” package for your operating system architecture.
- Alternatively use the client libraries already available in a locally installed database such as the free Oracle XE release.
Oracle Instant Client Zip Files
To use cx_Oracle with Oracle Instant Client zip files:
-
Download an Oracle 11.2, 12.1 or 12.2 “Basic” or “Basic Light” zip file: 64-bit or 32-bit, matching your Python architecture.
-
Unzip the package into a single directory that is accessible to your application, for example
C:\oracle\instantclient_12_2
. -
Set the environment variable
PATH
to include the path that you created in step 2. For example, on Windows 7, updatePATH
in Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables -> PATH. Alternatively useSET
to change yourPATH
in each command prompt window before you run python.If you have other Oracle software installed, then when you use Python you will need to make sure that the Instant Client directory, e.g.
C:\oracle\instantclient_12_2
, occurs inPATH
before any other Oracle directories.Restart any open command prompt windows.
-
Oracle Instant Client libraries require a Visual Studio redistributable with a 64-bit or 32-bit architecture to match Instant Client’s architecture. Each Instant Client version requires a different redistributable version:
- For Instant Client 11.2 install VS 2005 64-bit or VS 2005 32-bit
- For Instant Client 12.1 install VS 2010
- For Instant Client 12.2 install VS 2013
-
If you intend to co-locate optional Oracle configuration files such as
tnsnames.ora
,sqlnet.ora
ororaaccess.xml
with Instant Client, then create anetwork\admin
subdirectory, for exampleC:\oracle\instantclient_12_2\network\admin
.This is the default Oracle configuration directory for executables linked with this Instant Client.
Alternatively, Oracle configuration files can be put in another, accessible directory. Then set the environment variable
TNS_ADMIN
to that directory name.
测试:
import
os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
os.environ['path'] = 'D:/instantclient_12_2/'
import cx_Oracle
如果不设置path,可以在py中设置path
来源:oschina
链接:https://my.oschina.net/u/157939/blog/1842462