安装了numpy和其他依赖项,但似乎无法正确设置路径

问题描述 投票:0回答:1

我第一次在 VS code 中安装 python 的依赖项。之前已经安装了Python。我在 WSL 上使用 Ubuntu,我的文件结构位于 Ubuntu 的该文件夹中。我为我想做的一个指导项目安装了 numpy、matplotlib 和 PyTorch。安装所有软件包后,一切似乎都很好,但是项目中用于验证依赖项是否正确设置的文件会导致找不到模块(导入 numpy as np)的错误。在搜索 numpy 文档时,我发现我可能需要在 .bashrc 文件中添加一行,我添加了以下内容:export PYTHONPATH=/home/stevenseb/.pyenv/versions/3.9.4/lib/python3.9/site -packages ....进行此更改后,我收到了一个更详细的错误,我无法找到有用的文档。 (下面我发布了已安装软件包的列表)

type 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

pip install 给了我这个:

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")

我已经使用 pip 成功安装了 python 包,但是环境似乎无法访问这些包。我更新了 .bashrc 中的 PATH,但仍然遇到问题。

python-3.x numpy pip dependencies
1个回答
0
投票

我自己找到了答案,我会在这里发布,以防将来有人遇到这个问题。看来我是在全局安装依赖项,而不是创建要在其中安装的环境。此链接对此进行了解释:https://code.visualstudio.com/docs/python/python-tutorial一旦我按照步骤使用 .venv 创建环境,我就能够安装依赖项并且它们通过了验证.

© www.soinside.com 2019 - 2024. All rights reserved.