OpenSuse 上没有名为 'encodings' 的模块

No module named 'encodings' on OpenSuse

一大堆动作不断回到这个问题:

pip install encodings

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'


python3

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'


libreoffice --safe-mode

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'


zypper se python |grep '^i '

i  | libpython3_6m1_0                                         | Python Interpreter shared library                                                     | package
i  | libsamba-policy0-python3                                 | Active Directory Group Policy library                                                 | package
i  | python-qt3d-qt5-sip                                      | Sip files for python-qt3d-qt5                                                         | package
i  | python-qt5-utils                                         | Common files for PyQt5 for python2 and python3                                        | package
i  | python-qtdatavis3d-qt5-sip                               | Sip files for python-qtdatavis3d-qt5                                                  | package
i  | python-qtwebengine-qt5-sip                               | Sip files for python-qtwebengine-qt5                                                  | package
i  | python-rpm-generators                                    | Dependency generator dependencies for Python RPMs                                     | package
i  | python-rpm-macros                                        | RPM macros for building of Python modules                                             | package
i  | python-sip-common                                        | SIP tool to create python bindings -- common files                                    | package
i  | python-websockify-common                                 | Common data files for the Websockify TCP proxy/bridge                                 | package
i  | python3-Automat                                          | Self-service finite-state machines for the programmer on the go                       | package
i  | python3-Babel                                            | Internationalization utilities                                                        | package
i  | python3-Bottleneck                                       | A collection of fast NumPy array functions                                            | package
i  | python3-Brotli                                           | Python bindings for the Brotli compression library                                    | package
i  | python3-Cycler                                           | Composable style cycles                                                               | package
i  | python3-Cython                                           | The Cython compiler for writing C extensions for the Python language                  | package
i  | python3-Genshi                                           | A toolkit for generation of output for the web                                        | package
i  | python3-HeapDict                                         | A heap with decrease-key and increase-key operations                                  | package
i  | python3-Jinja2                                           | A fast and easy to use template engine written in pure Python                         | package
i  | python3-MarkupSafe                                       | Implements a XML/HTML/XHTML Markup safe string for Python                             | package
i  | python3-Pillow                                           | Python Imaging Library (Fork)                                                         | package
i  | python3-PyHamcrest                                       | Hamcrest framework for matcher objects                                                | package
i  | python3-PyJWT                                            | JSON Web Token implementation in Python                                               | package
i  | python3-PyNaCl                                           | Python binding to the Networking and Cryptography (NaCl) library                      | package
i  | python3-PySocks                                          | A Python SOCKS client module                                                          | package
i  | python3-PyWavelets                                       | PyWavelets is a Python wavelet transforms module                                      | package
i  | python3-PyYAML                                           | YAML parser and emitter for Python                                                    | package
i  | python3-Pygments                                         | A syntax highlighting package written in Python                                       | package
i  | python3-QtPy                                             | Abstraction layer on top of Qt bindings                                               | package
i  | python3-SQLAlchemy                                       | Database Abstraction Library                                                          | package
etc...

我想我只安装了 python 3.6,但我看到的唯一建议是安装了多个版本。

关于我现在应该测试什么来找到这个问题的根源有什么建议吗?

我的环境:


uname -a

Linux localhost.localdomain 5.3.18-150300.59.49-preempt #1 SMP PREEMPT Mon Feb 7 14:40:20 UTC 2022 (77d9d02) x86_64 x86_64 x86_64 GNU/Linux


python

-bash: /usr/bin/python: No such file or directory


python3

Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>(然后从 python 控制台退出)


echo $PYTHONHOME returns 没什么

echo $PYTHONPATH returns 没什么


我随后重新安装了 OS 并没有不同的结果(令我惊讶)并且我已经尝试 strace -o pythonstrace.txt python3.6 strace 输出的(大概)相关部分是:

