没有名为“numpy”的模块;对于 Mac OS,在 Visual Studio 代码中使用 pyenv
我的 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?..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题之一取决于您运行代码的方式。
在 VS Code 中,您必须确保它使用通过 pyenv 设置的正确 Python 环境。通常,除非配置为使用另一个解释器,否则它将使用全局 Python 解释器运行。
来自文档:
您可以在此处阅读有关如何在 VS Code 中正确设置它的信息。
另一个问题可能是您的 pip 不是为环境安装的,而是全局安装的。换句话说,您正在正确的环境中运行,但 pip 不正确。
在这种情况下,您可以运行:
它将使用链接到您当前使用的 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:
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:
Which will use
pip
that is linked to the python environment you are currently using.Hopefully that helps!
您使用的解释器是,
但您假设您正在使用
mlp
env 并且也安装到该 env:因此您的 Visual Studio 代码未正确设置,因为它应该使用
The interpreter you use is
but you are under the assumption that you are using the
mlp
env and have also installed to that env:So your visual studio code is not set up correctly, as it should be using
这里必须注意的几点
conda
python/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
conda
python/opt/homebrew/Caskroom/miniforge/base/bin/python
shows that this is Python installed fromHomebrew
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.您是否正在使用
conda run
来执行 python 文件,不是吗?像这样:这是Python扩展的新更新:
变更日志(2022 年 2 月 28 日)。
但正如您所发现的,它不会利用Anaconda3下的子环境。它使用的是基础环境。如果你在终端中像
python pythonFileName.py
一样直接执行它,你会发现它有效。看起来像是Python Extension更新后的问题,我已经在GitHub上提交了bug,你可以参考 此处了解更多详细信息。
更新:
解决方法:
conda run
)。原因:
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:It's the new update of Python Extension:
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:
conda run
).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
非常感谢您的帮助!
我现在使用本指南解决了问题:
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!
python -m pip install numpy
这对我有用
python -m pip install numpy
this is working for me