当我尝试在 GCP 上导入 NumPy 库时,如何修复出现的 Python 错误“...无法从共享对象映射段”?

Vin*_*iLL 4 shell numpy python-3.x pandas google-cloud-platform

我最近开始使用 Google Cloud Platform,并在 Linux 环境中的 Cloud Shell 中运行 python 脚本。

通过运行使用 pandas 库的脚本之一,我遇到了非常令人不快的错误:

    Traceback (most recent call last):
  File "pandas_excercises.py", line 1, in <module>
    import pandas as pd
  File "/home/dann_frol/.local/lib64/python3.6/site-packages/pandas/__init__.py", line 17, in <module>
    "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
was
installed.
We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
  * The Python version is: Python3.6 from "/usr/bin/python3"
  * The NumPy version is: "1.19.0"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: /home/dann_frol/.local/lib64/python3.6/site-packages/numpy/core/_multiarray_umath.cpython-36m-x86_64-linux-gnu.so: failed to map segment from shared object
Run Code Online (Sandbox Code Playgroud)

我检查了 python 和 numpy 版本,它们似乎与错误消息中描述的版本匹配。我尝试使用python3 -m pip uninstall pandasand重新安装 pandas 和 numpy python3 -m pip install pandas,但是没有帮助。

当我运行代码时,我使用以下命令:python3 pandas_excercises.py

有关操作系统的一些信息:

  Operating System: Container-Optimized OS from Google
            Kernel: Linux 4.19.114+
      Architecture: x86-64
Run Code Online (Sandbox Code Playgroud)

可能的问题是什么以及如何修复此错误?

谢谢,如有任何帮助,我们将不胜感激。

Evg*_*nZh 14

我非常确定,对于 Python 库的本机,该消息failed to map segment from shared object意味着该.so文件未标记为可执行。(ls -l说例如-rw-r--r--而不是-rwxr-xr-x。)

(我能想到的)在理智之后的唯一方法pip install是如果文件系统安装了noexec标志。(我的设置就是这种情况。)您可以使用mount命令快速检查它,如果设置了它,它将列在大括号内的其他标志中。

如何删除该noexec标志是另一个问题。

  • 是的,从 /tmp 构建并需要使用 exec 链接到共享库的 python 超出了不使用 exec 权限挂载 /tmp 的安全最佳实践。快速临时修复是“sudo mount -o remount,exec /tmp”,或者使更改永久保存在“/etc/fstab”中。 (3认同)