Skip to content
Advertisement

Python stopped working after Windows username change

I changed the windows10 username and now python is not working anymore.

The person who previously worked on this machine created the user-profile with a space within the name (C:UsersHis Name…). This ocassionally caused Problems because some programs can’t seem to handle spaces in a path. So I changed this to “C:UsersHisName…” like this:

When I execute “python” in CMD-line it says the command was not found.

Also IDLE doesn’t start anymore and Visual Studio Code can’t run python scripts. It seems like it can’t access the extensions.

I checked PATH in the enviromental Parameters and there were still the old Paths. But changing them to the new ones didn’t make a difference.

What can I do to make python work again?

Advertisement

Answer

This might be because Python wasn’t install for all the users.

You could check if the Python executable is located in the user’s home directory. The location of the home directory is retrieved by using the os.path.expanduser() method. The location of the Python interpreter is retrieved by using the sys.executable() method.

The following function returns True if the Python interpreter was installed within the user’s home directory, and False otherwise. It works under Linux, and should work under macOS and Windows (but I didn’t test those).

import sys
import os

def user_python():
    try:
        return sys.executable.startswith(os.path.expanduser("~"))
    except AttributeError:
        return False

If you want the directory accessible by everyone, you should put it in a directory everyone has access to, such as C:Python3.6, rather that under a Users directory. During the Python installation, you are given the options of who you want to install it for (ie single user or everyone), where you want to install it (again, something like C:Pyton3.6 is a good choice), whether you want to have Python update the Environmental Variables (why, yes you do) and whether you want to have ‘pip’ installed (again yes you do).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement