I'm creating cross-platform Python application, so I need to pay attention to the official disclaimers related to platform availability. For example:
os.getlogin()
...
Availability: Unix, Windows, not Emscripten, not WASI.
Unfortunatly I couldn't figure it out what "Unavailable" is in program behavior for wide spectr of functins (not only getlogin()
).
try...except StandardError
to provide failover?import
with try
as well or it is enough for function invocation? In other words:try:
import os
os.getlogin()
...
except StandardError: # platform doesn't support user recognition
...
VS
import os
try:
os.getlogin()
...
except StandardError: # platform doesn't support user recognition
...
You will simply get an AttributeError
. For example, on MacOS (set_handle_inheritable
is only available on Windows):
>>> os.get_handle_inheritable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'os' has no attribute 'get_handle_inheritable'
So you can either:
import os
and check the OS before every usageImportError
AttributeError