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)
python - How to pass a list as an environment variable? - Stack Overflow
python - How to add to the PYTHONPATH in Windows, so it finds my modules/packages? - Stack Overflow
Adding Python Path to Environment Variables HELP!
How to add python to path and how to use it in commandline/powershell?
For Windows 10/8/7:
- Open
System Properties(Right clickComputerin the start menu, or use the keyboard shortcut Win+Pause) - Click
Advanced system settingsin the sidebar. - Click
Environment Variables... - Select
PATHin theSystem variablessection - Click
Edit Add Python's path to the end of the list (the paths are separated by semicolons). For example:
C:\Windows;C:\Windows\System32;C:\Python27
For Windows XP:
- Open
System Properties(Type it in the start menu, or use the keyboard shortcut Win+Pause) - Switch to the
Advancedtab - Click
Environment Variables... - Select
PATHin theSystem variablessection - Click
Edit Add Python's path to the end of the list (the paths are separated by semicolons). For example:
C:\Windows;C:\Windows\System32;C:\Python27Test on a new terminal window or if using an integrated terminal within a text editor, close and restart your editor or the changes won't be applied.
For anyone trying to achieve this with Python 3.3+, the Windows installer now includes an option to add python.exe to the system search path. Read more in the docs.
The rationale
I recommend using JSON if you want to have data structured in an environment variable. JSON is simple to write / read, can be written in a single line, parsers exist, developers know it.
The solution
To test, execute this in your shell:
$ export ENV_LIST_EXAMPLE='["Foo", "bar"]'
Python code to execute in the same shell:
import os
import json
env_list = json.loads(os.environ['ENV_LIST_EXAMPLE'])
print(env_list)
print(type(env_list))
gives
['Foo', 'bar']
<class 'list'>
Package
Chances are high that you are interested in cfg_load
Debugging
If you see
JSONDecodeError: Expecting value: line 1 column 2 (char 1)
You might have used single-quotes instead of double-quotes. While some JSON libraries accept that, the JSON standard clearly states that you need to use double-quotes:
Wrong: "['foo', 'bar']"
Right: '["foo", "bar"]'
I'm not sure why you'd do it through the environment variables, but you can do this:
export LIST_ITEMS ="a.1 b.2 c.3"
And in Python:
list1 = [i.split(".") for i in os.environ.get("LIST_ITEMS").split(" ")]
for k, v in list1:
print(k, v)
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it, Please add
C:\Python27;
along with it. Else it will never work.
Windows 7 Professional I Modified @mongoose_za's answer to make it easier to change the python version:
- [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
- Click [New] under "System Variable"
- Variable Name: PY_HOME, Variable Value:C:\path\to\python\version

- Click [OK]
- Locate the "Path" System variable and click [Edit]
Add the following to the existing variable:
%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;

Click [OK] to close all of the windows.
As a final sanity check open a command prompt and enter python. You should see
>python [whatever version you are using]
If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.
Hey everyone. I’m an absolute beginner to computers, let alone python. I’m trying to add a python path to environment variable but when I try to edit the path, it takes me to a table-like screen. 1st pic is me selecting path and 2nd is the table. I’m following this tutorial online up until 1:20, where I’m getting a different screen than the uploader. I’ll try to link in another post under this, there’s only one attachment per post. Any help as to why that is, is greatly appreciated!