一、安裝環境
服務器:Windows Server 2016
Python版本:Python 3.8.5(如果不知道所安裝的版本,可用如下命令來查詢)
D:\> python Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
二、在python中安裝cx_Oracle
1. 在PyPI網站上下載cx_Oracle,下載網址為:https://pypi.org/project/cx_Oracle/#files。找到對應的版本,本例中下載的是cx_Oracle-8.0.1-cp38-cp38-win_amd64.whl

2. 安裝wheel用於支持安裝whl文件
D:\python\python38> pip install wheel Collecting wheel Downloading wheel-0.35.1-py2.py3-none-any.whl (33 kB) Installing collected packages: wheel Successfully installed wheel-0.35.1 WARNING: You are using pip version 20.1.1; however, version 20.2.4 is available. You should consider upgrading via the 'd:\python\python38\python.exe -m pip install --upgrade pip' command.
出現如上提示表明wheel-0.35.1安裝成功。
插曲:在上面安裝wheel的提示中,出現一條WARNING信息,提示pip的版本可以升級,因此也根據提示進行升級。
D:\Python\Python38>python -m pip install --upgrade pip
Collecting pip
Downloading pip-20.2.4-py2.py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 544 kB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.1.1
Uninstalling pip-20.1.1:
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒絕訪問。: 'd:\\python\\python38\\lib\\site-packages\\pip-20.1.1.dist-info\\entry_points.txt'
Consider using the `--user` option or check the permissions.
WARNING: You are using pip version 20.1.1; however, version 20.2.4 is available.
You should consider upgrading via the 'D:\Python\Python38\python.exe -m pip install --upgrade pip' command.
出現了拒絕訪問錯誤,可能是權限問題造成的,提示使用--user參數。
D:\Python\Python38>python -m pip install --user --upgrade pip Collecting pip Using cached pip-20.2.4-py2.py3-none-any.whl (1.5 MB) Installing collected packages: pip WARNING: The scripts pip.exe, pip3.8.exe and pip3.exe are installed in 'C:\Users\metc\AppData\Roaming\Python\Python38\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-20.2.4 WARNING: You are using pip version 20.1.1; however, version 20.2.4 is available. You should consider upgrading via the 'D:\Python\Python38\python.exe -m pip install --upgrade pip' command. D:\Python\Python38>python -m pip install --user --upgrade pip Requirement already up-to-date: pip in c:\users\metc\appdata\roaming\python\python38\site-packages (20.2.4)
至此pip已經升級到最新版本20.2.4
3. 安裝cx_Oracle-8.0.1-cp38-cp38-win_amd64.whl
D:\Python>pip install cx_Oracle-8.0.1-cp38-cp38-win_amd64.whl Processing d:\python\cx_oracle-8.0.1-cp38-cp38-win_amd64.whl Installing collected packages: cx-Oracle Successfully installed cx-Oracle-8.0.1
三、安裝Oracle輕量即時客戶端Instant Client
1. 根據服務器類型下載安裝文件,下載地址為:https://www.oracle.com/database/technologies/instant-client/downloads.html,本例中下載的是Instant Client for Microsoft Windows (x64)。

2. 將下載的文件解壓縮,本例中放在D:\oracle\instantclient_19_8\,然后將此目錄配置到系統環境變量的PATH中

3. 配置完成后需要重啟服務器,以使環境變量生效。
四、用py程序測試連接
編寫以下testOracle.py代碼進行測試:
import cx_Oracle
host = "192.168.1.101"
port = "1521"
sid = "oracledb"
dsn = cx_Oracle.makedsn(host, port, sid)
conn = cx_Oracle.connect("usrName", "passWord", dsn)
cursor = cx_Oracle.Cursor(conn)
cursor.execute("select * from v_user")
result = cursor.fetchall()
print(result)
cursor.close()
conn.close()
