hckr.fyi // thoughts

Fatal Python error Py_Initialize unable to load the file system code

by Michael Szul on

Sure that's an extremely long name for a blog post title, but at least it states the exact problem.

I ran into this early in the week when attempting to install Anaconda Python on my work computer. This was actually a reinstall. My work computer had Python on it already. In fact, it had Anaconda Python on it. This install was originally a part of the Visual Studio 2017 installation package, which let you choose additional components. I don't get to do too much Python at work (I do most of that at home on my Surface machine), I've been doing some data processing recently at work though that lends itself to an investment in Python and Jupyter Notebooks.

The problem? Jupyter Notebooks wouldn't run and Python was failing from the command line. My first assumption was that the Python installation was corrupt, so I uninstalled the Visual Studio 2017 bundled Anaconda Python, and instead, downloaded a new package from the Anaconda web site.

Nothing. In fact, the installation itself died with a "Python has stopped working" dialog from Windows.

Okay. Fine. Maybe the installer is bad, or isn't playing nicely with my computer. I'll use Chocolatey to install it.

Nothing.

The only consistent thing was that when the Python installer died, if I tried to run Python from the command line, I'd get an error message on the console:

Fatal Python error: Py_Initialize: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'
    

So Python made it on there somehow, but wasn't complete, or was blowing up.

Now I mentioned that this was my work machine. I've had Python on it before--even before the Visual Studio 2017 installation. In fact, when I installed VS2017, it was an opportunity to clean up the computer a little, so I removed many other installations (including Python) to start fresh with what I really needed for day-to-day development, and since VS2017 offered Anaconda for data science, I figured I would let Visual Studio handle those packages.

When you clean up your installations, however, it doesn't mean that you've cleaned up your environment. Turns out I had some errant PATH and PYTHONPATH environment variables that were pointing to directories still containing old Python 2.7 scripts and modules. Deleting these entries, and then uninstalling and reinstalling Anaconda Python ultimately resulted in a successful installation, and everything ran perfectly after that.