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

36.5k views Asked by At

I am trying to make simple python program that is opening list of webpages for a user to manually download reports from the site. I don't have any previous experience with preparing exe files.. And I'm just in learning process for python coding. All of this is done on Windows 7 x64

This is my python code:

#!C:/Python34/python.exe -u

from splinter import *
import time
import os
import csv

#----------------------------------
raporty = []
with open('../raporty.csv', newline='') as csvfile:
    contents = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in contents:
        r = ', '.join(row)
        r = r.replace(',','')
        raporty.append(r)

#--not implemented yet
zmienne = []
with open('../zmienne.csv', newline='') as csvfile:
    contents = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in contents:
        r = ', '.join(row)
        r = r.replace(',','')
        zmienne.append(r)

print("start")
browser = Browser()

#----------------LOGIN------------------
browser.visit('https://xxxx')
print(browser.title)
if browser.title == "xxxxxxxxxxxx":
    element = browser.find_by_name('login').first
    element.value = "xxxx"
    element2 = browser.find_by_name('password').first
    element2.value = "xxxx"
    browser.find_by_value('sign in').click()

time.sleep(5)

#----------------------------------
j = 1
for i in raporty:
    webpage = 'webpage_link'
    print("text" + i)
    browser.visit(webpage)
    j += 1

    if j > 15:
        time.sleep(j)
    else:
        time.sleep(12)

My setup.py file looks like this:

from distutils.core import setup
import py2exe

setup(
    console=['Final.py'],
    options={
            "py2exe":{
                    "skip_archive": True,
                    "unbuffered": True,
                    "optimize": 2,
                    "packages": ["encodings", "splinter"]
            }
    },
)

First issue I had to resolved was a missing files (webdriver.xpi and webdriver_prefs.json) from selenium package, but I've successfully included them in to library.rar file after compilation by hand. Unfortunately right know after running my file I get message:

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

There are 2 answers

1
Ken Li On

I have the same error when I install Anaconda with Python 3.6. The error is resolved by adding an environment variable "PYTHONPATH" which point to the installation location of Python.

I refer to the following link,

Py_Initialize fails - unable to load the file system codec

0
Pradeep Vasamsetty On

Setup: MAC OSX ANACONDA

It happens when there are multiple versions of python installed or partially deleted environments are present i.e just removing the packages/installations, not the path variables.

Things to check upfront:

echo $PYTHONHOME
echo $PYTHONPATH

If not, set the environment variables by updating your .bashrc or .bash_profile using

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

To set these varuiables in your current shell, run source .bashrc.

If this doesn't solve your problem then, search for folders with the name "encodings" listed in your machine.

find / -type d -name "encodings" which prints a list of directories having a folder named "encodings".

Remove all directories not pointing to your python in use.

rm -rf <directory>