[Solved] Fatal Python Error: Py_initialize: Unable to Load the File System Codec

Hello Geeks, I hope all are doing great. So in this article, we will see how we can solve the fatal python error: py_initialize: unable to load the file system codec. But before that, we will see some of the sample code and try to figure out why the error occurred. So, let’s get started.

Sample 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

So, as the error raised, we can say that the problem lies within initializing Python in our program. The C/C++ compiler is unable to load the system codec file for Python. The codec file is responsible for encoding and decoding our python program, and if the compiler fails to do so, it raises the given error. Now there may be several reasons for the error to occur. Some of them are discussed below:

  • System unable to locate python as it is not available in system/environment variables.
  • There may be more than one python versions available.
  • Python is not properly installed in the system.

Solution: Changing Python Path In Environment Variables

Now, in all the above cases, the solution is that you need to properly install the Python and correctly set the path of Python in system/environment variables. To do that, do the following steps:

[ For Windows 10 ]

  • Open system by right clicking on windows icon.
System windows 10
  • Search env in search bar and select “Edit System Environement Variables”.
Search env
  • If any path exists for python, you can delete them by clicking the path and then selecting delete.
  • Else, Select path and click “New” to add “PythonPath” in the system variables.
Environment Variable Path

In this way, you can add the python path to the environmental variables.

Fixing the Error in Ubuntu or Debian

You may also get the error while working on Debian or ubuntu. The reason mentioned above follows here too. However, the solution to fix the issue is quite simple in this case. You need to open the terminal and run the following command.

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

Fixing the Error in MacOS

However, the reason for the error remains the same for the macOS also, i.e., the system is unable to locate Python as it is not available in system/environment variables, or There may be more than one python version available. However, in macOS, the solution is slightly different than windows OS. In macOS, you have to find the .bashrc or .bash_profile file. In that file, you have to add the following command.

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

Once done, you can use the following command to set the variables.

source .bashrc

Fixing the Error in centOS

In centOs, we can solve the issue by running the following command in its terminal.

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

This will append the “/usr/local/bin/python” path in the existing path.

Fixing Error in Anaconda

However, you are using an anaconda, and while using it, you are getting the error. You need to add the anaconda path in the path variable. To do that, you need first to open the anaconda prompt and follow the given command.

where conda
where python

Once you get the output, you need to add that to the environmental variable of your system. You can follow the above instructions based on Operating System you have.

Fixing the Error in JupyterNotebook

However, the scenarios change here a bit when it comes to getting the error while using a jupyter notebook. Here, the error occurred because your local computer is not finding the path to Python as the jupyter notebook gets hosted on your local computer. To fix the error, one must specify the python path in environmental variables. You can follow the above sections based on your operating system to do that.

FAQs on Fatal Python Error: Py_initialize: Unable to Load the File System Codec

Can you embed Python with c++/C?

Yes, we can embed Python in C and C++ using Python.h library in C and C++.

Conclusion

So, today in this article, we have solved the Fatal python error: py_initialize: unable to load the file system codec. We have seen what the reasons for the error in the system are. Then we saw the steps to solve the error and set environment variables in our system.

I hope this article has helped you. Thank You.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments