'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:\Users\His Name...). This ocassionally caused Problems because some programs can't seem to handle spaces in a path. So I changed this to "C:\Users\HisName..." 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?



Solution 1:[1]

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).

Solution 2:[2]

You have changed the profile name and path in registry that's fine But you have to also add new python path in environment variables

Please update new python path in Environment Variables

Steps:

  1. Search Environment Variables in Cortana
  2. Click on "Environment Variables"
  3. In User Variables click on New
  4. Now add the path of python
  5. Close current cmd and open a new cmd to run python

Solution 3:[3]

I have the same problem here, same mistake. But then, I try open the installation file again (as administrator), then I click repair, somehow it could repair itself, creating another user in the users folder. Then I could uninstall it. In the end, I install it again under my own username

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit Gupta
Solution 2 Maheshwar Kuchana
Solution 3 Abraham