Guillaume Lebreton
Guillaume Lebreton

Reputation: 2783

Python os.getenv returning None, missing var on Windows 10

( >= Python 3.4 )

With os.getenv(), i am not able to retrieve some env vars, like %DATE% or %TIME%,

ex:

print(os.getenv('computername'))  # works
print(os.getenv('date'))  # not working, returning None
print(os.getenv('time'))  # not working, returning None

So i checked the list of var env python can detect by this: [print(var) for var in os.environ] It result a list of lot of env, but missing date, and time, at least. A strange thing is the last item of the list is a list of None objects, probably the missing var env, but only appearing when using the interactive console.. and thus not really in the list.. or maybe some artifacts of the print function used in the interactive console?

Try

import os
varenv = [var for var in os.environ]
[print(var) for var in varenv]`

with the interactive console from the command line

Finally, when your are in windows cmd, echo %date% %computername% works great, with no different processing between these var.

I presume it's maybe related with unicode, but havent found any answers yet in my researches: Why %DATE% or %TIME% not working while %computername% just fine ?

Upvotes: 1

Views: 2139

Answers (1)

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22952

In fact, %DATE% and %TIME% are not environment variables.

See this Microsoft documentation User Environment Variables to see the list of environment variables of your system.

Upvotes: 1

Related Questions