Skip to content

Commit 4506986

Browse files
committed
set environment variable
1 parent 7530f91 commit 4506986

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=title Setting environment variables on the command line on Windows, Linux, macOS
2+
=timestamp 2025-04-26T07:30:01
3+
=indexes set, ENV
4+
=status show
5+
=author szabgab
6+
=archive 1
7+
=comments_disqus_enable 0
8+
=show_related 1
9+
10+
Setting an environment variable on Linux and macOS can be done before we run some program.
11+
12+
```
13+
$ python -c 'import os; print(os.getenv("NAME"))'
14+
None
15+
16+
$ NAME=Foo python -c 'import os; print(os.getenv("NAME"))'
17+
Foo
18+
```
19+
20+
21+
On Windows we need to do it separately:
22+
23+
In CMD:
24+
25+
```
26+
> set NAME=Foo
27+
> python -c "import os; print(os.getenv('NAME'))"
28+
Foo
29+
```
30+
31+
In PowerShell
32+
33+
34+
```
35+
> [Environment]::SetEnvironmentVariable("NAME", "Foo", "Process")
36+
> python -c "import os; print(os.getenv('NAME'))"
37+
Foo
38+
```

0 commit comments

Comments
 (0)