简体   繁体   English

没有名为“numpy”的模块:Visual Studio Code

[英]No module named 'numpy': Visual Studio Code

I'm trying to setup Visual Studio Code for python development我正在尝试为 python 开发设置 Visual Studio 代码

to begin with, I've installed首先,我已经安装了

  1. Anaconda Python Anaconda Python
  2. Visual Studio Code视觉工作室代码

and in a new file I have the following code在一个新文件中我有以下代码

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

upon hitting Ctrl+Shift+BI get the following error按下 Ctrl+Shift+BI 时出现以下错误

import numpy as np

ImportError: No module named 'numpy' ImportError:没有名为“numpy”的模块

Also, is there python interactive window in VS Code?另外,VS Code 中是否有 python 交互式 window ? How to open it.如何打开它。

Changing python environment in VS code helped me.在 VS 代码中更改 python 环境对我有帮助。 Default the visual studio code takes original Python environment, it requires numpy to install.默认 Visual Studio 代码采用原始 Python 环境,它需要安装 numpy。 If you have anaconda python (numpy comes with it) installed, you could switch the original python environment to anaconda python environment in visuals studio code.如果你安装了 anaconda python(numpy 自带),你可以在 visuals studio 代码中将原来的 python 环境切换到 anaconda python 环境。 This can be done from the command palette Ctrl+Shift+P in visual studio这可以从 Visual Studio 中的命令面板Ctrl+Shift+P完成

Check this link for how to switch from original python to anaconda python environment, specifically:检查此链接以了解如何从原始 python 切换到 anaconda python 环境,特别是:

来自 VSCode 指令的片段 在此处输入图像描述

You may not have numpy installed on the version of python you are running.您可能没有在您正在运行的 python 版本上安装 numpy。

Try this:尝试这个:

import sys导入系统

print(sys.version)打印(系统版本)

Is the printed version Anaconda?是印刷版Anaconda吗? If you installed Anaconda python, it should come with numpy already installed.如果你安装了 Anaconda python,它应该已经安装了 numpy。 If it turns out to be another version of python you are accessing inside Visual Studio Code that doesn't have numpy installed, then that's what you need to fix.如果结果是您在没有安装 numpy 的 Visual Studio Code 中访问的另一个 python 版本,那么这就是您需要修复的问题。

The version of python that is called depends on which version of python comes up in your PATH variable first.调用的 python 版本取决于 PATH 变量中首先出现的 python 版本。 Type into a terminal: echo $PATH .在终端中输入: echo $PATH The output should look like this with Anaconda bin first: /Users/jlzhang/anaconda/bin:/usr/local/bin:/usr/bin:/bin首先使用 Anaconda bin 的输出应如下所示: /Users/jlzhang/anaconda/bin:/usr/local/bin:/usr/bin:/bin

If you do not have Anaconda bin first, you can add this to your ~/.bashrc file: echo如果您没有先安装 Anaconda bin,您可以将其添加到您的 ~/.bashrc 文件中:echo

# Use Anaconda python # 使用 Anaconda 蟒蛇

export PATH="/Users/jlzhang/anaconda/bin:$PATH"导出 PATH="/Users/jlzhang/anaconda/bin:$PATH"

Restart a terminal and Visual Studio Code and see if you are now running Anaconda python.重新启动终端和 Visual Studio Code 并查看您现在是否正在运行 Anaconda python。

Hope it helps/ Did it work?希望它有帮助/它有效吗?

选择合适的版本

click on python and select the appropriate version.单击python并选择适当的版本。 your issue will be solved你的问题将得到解决

You have to make sure VSCode selects the python interpreter bundled with Anaconda.您必须确保 VSCode 选择与 Anaconda 捆绑的 python 解释器。 If using Anaconda, please do not pip install your science packages.如果使用 Anaconda,请不要pip install你的科学包。

The solution is as follows for me with Anaconda:使用 Anaconda 的解决方案如下:

  1. To select a specific environment, use the Python: Select Interpreter command from the Command Palette ( Ctrl+Shift+P ).要选择特定环境,请使用Python:从命令面板中选择解释器命令 ( Ctrl+Shift+P )。

在此处输入图像描述

  1. Select python bundled by Anaconda选择 Anaconda 捆绑的 python 选择正确的解释器..

Numpy, scipy, and the like should now no longer have a warning! Numpy、scipy 等现在应该不再有警告了!

We've found that simply changing the environment worked.我们发现简单地改变环境是有效的。 Right-click on "Python Environments" and choose "Add/Remove Python Environments" and choose 3.5 instead of 3.6:右键单击“Python 环境”并选择“添加/删除 Python 环境”并选择 3.5 而不是 3.6:

添加/删除 Python 环境

if you are at VSC jupyter, you should check this:如果你在 VSC jupyter,你应该检查一下:

Like this (You can see this at the top right)像这样(你可以在右上角看到这个)

Choose Python in the dropdown(default is PowerShell) and execute pip install NumPy pandas etc.在下拉列表中选择 Python(默认为 PowerShell)并执行 pip install NumPy pandas 等。

Dropdown落下

If you are using jupiter notebook in VS Code, you need to select proper evvironmemt for it:如果你在 VS Code 中使用 jupiter notebook,你需要为它选择合适的 evvironmemt: 在此处输入图像描述

On my laptop, i found that there are some version of python installed (checkout picture that i inserted below)在我的笔记本电脑上,我发现安装了一些版本的python(我在下面插入的结帐图片)

Picture for some version of python installed安装了某些版本的python的图片

