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

WSL Ubuntu下Python依赖路径配置错误致Numpy导入失败求助

问题:WSL Ubuntu + VS Code中Python依赖(numpy等)导入失败

在WSL Ubuntu环境下的VS Code中配置Python项目依赖,已安装Python,通过pip安装了numpy、matplotlib、PyTorch等包,但运行依赖验证脚本时提示找不到numpy模块。手动在.bashrc中添加export PYTHONPATH=/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages后,出现更复杂的导入错误。

错误信息

hestevenseb@DESKTOP-PJJSAIC:~/stock-prediction-model$ /usr/bin/python3.10 /home/stevenseb/stock-prediction-model/project.py
Traceback (most recent call last):
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/core/__init__.py", line 24, in <module>
    from . import multiarray
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/core/overrides.py", line 8, in <module>
    from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/__init__.py", line 130, in <module>
    from numpy.__config__ import show as show_config
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/__config__.py", line 4, in <module>
    from numpy.core._multiarray_umath import (
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/core/__init__.py", line 50, 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:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.10 from "/usr/bin/python3.10"
  * The NumPy version is: "1.26.2"

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

Original error was: No module named 'numpy.core._multiarray_umath'


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

Traceback (most recent call last):
  File "/home/stevenseb/stock-prediction-model/project.py", line 1, in <module>
    import numpy as np
  File "/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site-packages/numpy/__init__.py", line 135, 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.re

已安装包列表

Package                  Version
------------------------ ----------
aiohttp                  3.9.1
aiosignal                1.3.1
alpha-vantage            2.3.1
async-timeout            4.0.3
attrs                    23.1.0
certifi                  2023.7.22
charset-normalizer       3.3.2
contourpy                1.2.0
cycler                   0.12.1
distlib                  0.3.7
filelock                 3.12.4
fonttools                4.47.0
frozenlist               1.4.1
fsspec                   2023.12.2
idna                     3.6
importlib-resources      6.1.1
Jinja2                   3.1.2
kiwisolver               1.4.5
MarkupSafe               2.1.3
matplotlib               3.8.2
mpmath                   1.3.0
multidict                6.0.4
networkx                 3.2.1
numpy                    1.26.2
nvidia-cublas-cu12       12.1.3.1
nvidia-cuda-cupti-cu12   12.1.105
nvidia-cuda-nvrtc-cu12   12.1.105
nvidia-cuda-runtime-cu12 12.1.105
nvidia-cudnn-cu12        8.9.2.26
nvidia-cufft-cu12        11.0.2.54
nvidia-curand-cu12       10.3.2.106
nvidia-cusolver-cu12     11.4.5.107
nvidia-cusparse-cu12     12.1.0.106
nvidia-nccl-cu12         2.18.1
nvidia-nvjitlink-cu12    12.3.101
nvidia-nvtx-cu12         12.1.105
packaging                23.2
Pillow                   10.1.0
pip                      23.3.2
pipenv                   2023.9.8
platformdirs             3.10.0
pyparsing                3.1.1
python-dateutil          2.8.2
requests                 2.31.0
setuptools               68.2.2
six                      1.16.0
sympy                    1.12
torch                    2.1.2
triton                   2.1.0
typing_extensions        4.9.0
urllib3                  2.1.0
virtualenv               20.24.5
yarl                     1.9.4
zipp                     3.17.0

验证脚本

import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

from alpha_vantage.timeseries import TimeSeries 

print("All libraries loaded")

解决方法

  • 移除手动添加的PYTHONPATH配置
    编辑~/.bashrc,删除之前添加的export PYTHONPATH=...行,执行source ~/.bashrc使更改生效。手动指定PYTHONPATH会导致Python版本与包的版本不匹配(当前调用的是/usr/bin/python3.10,而包安装在pyenv的3.9.4环境下)。

  • 确保使用正确的Python环境

    1. 激活pyenv的3.9.4环境:pyenv activate 3.9.4
    2. 验证当前Python版本:python --version,确认输出为3.9.4
    3. 重新运行脚本:python /home/stevenseb/stock-prediction-model/project.py
  • 检查pip与Python的对应关系
    执行which pip,确认路径指向/home/stevenseb/.pyenv/versions/3.9.4/bin/pip。如果不是,使用pip3.9重新安装依赖:pip3.9 install numpy matplotlib torch alpha-vantage

  • VS Code中配置匹配的解释器
    在VS Code中按Ctrl+Shift+P,输入Python: Select Interpreter,选择pyenv的3.9.4版本,确保编辑器使用的解释器和终端激活的环境一致。

  • 修复numpy安装(若上述步骤无效)
    卸载当前numpy:pip uninstall numpy -y,然后重新安装适配Python3.9的numpy版本:pip install numpy==1.26.2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:30:54