致命的 Python 错误:Py_Initialize:无法加载文件系统编解码器。导入错误:没有名为“编码”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30767191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:56:39  来源:igfitidea点击:

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

pythonencodingexesplinter

提问by Kusnierewicz

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

我正在尝试制作简单的 python 程序,该程序打开网页列表供用户从站点手动下载报告。我以前没有任何准备 exe 文件的经验..而且我只是在学习 python 编码的过程。所有这些都是在 Windows 7 x64 上完成的

This is my python code:

这是我的python代码:

#!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:

我的 setup.py 文件如下所示:

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:

我必须解决的第一个问题是 selenium 包中丢失的文件(webdriver.xpi 和 webdriver_prefs.json),但我手动编译后已成功将它们包含到 library.rar 文件中。不幸的是,在运行我的文件后,我收到消息:

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

回答by Ken Li

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.

当我使用 Python 3.6 安装 Anaconda 时,我遇到了同样的错误。通过添加指向 Python 安装位置的环境变量“PYTHONPATH”来解决该错误。

I refer to the following link,

我参考以下链接,

Py_Initialize fails - unable to load the file system codec

Py_Initialize 失败 - 无法加载文件系统编解码器

anacondapython

水蟒

回答by Pradeep Vasamsetty

Setup: MAC OSX ANACONDA

设置: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.

当安装了多个版本的 python 或存在部分删除的环境时,就会发生这种情况,即只删除包/安装,而不是路径变量。

Things to check upfront:

需要提前检查的事项:

1. echo $PYTHONHOME
2. echo $$PYTHONPATH

If not, set the environment variables by updating your .bashrcor .bash_profileusing

如果没有,请通过更新.bashrc.bash_profile使用来设置环境变量

1. export PYTHONHOME="/Users/<user>/anaconda3/""
2. export PYTHONPATH=$PYTHONHOME/bin

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

然后再试一次。如果这不能解决您的问题,请在您的机器中搜索名称为“encodings”的文件夹。

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

find / -type d -name "encodings"它打印具有名为“encodings”的文件夹的目录列表。

Remove all directories not pointing to your python in use.

删除所有未指向正在使用的 python 的目录。

rm -rf <directory>