How to Fix Fatal Python Error: Py_Initialize Unable to Load the File System Codec

Abid Ullah Feb 02, 2024
  1. the Fatal Python error: Py_Initialize: unable to load the file system codec in Python
  2. Fix the Fatal Python error: Py_Initialize: unable to load the file system codec in Python
  3. Conclusion
How to Fix Fatal Python Error: Py_Initialize Unable to Load the File System Codec

In the article, we will learn how to resolve the fatal python error that occurs during the execution of the code.

the Fatal Python error: Py_Initialize: unable to load the file system codec in Python

Suppose you are trying to construct a simple C++ test that uses an embedded Python 3.2 interpreter. There is a Fatal Python error: Py_Initialize: unable to load the file system codec when the project is built.

Example Code:

#include <Python.h>

int main(int, char**) {
  Py_Initialize();
  Py_Finalize();
  return 0;
}

Output:

Fatal Python error: Py_Initialize: unable to load the file system codec
LookupError: no codec search functions registered: can't find encoding

There is a problem loading the system codec file for Python through the C/C++ compiler.

The following factors can cause the Fatal error in Python.

  1. Python is not found in the system/environment variables, so the system cannot locate it.
  2. There may be more than one Python version available.
  3. It seems that Python is not installed correctly on the system.

Fix the Fatal Python error: Py_Initialize: unable to load the file system codec in Python

The solution is that you need to properly install the Python and correctly set the path of Python in system/environment variables. Follow these steps to accomplish this:

for Windows 10 Operating System

  1. The system can be accessed by right-clicking the Windows icon.

    Searching system in windows setting

  2. Select Edit System Environment Variables after searching for env in the search bar.

    Searching environment variable in windows setting

  3. You can delete any Python paths by clicking the path and then selecting delete.

  4. Alternatively, you can click New and add Python Path to the system variables.

    Selecting python path in environment variables setting

Doing this allows us to add the Python path to the environmental variables.

for Ubuntu or Debian

You may receive the same error if you work on Ubuntu or Debian. Here is what we need to follow to resolve this error.

To run the command, open the terminal and type the following.

$ export PYTHONHOME=/usr/local/lib/python3.5/
$ export PYTHONPATH=/usr/local/lib/python3.5/

for macOS

In macOS, the error also occurs because the system cannot locate Python. After all, it is not present in system/environment variables. This is because more than one Python version is available.

There is a slight difference in the solution between macOS and Windows. macOS users must find a file called .bashrc or .bash_profile.

There is one command that you have to add to that file.

export PYTHONHOME="/Users/<user>/python3/"
export PYTHONPATH="${PYTHONHOME}/bin"

The variables can be set using the following command once you have completed the previous step.

source .bashrc

Users are responsible for their .bashrc file (located in their home directory). Here the source will be extracted from the .bashrc file.

for CentOS

The following command can run in the terminal of centOS to resolve the problem.

export PATH=$PATH:/usr/local/bin/python

Exports the path from local Bin Python through the export PATH=$.... As a result, the /usr/local/bin/python path will be added to the existing one.

for Jupyter Notebook

Getting the fatal error when using Jupyter notebooks presents a completely different circumstance. Because the Jupyter notebook is hosted on your local computer, the error is caused because your local computer has trouble finding the path to Python.

Specifying the Python path in environmental variables is necessary to fix the error. Following the above steps will help us achieve this based on our operating system.

We can embed Python in those languages using the Python.h library in C and C++.

Conclusion

In this Python article, we have dealt with the Fatal Python error: py_initialize: unable to load the file system codec. It has been revealed what the system error is caused by.

After seeing the steps to solve the error, we set environment variables in our system.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn

Related Article - Python Error