简体   繁体   English

使用 python 为 ORACLE 在 linux 中设置环境变量?

[英]Setting environment variables in linux using python for ORACLE?

I am using cx_oracle library and have to specify the following using linux我正在使用 cx_oracle 库,并且必须使用 linux 指定以下内容

export LD_LIBRARY_PATH=/opt/oracle/12.1.0_64bit/product/12.1.0/client_2/lib export ORACLE_HOME=/opt/oracle/12.1.0_64bit/product/12.1.0/client_2导出 LD_LIBRARY_PATH=/opt/oracle/12.1.0_64bit/product/12.1.0/client_2/lib 导出 ORACLE_HOME=/opt/oracle/12.1.0_64bit/product/12.1.0/client_2

this makes my code work fine这使我的代码工作正常

the issue is this doesn't save across every session so I decided to add it to my script instead like this问题是这不会在每个 session 中保存,所以我决定将它添加到我的脚本中,而不是像这样

os.environ["ORACLE_HOME"] = /opt/oracle/12.1.0_64bit/product/12.1.0/client_2 
os.environ["LD_LIBRARY_PATH"] = /opt/oracle/12.1.0_64bit/product/12.1.0/client_2 

but its not working.但它不工作。 I added that line right after my imports so its the first thing ran, but its not changing the variables.我在导入之后立即添加了该行,因此它首先运行,但它没有更改变量。

when I echo those two variables after the script they're still both empty.当我在脚本之后回显这两个变量时,它们仍然是空的。

On Linux, you need to set LD_LIBRARY_PATH before the process (eg Python) starts.在 Linux 上,您需要在进程(例如 Python)启动之前设置 LD_LIBRARY_PATH。

Otherwise you can add the env vars to files like ~/.bashrc, or have a wrapper script like p.sh:否则,您可以将环境变量添加到 ~/.bashrc 之类的文件中,或者使用 p.sh 之类的包装脚本:

#!/bin/sh

export LD_LIBRARY_PATH=/opt/oracle/12.1.0_64bit/product/12.1.0/client_2/lib 
export ORACLE_HOME=/opt/oracle/12.1.0_64bit/product/12.1.0/client_2
python $@

Do you need the full client?您需要完整的客户端吗? Do you have other Oracle software installed?您是否安装了其他 Oracle 软件? If no & no, then consider using Oracle Instant Client and using ldconfig so you don't need to set environment variables, see the installation instructions at https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html#ic_x64_inst如果没有&没有,那么考虑使用Oracle Instant Client并使用ldconfig这样您就不需要设置环境变量,请参阅 https 的安装说明://www.ZA189C633D9995E11instan88607170EC9A.com4 x86-64-downloads.html#ic_x64_inst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM