Environment variables must be strings, so use
import os
os.environ["DEBUSSY"] = "1"
to set the variable DEBUSSY to the string 1.
To access this variable later, simply use
print(os.environ["DEBUSSY"])
Child processes automatically inherit the environment of the parent process -- no special action on your part is required.
Answer from Sven Marnach on Stack OverflowEnvironment variables must be strings, so use
import os
os.environ["DEBUSSY"] = "1"
to set the variable DEBUSSY to the string 1.
To access this variable later, simply use
print(os.environ["DEBUSSY"])
Child processes automatically inherit the environment of the parent process -- no special action on your part is required.
You may need to consider some further aspects for code robustness;
when you're storing an integer-valued variable as an environment variable, try
os.environ['DEBUSSY'] = str(myintvariable)
then for retrieval, consider that to avoid errors, you should try
os.environ.get('DEBUSSY', 'Not Set')
possibly substitute '-1' for 'Not Set'
so, to put that all together
myintvariable = 1
os.environ['DEBUSSY'] = str(myintvariable)
strauss = int(os.environ.get('STRAUSS', '-1'))
# NB KeyError <=> strauss = os.environ['STRAUSS']
debussy = int(os.environ.get('DEBUSSY', '-1'))
print "%s %u, %s %u" % ('Strauss', strauss, 'Debussy', debussy)
export - set environment variable in python script - Stack Overflow
bash - Set environment variables in a python script and use them in a chained call - Unix & Linux Stack Exchange
python - PYTHONPATH on Linux - Stack Overflow
linux - set environment variables by file using python - Stack Overflow
Videos
There is no way to change the environment variables from a child process. And there is an exactly same question on SO.
There is a workaround to what you want to achieve. Assuming you use bash as your shell, you can write the changes to bash initialization files(I chose ~/.bashrc):
#!/usr/bin/python
import os
os.system('bash -c \'echo "export a=100000" >> ~/.bashrc\'')
os.system('bash -c \'source ~/.bashrc\'')
You need to prepend bash -c, since python uses sh as the default shell.
Idea derived from a similar question on SO(different from the one falconer posted).
bash:
LD_LIBRARY_PATH=my_path
sqsub -np $1 /path/to/executable
Similar, in Python:
import os
import subprocess
import sys
os.environ['LD_LIBRARY_PATH'] = "my_path" # visible in this process + all children
subprocess.check_call(['sqsub', '-np', sys.argv[1], '/path/to/executable'],
env=dict(os.environ, SQSUB_VAR="visible in this subprocess"))
There are many good answers here but you should avoid at all cost to pass untrusted variables to subprocess using shell=True as this is a security risk. The variables can escape to the shell and run arbitrary commands! If you just can't avoid it at least use python3's shlex.quote() to escape the string (if you have multiple space-separated arguments, quote each split instead of the full string).
shell=False is always the default where you pass an argument array.
Now the safe solutions...
Method #1
Change your own process's environment - the new environment will apply to python itself and all subprocesses.
os.environ['LD_LIBRARY_PATH'] = 'my_path'
command = ['sqsub', '-np', var1, '/homedir/anotherdir/executable']
subprocess.check_call(command)
Method #2
Make a copy of the environment and pass is to the childen. You have total control over the children environment and won't affect python's own environment.
myenv = os.environ.copy()
myenv['LD_LIBRARY_PATH'] = 'my_path'
command = ['sqsub', '-np', var1, '/homedir/anotherdir/executable']
subprocess.check_call(command, env=myenv)
Method #3
Unix only: Execute env to set the environment variable. More cumbersome if you have many variables to modify and not portabe, but like #2 you retain full control over python and children environments.
command = ['env', 'LD_LIBRARY_PATH=my_path', 'sqsub', '-np', var1, '/homedir/anotherdir/executable']
subprocess.check_call(command)
Of course if var1 contain multiple space-separated argument they will now be passed as a single argument with spaces. To retain original behavior with shell=True you must compose a command array that contain the splitted string:
command = ['sqsub', '-np'] + var1.split() + ['/homedir/anotherdir/executable']
PYTHONPATHis an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:# make python look in the foo subdirectory of your home directory for # modules and packages export PYTHONPATH=${PYTHONPATH}:${HOME}/fooHere I use the
shsyntax. For other shells (e.g.csh,tcsh), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.
The folder where your modules live is dependent on
PYTHONPATHand where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the commandls-- where doeslslive?/usr/bin?/bin? 99% of the time, you don't need to care -- Just uselsand be happy that it lives somewhere on yourPATHso the shell can find it.I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.
Configure
PYTHONPATHto include the directory where your module resides and python will be able to find your module.
PYTHONPATHis an environment variable- Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7on Ubuntu- you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
- if you use pip or setuptools, then you don't need to set
PYTHONPATHexplicitly
If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.
There's a great python library python-dotenv that allows you to have your variables exported to your environment from a .env file, or any file you want, which you can keep out of source control (i.e. add to .gitignore):
# to install
pip install -U python-dotenv
# your .env file
export MY_VAR_A=super-secret-value
export MY_VAR_B=other-very-secret-value
...
And you just load it in python when your start like:
# settings.py
from dotenv import load_dotenv
load_dotenv()
Then, you can access any variable later in your code:
from os import environ
my_value_a = environ.get('MY_VALUE_A')
print(my_value_a) # 'super-secret-value'
You don't need to use subprocess.
Read lines and split environment variable name, value and assign it to os.environ:
import os
with open('/home/user/env_script.env') as f:
for line in f:
if 'export' not in line:
continue
if line.startswith('#'):
continue
# Remove leading `export `
# then, split name / value pair
key, value = line.replace('export ', '', 1).strip().split('=', 1)
os.environ[key] = value
or using dict.update and generator expression:
with open('env_script.env') as f:
os.environ.update(
line.replace('export ', '', 1).strip().split('=', 1) for line in f
if 'export' in line
)
Alternatively, you can make a wrapper shell script, which sources the env_script.env, then execute the original python file.
#!/bin/bash
source /home/user/env_script.env
python /path/to/original_script.py