set environment variable in python script
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"))
Set shell environment variable via python script
As long as you start the "instrument" (a script I suppose) from the very same process it should work:
In [1]: os.putenv("VARIABLE", "123")
In [2]: os.system("echo $VARIABLE")
123
You can't change an environment variable of a different process or a parent process.
How to set environment variables in GitHub actions using python
You cannot set environment variables directly. Instead, you need to write your environment variables into a file, whose name you can get via $GITHUB_ENV
.
In a simple workflow step, you can append it to the file like so (from the docs):
echo "{name}={value}" >> $GITHUB_ENV
In python, you can do it like so:
import os
env_file = os.getenv('GITHUB_ENV')
with open(env_file, "a") as myfile:
myfile.write("MY_VAR=MY_VALUE")
Given this python script, you can set and use your new environment variable like the following:
- run: python write-env.py
- run: echo ${{ env.MY_VAR }}
How to run a script with a temporary environment variable?
Your shell command can be
BEAR_NAME="sleepybear" python hello.py
Related Topics
How to Find First Non-Zero Value in Every Column of a Numpy Array
How to Initialize Weights in Pytorch
Python 2.X - Write Binary Output to Stdout
What Is the Most Compatible Way to Install Python Modules on a MAC
Converting List of Tuples into a Dictionary
High Performance Fuzzy String Comparison in Python, Use Levenshtein or Difflib
Calculating Pearson Correlation and Significance in Python
Modular Multiplicative Inverse Function in Python
How to Print Out Status Bar and Percentage
Substitute Multiple Whitespace with Single Whitespace in Python
How to Apply Itertools.Product to Elements of a List of Lists
How to Use a Conditional Expression (Expression with If and Else) in a List Comprehension
How to Give Column Name Dynamically from String Variable in SQL Alchemy Filter
Flask-Sqlalchemy Import/Context Issue
Detecting Mouse Clicks in Windows Using Python
Solving Embarassingly Parallel Problems Using Python Multiprocessing