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 Overflow
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-set-python-environment-variable-pythonpath-on-linux
How to set Python environment variable PYTHONPATH on Linux?
March 24, 2026 - Open a terminal and use the export command to set PYTHONPATH ? export PYTHONPATH=/home/user/myproject:$PYTHONPATH · This command adds /home/user/myproject to PYTHONPATH while preserving any existing paths. The colon (:) separates multiple paths on Linux. ... This displays the current PYTHONPATH value, showing all directories Python will search for modules.
Discussions

bash - Set environment variables in a python script and use them in a chained call - Unix & Linux Stack Exchange
Unix & Linux is part of Stack Overflow’s open communities: specialist spaces where curiosity is welcome, knowledge is shared freely, and the best answers rise to the top. Find your community · The 2026 Annual Developer Survey Results Site is live— view the results here!. ... Is it possible to pass environment variables from child to parent in user space? [duplicate] (3 answers) Closed 2 years ago. I would like to set ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
August 28, 2024
linux - set environment variables by file using python - Stack Overflow
I want set these environment variables by reading from file . how can i do this in python More on stackoverflow.com
🌐 stackoverflow.com
python - PYTHONPATH on Linux - Stack Overflow
PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. More on stackoverflow.com
🌐 stackoverflow.com
export - set environment variable in python script - Stack Overflow
I have a bash script that sets an environment variable an runs a command LD_LIBRARY_PATH=my_path sqsub -np $1 /homedir/anotherdir/executable Now I want to use python instead of bash, because I wan... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Vonage
developer.vonage.com › en › blog › python-environment-variables-a-primer
Python Environment Variables (Env Vars): A Primer
You can store environment variables ... will need the following: ... You get and set environment variables in Python using the built-in os module....
🌐
Tutorialspoint
tutorialspoint.com › python › python_environment.htm
Python - Environment Setup
To add the Python directory to ...ocal/bin/python" and press Enter. In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter....
🌐
Medium
medium.com › python-bootcamp-basics › ubuntu-environment-variables-and-accessing-them-via-python-98e1484c6673
Ubuntu environment variables and accessing them via python | by Rupesh Desai | Python Bootcamp Basics | Medium
October 15, 2021 - This Python Bootcamp series is designed for developers or anyone who is new to Python programming so they can get started immediately. ... It is quite a daunting task to figure out how to set environment variables and access them in your application until you know exactly how to do it. My goal in this story is to provide a short two-minute read-up / video on achieving this quickly. There are two types of commands on Linux / Ubuntu to set environment variables if you are reading this.
🌐
Ask Ubuntu
askubuntu.com › questions › 1331463 › how-to-make-an-environment-variable-available-for-a-python-program-using-os-envi
How to make an environment variable available for a python program using os.environ module? - Ask Ubuntu
... When using pycharm, the environment variables have to be defined in "Run/Debug Configurations" dialog. In the python configuration inside the region "Environment" you can define environment variables that are passed to the program.
Find elsewhere
🌐
Linux Hint
linuxhint.com › set-environment-variables-python
How to get and set environment variables in Python – Linux Hint
The os module will require to import to read the environment variables. The os.environ object is used in Python to access the environment variable. The coder can set and get the value of any environment variable by using this object.
🌐
CodingNomads
codingnomads.com › blog › python-environment-variables-set-a-variable-in-bash
Python Environment Variables: Set a Variable in Bash
For a project-specific secret, it’s a much better idea to set a variable in Bash inside of a virtual environment, which makes the variables turn into virtual environment variables. Using environment variables inside of a Python virtual environment is easier than having to first export and then unset each variable every time that you want to work on a project.
🌐
YouTube
youtube.com › watch
How To Set And Use Environment Variables In Your Python And R Scripts - YouTube
In this video, I'll show you how to set environment variables on Windows, Mac, and Linux. I'll also show you how to read them into your Python & R scripts.🔗...
Published: August 21, 2023
🌐
Stack Exchange
unix.stackexchange.com › questions › 782648 › set-environment-variables-in-a-python-script-and-use-them-in-a-chained-call
bash - Set environment variables in a python script and use them in a chained call - Unix & Linux Stack Exchange
August 28, 2024 - Since python runs in a child process of the shell it can't directly alter the shell's environment variables. However, your python script can output proper shell commands that can be evaluated in the shell. $ python ./set_env.py MY_VAR="x y z";MY_VAR2="a b c"
🌐
Medium
medium.com › @haroldfinch01 › how-to-set-environment-variables-in-python-ee16132ca524
How to set environment variables in Python? | by Harold Finch | Medium
January 28, 2025 - How to set environment variables in Python? In Python, you can set environment variables using the os module. Environment variables are typically used to store configuration settings or sensitive …
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › using_pythonpath.html
Using PYTHONPATH — Functional MRI methods
From the advanced system settings dialog, choose the “Environment variables” button: In the Environment variables dialog, click the “New” button in the top half of the dialog, to make a new user variable: Give the variable name as PYTHONPATH and the value is the path to the code directory.
🌐
Medium
medium.com › @cadarsh88 › set-and-manage-environment-variables-keys-python-os-package-ba75788a1faa
Set and Manage Environment Variables, Keys — Python OS Package | by Adarsh | Medium
November 28, 2025 - But, if we need to override the configured keys for a specific execution, we can set them using Python’s os.environ method. import os import boto3 # 1. Set the environment variables (these override ~/.aws/credentials) os.environ['AWS_ACCESS_KEY_ID'] = 'AKIA...TemporaryKey...' os.environ['AWS_SECRET_ACCESS_KEY'] = 'wJalrX...TemporarySecret...' os.environ['AWS_REGION'] = 'us-west-2' # This also overrides the config file # 2.
Top answer
1 of 3
73
  1. PYTHONPATH is 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}/foo 
    

    Here I use the sh syntax. 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).

  2. 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.

  3. The folder where your modules live is dependent on PYTHONPATH and 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 command ls -- where does ls live? /usr/bin? /bin? 99% of the time, you don't need to care -- Just use ls and be happy that it lives somewhere on your PATH so the shell can find it.

  4. 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.

  5. Configure PYTHONPATH to include the directory where your module resides and python will be able to find your module.

2 of 3
46
  1. PYTHONPATH is an environment variable
  2. Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
  3. /usr/lib/python2.7 on Ubuntu
  4. 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).
  5. if you use pip or setuptools, then you don't need to set PYTHONPATH explicitly

If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.

Top answer
1 of 3
118

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"))
2 of 3
26

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']
🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding the python path environment variable in python
Understanding the Python Path Environment Variable in Python [Updated]
September 14, 2025 - Python path is an environment variable used to maintain directories of custom Python libraries. Learn how to set a python path variable on Windows and Mac now!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Doppler
doppler.com › blog › environment-variables-in-python
Using Environment Variables in Python for App Configuration and Secrets
Set environment specific database variables such as DB_USER and DB_PASSWORD so database credentials are not hard-coded · When a Python process is created, the available environment variables populate the os.environ object which acts like a Python dictionary.