Forum Discussion

RadoslawPoprawski's avatar
RadoslawPoprawski
Copper Contributor
Feb 17, 2024

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

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
os.getlogin()

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

  • RadoslawPoprawski 

    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. 

    • RadoslawPoprawski's avatar
      RadoslawPoprawski
      Copper Contributor
      still im experimenting.
      I tried this in VSC :
      import os
      def get_path():
      PATH = os.getlogin()
      return PATH
      print(get_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

Resources