没有名为“numpy”的模块;对于 Mac OS,在 Visual Studio 代码中使用 pyenv

发布于 2025-01-12 07:20:40 字数 905 浏览 0 评论 0 原文

我的 numpy 有问题。

当我尝试在 Visual Studio 中运行 main.py 文件时,我收到此消息。

import numpy as np
ModuleNotFoundError: No module named 'numpy'
ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', '/Users/Bruker/Documents/Prosjektoppgave/PPO/main.py']' command failed. 

(请参阅上面的错误)

我已经创建了一个虚拟环境,并且正在使用 3.8.12 ('mlp': conda) 这也是我在终端中所处的环境。

我正在使用 Macbook 并尝试使用 Visual Studio 代码中的张量流,但代码停在 import numpy as np 处。

如果我运行 pip install numpy,我会收到以下消息:

Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages (1.22.2)

如果我运行,

import sys
print("version: ", sys.executable)

我会收到

version:  /opt/homebrew/Caskroom/miniforge/base/bin/python

What should I do?..

I have a problem with numpy.

I receive this when I try to run my main.py file in visual studio.

import numpy as np
ModuleNotFoundError: No module named 'numpy'
ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', '/Users/Bruker/Documents/Prosjektoppgave/PPO/main.py']' command failed. 

(See above for error)

I have created a virtual environment and I am using 3.8.12 ('mlp': conda) which is also the environment I am in with the terminal.

I am using a Macbook and trying to use tensorflow from visual studio code, but the code stops at import numpy as np.

If I run pip install numpyI receive the following message:

Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages (1.22.2)

And if I run

import sys
print("version: ", sys.executable)

I receive

version:  /opt/homebrew/Caskroom/miniforge/base/bin/python

What should I do?..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

以为你会在 2025-01-19 07:20:40

问题之一取决于您运行代码的方式。
在 VS Code 中,您必须确保它使用通过 pyenv 设置的正确 Python 环境。通常,除非配置为使用另一个解释器,否则它将使用全局 Python 解释器运行。

来自文档:

全局和虚拟环境#
默认情况下,您安装的任何 Python 解释器都在其自己的全局环境中运行,该环境不特定于任何一个项目。

您可以在此处阅读有关如何在 VS Code 中正确设置它的信息。

另一个问题可能是您的 pip 不是为环境安装的,而是全局安装的。换句话说,您正在正确的环境中运行,但 pip 不正确。
在这种情况下,您可以运行:

python -m pip install numpy

它将使用链接到您当前使用的 python 环境的 pip

希望这有帮助!

One of the issues depends on how you are running the code.
In VS Code you have to ensure that it is using the correct environment Python you have set up with pyenv. Typically, it will run with the global Python interpreter unless configured to use another.

From the docs:

Global and virtual environments#
By default, any Python interpreter that you've installed runs in its own global environment, which is not specific to any one project.

You can read here about how to correctly set it up in VS Code.

Another issue may be that your pip is not installing it for the environment but globally. In other words, you are running with the correct environment but incorrect pip.
In that case you can run:

python -m pip install numpy

Which will use pip that is linked to the python environment you are currently using.

Hopefully that helps!

自由范儿 2025-01-19 07:20:40

您使用的解释器是,

/opt/homebrew/Caskroom/miniforge/base/bin/python

但您假设您正在使用 mlp env 并且也安装到该 env:

Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages (1.22.2)

因此您的 Visual Studio 代码未正确设置,因为它应该使用

/opt/homebrew/Caskroom/miniforge/base/envs/mlp/bin/python

The interpreter you use is

/opt/homebrew/Caskroom/miniforge/base/bin/python

but you are under the assumption that you are using the mlp env and have also installed to that env:

Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages (1.22.2)

So your visual studio code is not set up correctly, as it should be using

/opt/homebrew/Caskroom/miniforge/base/envs/mlp/bin/python
拥有 2025-01-19 07:20:40

这里必须注意的几点

  1. 您说您使用 conda python
  2. /opt/homebrew/Caskroom/miniforge/base/bin/python 表明这是从 安装的 Python Homebrew

