How to set environment variables in Python

0 votes

I need to set some environment variables in the Python script and I want all the other scripts that are called from Python to see the environment variables' set.

If I do,

os.environ["DEBUSSY"] = 1

it complains saying that 1 has to be a string.

I also want to know how to read the environment variables in Python (in the latter part of the script) once I set it.

Nov 20, 2020 in Python by kartik
• 37,510 points
84 views

1 answer to this question.

0 votes

Hii,

Environment variables must be strings, so use

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 variables of the parent process -- no special action on your part is required.

answered Nov 20, 2020 by Niroj
• 82,720 points

Related Questions In Python

0 votes
1 answer

How can I prevent or alter access to class variables in Python?

The ActiveState solution that Pynt references makes instances of ...READ MORE

answered Dec 5, 2018 in Python by aryya
• 7,440 points
529 views
0 votes
2 answers

How to use virtual environment in Python?

venv (for Python 3) and virtualenv (for Python 2) allow ...READ MORE

answered Aug 24, 2020 in Python by Pistle
• 300 points
119 views
0 votes
1 answer

How to set window size in Tkinter Python?

The window size adjustment can be done ...READ MORE

answered May 8, 2019 in Python by Giri
2,852 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,039 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,560 views
+1 vote
12 answers
0 votes
3 answers

How to use not equal operator in python

We can use Python not equal operator ...READ MORE

answered Dec 9, 2020 in Python by Niroj
• 82,720 points
133,042 views