You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 24.04虚拟环境pip异常及NumPy导入失败问题解决

Ubuntu 24.04 + Python3.12虚拟环境pip异常及NumPy导入失败解决方法

问题现象

在Ubuntu 24.04(预装Python3.12)环境下,创建并激活Python虚拟环境后,出现以下异常:

  • 执行pip install <package_name>时,系统调用全局pip而非虚拟环境本地pip
  • 直接调用虚拟环境内的pip提示权限拒绝
  • 改用python -m pip install numpy成功安装后,导入NumPy时触发failed to map segment from shared object错误

操作与报错详情

  1. 创建虚拟环境:
python3 -m venv .venv
  1. 激活虚拟环境:
source ./.venv/bin/activate
  1. 路径检查结果:
(.venv) $ which pip
/usr/bin/pip
(.venv) $ which python
/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/test/.venv/bin/python
  1. 直接调用虚拟环境pip报错:
(.venv) $ ./.venv/bin/pip install numpy
bash: ./.venv/bin/pip: Permission denied
  1. NumPy导入完整报错:
(.venv) ams@ams-Alienware-m17-R3:/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts$ python run_prediction.py 
Traceback (most recent call last):
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/_core/__init__.py", line 23, in <module>
    from . import multiarray
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/_core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/_core/overrides.py", line 7, in <module>
    from numpy._core._multiarray_umath import (
ImportError: /mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/_core/_multiarray_umath.cpython-312-x86_64-linux-gnu.so: failed to map segment from shared object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/__init__.py", line 114, in <module>
    from numpy.__config__ import show_config
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/__config__.py", line 4, in <module>
    from numpy._core._multiarray_umath import (
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/_core/__init__.py", line 49, in <module>
    raise ImportError(msg)
ImportError: 

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
installed.

We have compiled some common reasons and troubleshooting tips at:

    [外部链接已移除]

Please note and check the following:

  * The Python version is: Python3.12 from "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/bin/python"
  * The NumPy version is: "2.2.6"

and make sure that they are the versions you expect.
Please carefully study the relevant documentation for further help.

Original error was: /mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/_core/_multiarray_umath.cpython-312-x86_64-linux-gnu.so: failed to map segment from shared object


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/run_prediction.py", line 2, in <module>
    import numpy as np
  File "/mnt/DATA/AUC/Research Assistant/Pedestrian-Estimation-Dataset-Annotation/Scripts/.venv/lib/python3.12/site-packages/numpy/__init__.py", line 119, in <module>
    raise ImportError(msg) from e
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

排查与解决步骤

  1. 定位根因:检查磁盘挂载参数,发现存放项目的NTFS格式SSD挂载时带有noexec(禁止执行)参数:
mount | grep -F /mnt

输出:

(rw,nosuid,nodev,noexec,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096,user,x-gvfs-show)
  1. 修改挂载配置:编辑/etc/fstab文件,将对应分区的挂载参数中的noexec替换为exec:
    原配置行:
UUID=349264BD926484E8 /mnt/DATA  auto rw,user,uid=1000,gid=1000,dmask=0002,fmask=0002,nosuid,nodev,nofail,x-gvfs-show 0 0

修改后:

UUID=349264BD926484E8 /mnt/DATA  auto rw,user,exec,uid=1000,gid=1000,dmask=0002,fmask=0002,nosuid,nodev,nofail,x-gvfs-show 0 0
  1. 生效配置:执行命令重新挂载分区(或重启系统):
sudo umount /mnt/DATA && sudo mount /mnt/DATA

完成以上操作后,虚拟环境的pip可正常调用,NumPy导入异常也会消失。


内容的提问来源于stack exchange,提问作者abdo Salm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:47:02