本文介绍了Linux profile.d环境变量在Python中不适用于cx_oracle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前的问题的一个延续:。

This is a bit of a continuation from my previous question: cx_Oracle does not recognize location of Oracle software installation for installation on Linux.

在我能够正确安装cx_oracle之后,我想设置我的环境,所以环境变量不必每次导出。

After I was able to get cx_oracle installed properly, I wanted to set up my environment so the environment variables don't have to be exported every time.

为此,我写了一个包含这两个导出语句的shellscript :

To do this, I wrote a shellscript that included these two export statements:

export ORACLE_HOME=/home/user1/instantclient_12_1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

并将此.sh文件保存到 /etc/profile.d / 文件夹。

And saved this .sh file into the /etc/profile.d/ folder.

当我用PuTTY再次登录服务器时,echo语句表示环境变量位于:

When I log into the server again with PuTTY, the echo statements say that the environment variables are there:

# echo $ORACLE_HOME
/home/user1/instantclient_12_1
# echo $LD_LIBRARY_PATH
:/home/user1/instantclient_12_1

但是当我使用cx_oracle运行一些python代码时,我收到一个错误:

But when I run some python code with cx_oracle, I get an error:

ImportError: libclntsh.so.12.1: cannot open shared object file: No such file or directory

代码只在我重新输入导出命令时再次运行为环境变量。在执行此操作之后,使用cx_oracle的代码运行正常。

The code only runs again when I re-enter the export commands for the environment variables. After I do that, the code using cx_oracle runs fine.

为什么环境变量不能正常工作,即使我在执行echo命令时显示?我如何使环境变量保持正确?

Why don't the environment variables work properly even though they show up when I do the echo command? And how do I get the environment variables to persist properly?

我阅读的指南说要使用 / etc / profile中的shell脚本因为最好不要直接编辑 / etc / profile

The guides I read say to do it with a shell script in /etc/profile.d/ because it's better to not edit /etc/profile directly.

更新:

我尝试将两条导出行添加到 / etc / profile 但是我仍然会遇到相同的问题,当环境变量在那里,当我回声,但我仍然得到这个错误,当尝试使用cx_oracle在python:

I tried adding the two export lines to /etc/profile, but I still get the same problem where the environment variables are there when I echo, but I still get this error when trying to use cx_oracle in python:

ImportError: libclntsh.so.12.1: cannot open shared object file: No such file or directory



我没想到有关定义环境变量的一些关键事项?

Am I missing some key thing about defining environment variables?

第二次更新:
我尝试用a初始化环境我计划使用调用cx_Oracle的代码运行的shell脚本:

Second Update:I tried initializing the environment with a shell script that I planned to run with the code that calls cx_Oracle:

StartServer.sh的内容:

Contents of StartServer.sh:

export ORACLE_HOME=/home/user1/instantclient_12_1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
python3 ./UDPDBQuery.pyc

我尝试在后台运行代码:

And I try to run the code in the background by doing:

bash StartServer.sh &

但是我仍然遇到与以前一样的错误,就好像我没有放入环境变量。它只适用于我自己导出变量,然后再次运行代码。当我注销时,代码也停止在后台运行。我仍然非常困惑,为什么它不工作。

But I still run into that same error as before, as if I did not put in the environment variables. It only works if I export the variables myself, and then run the code again. The code also stops running in the background when I log out. I'm still very confused as to why it isn't working.

环境变量不能被cx_oracle使用,除非我手动为他们导出导出语句?

Are environment variables not usable by cx_oracle unless I manually do the export statement for them?

推荐答案

好的,我发现两个环境变量之一没有正确导出与 .sh / code> /etc/profile.d 中的code>文件,并且执行 $ LD_LIBRARY_PATH 会给我没有这样的文件或directorytclient_12_1 ,但 $ ORACLE_HOME 会给我 / home / user1 / instantclient_12_1 /:是一个目录

Alright, I found out that one of the two environment variables was not exporting properly with the .sh file in /etc/profile.d, and doing $LD_LIBRARY_PATH would give me No such file or directorytclient_12_1, but $ORACLE_HOME would give me /home/user1/instantclient_12_1/: is a directory.

我解决这个问题的方式是将导出语句拆分成配置文件中的两个单独的shell脚本。 d

The way I solved this was to split the export statements into two separate shell scripts in profile.d.

现在一切都可行。

这篇关于Linux profile.d环境变量在Python中不适用于cx_oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:12