这可能是您正在处理的不缺少 numpy 的根本原因问题。在 Mac 中,坚持使用一个 python 版本非常重要。请重新配置您的 Python 路径。

对于您的 conda 环境,您可以检查 https://docs .anaconda.com/anaconda/user-guide/tasks/integration/python-path/

建议: 下载Anaconda 包 并安装它。

由于您正在使用 TensorFlow,我建议您只使用 Conda 更新,而不是使用任何 pip install 来更好地进行包依赖性检查和正确计划的包更新。

Few points you must note here

  1. You said you using conda python
  2. /opt/homebrew/Caskroom/miniforge/base/bin/python shows that this is Python installed from Homebrew

This could be the root cause issue you are dealing with not missing numpy. In Mac, it is very important to stick to one python version. Please reconfigure your Python Paths.

For your conda environment, you can check https://docs.anaconda.com/anaconda/user-guide/tasks/integration/python-path/

Suggestion: Download Anaconda Package and install it.

Since you are working with TensorFlow, I would recommend you to stick to only Conda updates, than using any pip install for better package dependency checks and properly planned package updates.

此生挚爱伱 2025-01-19 07:20:40

您是否正在使用 conda run 来执行 python 文件,不是吗?像这样:

conda run -n env01 --no-capture-output --live-stream python c:/Work/python3.10/hello/a.py

这是Python扩展的新更新:

使用 conda run 作为 conda 环境来运行 python 文件和
安装模块。 (#18479)

变更日志(2022 年 2 月 28 日)。

但正如您所发现的,它不会利用Anaconda3下的子环境。它使用的是基础环境。如果你在终端中像python pythonFileName.py一样直接执行它,你会发现它有效。

看起来像是Python Extension更新后的问题,我已经在GitHub上提交了bug,你可以参考 此处了解更多详细信息。

更新:

解决方法:

  1. 设置“python.terminal”在settings.json 中将.activateEnvironment”设置为 false。
  2. 降级到可以正常工作的以前版本的扩展(避免 conda run )。
  3. 尝试以下 VSIX,它具有潜在的修复功能:https://github.com/microsoft/vscode-python/suites/5578467772/ artifacts/180581906,使用扩展:从 VSIX 命令安装来安装VSIX。

原因:

Conda 有一些问题:

conda run -n MY-ENV python FILE.py 使用基本解释器而不是环境解释器。

使用 conda run inside 运行已激活的环境应与在外部运行相同

conda run 不会删除基本环境$PATH 中的组件

Are you taking conda run to execute the python file, isn't it? like this:

conda run -n env01 --no-capture-output --live-stream python c:/Work/python3.10/hello/a.py

It's the new update of Python Extension:

Use conda run for conda environments for running python files and
installing modules. (#18479)

changelog(28 February 2022).

But as you have found, it will not take advantage of the sub environments under Anaconda3. It's using the base environment. If you execute it directly like python pythonFileName.py in the terminal, you will found it works.

It looks like an issue of Python Extension after the update, I have submitted a bug on GitHub, you can refer to here for more details.

Update:

Workaround:

  1. Set "python.terminal.activateEnvironment" to false in the settings.json.
  2. Downgrade to the previous version of the extension which works fine(avoid conda run).
  3. Try out the following VSIX which has the potential fix: https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906, Use Extension: Install from VSIX command to install the VSIX.

Reason:

Conda has some problems:

conda run -n MY-ENV python FILE.py uses the base interpreter instead of environment interpreter.

Running using conda run inside already activated environment should be the same as running it outside

conda run does not remove base environment components from $PATH

千年*琉璃梦 2025-01-19 07:20:40

非常感谢您的帮助!

我现在使用本指南解决了问题:
https://www.youtube.com/watch?v=_CO-ND1FTOU

首先,我在计算机上卸载了 conda 和 homebrew,然后安装了 miniforge。

至少现在有效!

Thank you very much for the help!

I solved the problem for now using this guide:
https://www.youtube.com/watch?v=_CO-ND1FTOU

Firstly, I uninstalled conda and homebrew on my computer and then installed miniforge.

It works for now at least!

娇纵 2025-01-19 07:20:40

python -m pip install numpy

这对我有用

python -m pip install numpy

this is working for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文