One of them is python 3.7.6 which installed together when i installed Anaconda (I installed it the day before).其中之一是我安装 Anaconda 时一起安装的 python 3.7.6(我在前一天安装​​了它)。 I assumed that it was the updated version of python.我认为这是python的更新版本。 So i changed my Jupyter kernel to that version of Python.所以我将我的 Jupyter 内核更改为那个版本的 Python。 It works for me.这个对我有用。

you can do something else in cmd type :您可以在 cmd 类型中执行其他操作:

cmd) where pip cmd) 在哪里点

and cmd return some address after you should install NumPy for all addresses and make sure installed NumPy in all them ->并且 cmd 在您应该为所有地址安装 NumPy 并确保在所有地址中安装 NumPy 后返回一些地址 ->

cmd) ADDR1 install numpy cmd) ADDR1 安装 numpy

cmd) ADDR2 install numpy cmd) ADDR2 安装 numpy

... and all addresses ...以及所有地址

如果您尝试过 pip install numpy 但它不起作用,请将其复制粘贴到您的 cmd

pip install numpy  --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org

I'm my case I was receiving a message in the integrated terminal of Visual Studio Code:我是我的情况,我在 Visual Studio Code 的集成终端中收到一条消息:

ModuleNotFoundError: No module named 'scipy'

But the terminal was showing that scipy was installed:但终端显示 scipy 已安装:

Requirement already satisfied: scipy in ./opt/anaconda3/lib/python3.9/site-packages (1.7.1)

I discover that is because the vscode integrated terminal was using the default python version installed before Anaconda.我发现这是因为 vscode 集成终端使用的是在 Anaconda 之前安装的默认 python 版本。 The steps provided in others answers, like change the interpreter, did not solved my problem.其他答案中提供的步骤,例如更改解释器,并没有解决我的问题。 So, I just deactivated Anaconda:所以,我刚刚停用了 Anaconda:

conda deactivate

I Installed the desired module in the default python installation:我在默认的 python 安装中安装了所需的模块:

pip install scipy

And then I reactivated conda:然后我重新激活了 conda:

conda activate

After it, everything works now.之后,现在一切正常。

1. Numpy have showing error when importing at VS CODE 1. Numpy 在 VS CODE 导入时出现错误

When i Run the code it show No module named 'numpy' .Although i check by pip install numpy it show satisfy that its already intsall当我运行代码时,它显示没有名为“numpy”的模块。虽然我通过 pip install numpy 检查它显示满足它已经 intsall

Then i found solution 1.Press Ctrl+shift+P 2.type python interpreter and click on it.然后我找到了解决方案1.按 Ctrl+shift+P 2.键入 python 解释器并单击它。 3.Then choose The Global interpreter shown in image attached. 3.然后选择所附图像中显示的全局解释器。 4.again run the code no error. 4.再次运行代码没有错误。 No Error,Done 100%没有错误,完成 100%

Even after conda(Anaconda) is installed and the anaconda version of python is selected(using ctrl+shift+p and selecting Python: Select interpreter ) on the VS code python extension interpreter the version of the kernel used to run the notebook can be different. Even after conda(Anaconda) is installed and the anaconda version of python is selected(using ctrl+shift+p and selecting Python: Select interpreter ) on the VS code python extension interpreter the version of the kernel used to run the notebook can be different . Below is when the default installation of python is selected on the top right corner of VS code:下面是VS code右上角选择python默认安装时的情况: 默认 python 内核找不到 Numpy 模块But when the anaconda version of python is selected on the top right corner of VS code, the numpy module is imported and runs(look for the currently selected one on the top menu).但是当VS代码右上角选择anaconda版本python时,numpy模块被导入并运行(在顶部菜单中查找当前选择的那个)。 在此处输入图像描述

Note after installing the python extension on VS code and selecting the python interpreter, it will automatically load and show you which interpreters you have on your system to choose from.请注意,在 VS 代码上安装 python 扩展并选择 python 解释器后,它将自动加载并显示您系统上有哪些解释器可供选择。

I used to get the same error.我曾经得到同样的错误。

if you installed python and pip then run this code on the vs code terminal 'pip install numpy'.如果您安装了 python 和 pip,则在 vs code 终端“pip install numpy”上运行此代码。

您应该尝试通过“pip install numpy”这一行代码使用终端和 cmd 在 vs 代码中安装 numpy 它在我的情况下有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “没有名为 PIL 的模块”Visual Studio 代码错误 - 'No module named PIL' Visual Studio Code Error moduleNotFoundError:没有在 Visual Studio 代码中命名的模块 - moduleNotFoundError: No module named in visual studio code Visual Studio Code ModuleNotFoundError:没有名为“_overlapped”的模块 - Visual Studio Code ModuleNotFoundError: No module named '_overlapped' ModuleNotFoundError:Visual Studio Code 中没有名为“pyperclip”的模块 - ModuleNotFoundError: No module named 'pyperclip' in Visual Studio Code Visual Studio 代码中没有名为“dotenv”的模块 - no module named "dotenv" in visual studio code ModuleNotFoundError:Visual Studio Code 中没有名为“bs4”的模块 - ModuleNotFoundError: No module named 'bs4' just in Visual Studio Code Visual Studio 代码上的“没有名为 pandas 的模块”错误 - jupyter notebook 上没有问题 - "No module named pandas" error on visual studio code - NO ISSUE on jupyter notebook ModuleNotFoundError:在 Visual Studio 代码中没有我的自定义类型名为“xxx”的模块 - ModuleNotFoundError: No module named 'xxx' with my custom type in Visual Studio code ModuleNotFoundError:使用 Visual Studio Code 在 Python 中没有名为“PySide6”的模块 - ModuleNotFoundError: No module named 'PySide6' in Python using Visual Studio Code Visual Studio 代码窗口,Python Pandas。 没有名为 pandas 的模块 - Visual Studio Code windows , Python Pandas . No module named pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM