0

When I want a user in Python3, from terminal

sudo su

python3

>>> import getpass

>>>getpass.getuser()

'root'

how can I get the normal username?

2 Answers 2

2

If you are running the script interactively, you should be using sudo to run it and os.getlogin() will give the correct user.

Sign up to request clarification or add additional context in comments.

1 Comment

I am not using sudo why because I running on root user
0

I recommend the os.getlogin() from @bromate's answer.

Here is another way:

import os
user = os.environ['SUDO_USER'] # KeyError if not run under sudo

1 Comment

Great, it's working. Thanks Its return my normal user name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.