If you are running the script interactively, you should be using sudo to run it and os.getlogin() will give the correct user.
When I want a user in Python3, from terminal
sudo su
python3
>>> import getpass
>>>getpass.getuser()
'root'
how can I get the normal username?
python get normal username from the root user?