Setting environment variables in linux using python for ORACLE? - python

I am using cx_oracle library and have to specify the following using 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
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
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 (e.g Python) starts.
Otherwise you can add the env vars to files like ~/.bashrc, or have a wrapper script like 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? 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

Related

Python Virtual Environment Shebang for Window and Linux

I have a python file that I'd like to execute using the virtual environment (venv) in the same directory as the file. I'd like it to use this venv regardless of whether the user has activated it or not to avoid accidentally running it without the proper environment. I'd like this to work on both Linux and Windows (via Git Bash).
The issue is that venv puts python under the "bin" directory on Linux but under "Scripts" on Windows and I can't seem to find a way to change this behavior when creating it. I tried creating it with Git Bash in hopes that it would "trick" python into using a bin directory instead of Scripts, but that didn't work.
The following shebang works well on Linux:
#!.venv3/bin/python
And this one works well on Windows:
#!.venv3/Scripts/python
But what will work on both? I know that one option would be to create a shell script that activates the environment based on the detected os (using $OSTYPE), but I'd like to avoid this if possible as it isn't otherwise necessary.
You could use the Windows version of the shebang:
#!.venv3/Scripts/python
and then create a symbolic link on the Linux side:
.venv3/Scripts -> .venv3/bin
I just tried this with one of my virtual envs. It worked fine for me. I didn't test the Windows case, but of course, that has to work as we're using the correct shebang for Windows to begin with.

How to use environment variables in blender

I added a enviroment variable writing in the ~/.bashrc file this two line
var="stuff.."
export var
using the python interpreter in a normal terminal this two lines of code works
import os
print(os.environ['var'])
but in a blender python console it generate a KeyError so printing os.environ list i can see that there isn't a item with 'var' as key
So i think that is a problem with the environment settings in unix system.
Can anyone help me and explain how export the environment variables for the other processes? Thanks and sorry for the english
The .bachrc file (and similar such as .cshrc) is read when your shell is started, similarly when you start a GUI desktop the shell rc files are read at the time of it starting and the variables at that time are then part of the environment passed onto any GUI apps, changes made while running do not get read in as you start a new app. You can find ways of setting environment variables for different desktops.
One way of passing environment variables into blender is to start it from a terminal window. The rc files will be read when you open the terminal, you can also manually set environment variables before starting blender.
Another way to set environment variables for blender is to start it from a script, this may be one called myblender that will be found in your $PATH or it can also be named blender if it will be found before the real blender. In this script you can set variables before starting blender and any changes will be in effect when you run it.
#!/bin/bash
var="stuff.."
export var
exec /usr/local/bin/blender "$#"
After updating ~/.bashrc you either have to source ~/.bashrc in the terminal where you launch blender or log out and log back in to your system, where the variable should then be in the environment.
If you need to get environment variables that may or may not be available, you can also do something like os.getenv('var', 'default value')

Linux profile.d environment variables don't work with cx_oracle in Python

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.
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.
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
And saved this .sh file into the /etc/profile.d/ folder.
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
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
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.
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?
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.
Update:
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?
Second Update:
I tried initializing the environment with a shell script that I planned to run with the code that calls cx_Oracle:
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.
Are environment variables not usable by cx_oracle unless I manually do the export statement for them?
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.
The way I solved this was to split the export statements into two separate shell scripts in profile.d.
Everything works now.

Save virtualenv variable on linux

I use Linux (Ubuntu 14). I need to save a variable to my virtualenv, so I write on the bash shell with the virtualenv activated:
$ export SECRET_KEY='asdgladfkga....'
and then I can do (using python):
import os
key = os.environ["SECRET_KEY"]
Everything works fine until I restart the computer, then, the variable is erased. How do I truly save it? can I do this without installing virtualenvwrapper?
If you want the SECRET_KEY variable to be defined only when you activate your virtualenv, then you could modify the preactivate script in your virtualenv bin directory by adding
export SECRET_KEY='asdgladfkga....'
This isn't really a virtualenv issue. virtualenv is meant to sandbox your Python environment. Running the activate script, which you source, does change some environment variables at the OS level, but those are not saved/sandboxed, and are not connected to the virtualenv itself other than in the current instance of the shell in which you ran activate.
When you reboot, your .bashrc will load your bash environment back up based on the contents of that file.
If you wanted to keep a "secret key" -- offhand, I'm not sure I get why you want to do this; what are you trying to accomplish? -- you could have a wrapper script that:
Set the key in the current shell via EXPORT
echos the key to a file in your home directory with the same EXPORT command
Then, you could modify your .bashrc to source that file if it exists, so it would get loaded on startup, and therefore survive reboots.
When the virtualenv wrapper was run again, the process would repeat (and not change anything functionally if the key didn't change; you could optimize it to not bother setting the variable again nor write out the file if the key is the same).

different PYSTARTUP variables for different python installations

We have an in-house developed network-based application that is based on python, it provides a command-line environment in the same way like python does. I also have a local python installation on my machine. Sometimes for simple python tasks, i prefer using my local python install... Is possible to to have a different PYSTARTUP env variable for each of installation?
You can always export PYSTARTUP="whatever" on the shell before starting your script.
You can also put PYSTARTUP="whatever" in front of the command you want to run, e.g. PYSTARTUP="whatever" python something.py

Categories

Resources