=PY() in excel to return desktop path or at least windows username.

Copper Contributor

So hello everyone.
I started learning python, and its going good.
However there is discrepancy / difference between options for python and for python in excel (i.e. = PY() function.
So I tried to do this in Python:
import os

or other codes from the web to get desktop path as string. and inside the Visual studio code it works (all of them)
however when i copy that code into the =PY() function they stop working and I'm getting errors.

So my question is:
what Python code I can use to get desktop path and/or windows username inside the =PY() such that it will display as text the result?

2 Replies


To my knowledge Python in Excel accepts any Excel objects which are within opened file; code itself is run on the Microsoft cloud; thus it has no idea about your environment which is outside opened file. 

still im experimenting.
I tried this in VSC :
import os
def get_path():
PATH = os.getlogin()
return PATH
and yet im getting this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'get_path' is not defined