34

I'm installing Python on my Windows 10 laptop, and when I try to run it I get this:

Fatal Python error: Py_Initialize: unable to load the file system codec ModuleNotFoundError: No module named 'encodings' Current thread 0x0000037c (most recent call first):

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milos Milunovic
  • 422
  • 2
  • 5
  • 10

9 Answers9

36

I ran into this same issue on Windows 10. Here's how I fixed it:

  1. Open your 'Environment Variables' (Under 'System Properties').
  2. In the window that opens, select the 'Path' row, then click the 'Edit...' button.
  3. There should be two environment variables C:\Python37-32\Scripts\ and C:\Python37-32\ Then click 'OK' (Make sure to check that these path values correspond to the location and version of your Python install.)
  4. Next, in the top portion of the 'Environment Variables' window, look for the PYTHONHOME variable and make sure that it is also set to C:\Python37-32
J.S. Peterson
  • 2,839
  • 1
  • 14
  • 17
  • 1
    So while installing packages with pip, I need to be at C:\Python37-32\Scripts\ always? Is there a way to install packages without getting in C:\Python37-32\Scripts\ ? – Shruti Kar May 18 '19 at 20:14
  • 5
    What do you mean by: "There should be two environment variables C:\Python37-32\Scripts\ and C:\Python37-32\"? Those are pathnames not environment variables. Please edit your comment to give the names of the environment variables. – Francis Litterio Feb 19 '20 at 14:55
  • @FrancisLitterio: I had a bit of trouble understanding too. He was saying to add those two paths to the Path environment variable (that he says to find and edit in step 2) – Malivil Mar 31 '20 at 21:17
  • 1
    I wonder why the python installer in Windows doesn't already set all this?! – Matthew Sep 01 '22 at 05:24
3

This issue has been addressed here in this thread. Apparently, running the Python setup as an administrator and/or correctly setting the environment variables solved this issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paandittya
  • 885
  • 8
  • 17
  • 5
    This is a common error from setting `PYTHONHOME` permanently. Any distribution of Python that requires this is badly designed. – Eryk Sun Mar 01 '17 at 10:57
  • @eryksun, can you explain a little? – Jamie Marshall Oct 05 '18 at 21:16
  • @JamieMarshall, `PYTHONHOME` isn't limited to any particular version or distribution of Python, and it overrides the default way the standard library is located. Thus setting it permanently will break every other Python installation. – Eryk Sun Oct 06 '18 at 21:24
  • @ErykSun, intrestingly, Visual Studio, requires this to be explicitly set in order to have successful compilation when it comes to python extension /embedding development. – Hossein Jun 06 '20 at 09:59
1

First, don't forget to select "Add Python 3.x to PATH" before you click on Install now and reboot after installation so that the new path is taken into account by Windows.

Second, I had the the same problem with Python 3 on Windows 7 and 64-bit and I got rid of it by deleting PYTHONPATH and PYTHONHOME from Windows 7 system environment variables, because I had a previous installation of Python 2 and those paths were pointing to my the Python 2 directory. I had to simply to delete the PYTHONPATH and PYTHONHOME variables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gorgorlu
  • 21
  • 4
1

I had the same problem (I use a Mac, but I don't think it makes any difference in this problem) and it was coming from the difference between the selected Python interpreter path and the current working directory in spyder.

I use virtual environments in Python to avoid version conflicts of installed modules between various projects (I believe it is a good practice). This leads me to configure the interpreter path in spyder by accessing:

preferences > python interpreter > use the following interpreter

The problem comes when I launch spyder from a different path. I get exactly the same error as Milo's. To solve it I see two options:

  1. change the interpreter path to match the spyder working directory
  2. change the spyder working directory to match the Python interpreter path
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David
  • 49
  • 2
1

I solved this issue by deleting my virtual environment and creating a new one. I believe in my case the error came because the old virtual environment was running on Python 3.6, which I had recently uninstalled and replaced with Python 3.8.

This is probably bad practice in general, but I don't have any real projects where the version matters.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
0

Even I had the same issue when I installed the Python 3.7 beta version, and I resolved it by following these steps:

  1. If you have nay previous version of Python installed and the environment variable and path is set for that version already, just remove the path and environment variable

  2. Run the downloaded Python 3.7 EXE file file as administrator

  3. At the end of installation if it asks the permission for path length just click on that. Now type "python" on the command line and see. It should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pujitha
  • 11
  • 2
0

I am not sure why this question is still here without a solution. I just encountered this and solved by this:

  1. Close all CMD or console emulators.

  2. Go to the system environment settings and clear all old Python path or environment settings. Make sure you check the PATH in both User and System settings as well.

  3. Try again python -V and see if you can run it or not.

  • If you have removed all Python environment settings, I recommend you to reinstall Python and turn on the Add Python to PATH setting during installation.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DrKNa
  • 55
  • 1
  • 8
0

Before installing the Python interpreter, check environment and remove the existing PYTHONHOME, and python path under "PATH" of environment. Or change it to the new path to be installed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
caot
  • 3,066
  • 35
  • 37
0

If this issue is happening to you in a virtual environment, just delete it and create another. It worked for me.

justbecause
  • 189
  • 2
  • 9