munmap(0x7f780442e000, 262144)          = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f780442e000
fstat(0, {st_mode=S_IFCHR|0600, st_rdev=makedev(0x88, 0x7), ...}) = 0
stat("/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/local/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/root/bin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/local/bin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/python3.6", {st_mode=S_IFREG|0755, st_size=10472, ...}) = 0
readlink("/usr/bin/python3.6", 0x7ffff6517fa0, 4096) = -1 EINVAL (Invalid argument)
openat(AT_FDCWD, "/usr/bin/pyvenv.cfg", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/pyvenv.cfg", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/bin/Modules/Setup", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/os.py", 0x7ffff6518f40) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/os.pyc", 0x7ffff6518f40) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/os.py", {st_mode=S_IFREG|0644, st_size=37526, ...}) = 0
stat("/usr/bin/pybuilddir.txt", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/lib-dynload", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/lib-dynload", {st_mode=S_IFDIR|0755, st_size=4802, ...}) = 0
brk(0x55a4968ff000)                     = 0x55a4968ff000
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ee000
sysinfo({uptime=132115, loads=[4256, 3872, 448], totalram=16583553024, freeram=1984724992, sharedram=68530176, bufferram=122511360, totalswap=2146430976, freeswap=2146430976, procs=990, totalhigh=0, freehigh=0, mem_unit=1}) = 0
brk(0x55a496921000)                     = 0x55a496921000
sigaltstack({ss_sp=0x55a4968d9c20, ss_flags=0, ss_size=8192}, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
stat("/usr/lib/python36.zip", 0x7ffff6529bd0) = -1 ENOENT (No such file or directory)
stat("/usr/lib", {st_mode=S_IFDIR|0755, st_size=10808, ...}) = 0
stat("/usr/lib/python36.zip", 0x7ffff65293b0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
getdents64(3, /* 204 entries */, 32768) = 6800
getdents64(3, /* 0 entries */, 32768)   = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings/__init__.cpython-36m-x86_64-linux-gnu.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.abi3.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ae000
stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/__init__.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fstat(3, {st_mode=S_IFREG|0644, st_size=3922, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=3922, ...}) = 0
read(3, "3\r\r\n@*3a\n[=12=][=12=]3[=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=]@[=12=][=12=]"..., 3923) = 3922
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6/codecs.py", {st_mode=S_IFREG|0644, st_size=36276, ...}) = 0
stat("/usr/lib64/python3.6/codecs.py", {st_mode=S_IFREG|0644, st_size=36276, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/__pycache__/codecs.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=33902, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=33902, ...}) = 0
read(3, "3\r\r\n@*3a45[=12=][=12=]3[=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=]:[=12=][=12=][=12=]@[=12=][=12=]"..., 33903) = 33902
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
getdents64(3, /* 128 entries */, 32768) = 4336
getdents64(3, /* 0 entries */, 32768)   = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings/aliases.py", {st_mode=S_IFREG|0644, st_size=15577, ...}) = 0
stat("/usr/lib64/python3.6/encodings/aliases.py", {st_mode=S_IFREG|0644, st_size=15577, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/aliases.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=6266, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=6266, ...}) = 0
read(3, "3\r\r\n@*3a1<[=12=][=12=]3[=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=][=12=]D[=12=][=12=]@[=12=][=12=]"..., 6267) = 6266
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings/utf_8.py", {st_mode=S_IFREG|0644, st_size=1005, ...}) = 0
stat("/usr/lib64/python3.6/encodings/utf_8.py", {st_mode=S_IFREG|0644, st_size=1005, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/utf_8.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1584, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0

这似乎表明它无法从各种不同的目录中找到一大堆文件,而且它们甚至都不在一个公共目录分支中!精选示例:/sbin/python3.6/usr/sbin/python3.6/usr/local/sbin/python3.6/usr/local/bin/python3.6/usr/lib64/python3.6

刚刚也在我家试过 bash 提示:

me@myhost:~> python
ImportError: No module named site
me@myhost:~> python2
ImportError: No module named site
me@myhost:~> python2.7
ImportError: No module named site
me@myhost:~> python3
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f5e446c0740 (most recent call first):
Aborted (core dumped)
me@myhost:~> python3.6
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f0e8c018740 (most recent call first):
Aborted (core dumped)
greg@TRANQUILITY:~> 

并且作为 root:

HOST:~ # python
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[2]+  Stopped                 python
HOST:~ # python2
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[3]+  Stopped                 python2
HOST:~ # python2.7
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[4]+  Stopped                 python2.7
HOST:~ # python3
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[5]+  Stopped                 python3
HOST:~ # python3.6
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

我检查了 env 并将 export LC_CTYPE=en_GB.UTF-8 添加到 ~/.bashrc(我的,不是 root 的)- 与新终端没有区别

我还使用了'strace python3.6as both me and root and saved the output to two files and then in VSCode diffed them. I don't have the tech knowledge to make sense of it but the 'root' strace is much longer and works, the 'greg' strace is shorter and on line 216, sayswrite(2, "No module named 'encodings'", 27No module named 'encodings') = 27`

作为格雷格 https://www.dropbox.com/s/p55uen19njil5zb/stracePython3.6-greg.lua?dl=0

作为根用户 https://www.dropbox.com/s/dlxu43yeqnb7li2/stracePython3.6-root.lua?dl=0

查看 rootgregstrace 输出,问题似乎很清楚。

对于 root 用户,python 3.6 在 /usr/lib64/python3.6 中查找库。

但是,对于 greg,它只在 /usr/bin/python3 下查找子目录。这不起作用,因为 /usr/bin/python3 是一个文件。

我怀疑用户 gregPYTOHNHOME 错误地设置为 Python 二进制文件的位置,这就是导致问题的原因。

从您的环境中删除 PYTOHNHOME,注销并重新登录。

注意:下面的内容可能找错人了。我将其留作参考。


encodings 模块是 python 标准库的一个(未记录的)部分。它由 locale 模块使用。

根据输出,我怀疑您的 Python 安装已损坏或损坏。尝试 re-installing python.

编辑:

如果强制 re-install 不能解决问题,请检查目录 encodings 是否存在于您的 Python stdlib 目录中,并且所有用户都可以访问。

要找出哪个目录是:

python
>>> import sysconfig
>>> sysconfig.get_path('stdlib')
'/usr/local/lib/python3.9'

检查可访问性:

ls -ld /usr/local/lib/python3.9/encodings
drwxr-xr-x  3 root  wheel  5632 Dec 11 14:34 /usr/local/lib/python3.9/encodings/

为所有用户读取和执行,这样就可以了。

EDIT2:

查看跟踪结果。

我认为丢失的文件不是什么值得担心的事情。鉴于 Python 是一个多平台程序,这只是 Python 寻找文件可以具有的不同 locations/names。

有趣的部分即将结束:

stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ae000

这表明它可以找到目录,并将__init__.py映射到内存中。

编辑3:

由于 Python 2.7 和 3.6 现在都是 end-of-life,我建议删除它们,然后安装 python 3.10。

编辑4:

目录结构取决于您的安装方式Python。 WRT 权限,目录通常具有权限 0755 和文件 0644。传统上,在 UNIX 上,/usr/ 中的文件属于 root,而组因系统而异。 最后一个数字(“其他人”既不是所有者也不是组的许可)可能最相关。

您可以结合使用 findchmod 来修复权限。例如:

find /usr/lib64/python3.6 -type d -exec chmod 755 {} \;
find /usr/lib64/python3.6 -type f -exec chmod 644 {} \;

应该为 Python 树中的文件和目录设置一组良好的权限。