VS Code:ModuleNotFoundError:没有名为“pandas”的模块

nil*_*ore 19 python visual-studio-code

pandas尝试在 VS Code 中导入

import pandas
Run Code Online (Sandbox Code Playgroud)

并得到了

Traceback (most recent call last):
  File "c:\Users\xxxx\hello\sqltest.py", line 2, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'
Run Code Online (Sandbox Code Playgroud)

pandas尝试安装

pip install pandas

pip3 install pandas

python -m pip install pandas
Run Code Online (Sandbox Code Playgroud)

分别返回的

(.venv) PS C:\Users\xxxx\hello> pip3 install pandas

Requirement already satisfied: pandas in c:\users\xxxx\hello\.venv\lib\site-packages (1.1.0)
Requirement already satisfied: pytz>=2017.2 in c:\users\xxxx\hello\.venv\lib\site-packages (from pandas) (2020.1)
Requirement already satisfied: numpy>=1.15.4 in c:\users\xxxx\hello\.venv\lib\site-packages (from pandas) (1.19.1)
Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\xxxx\hello\.venv\lib\site-packages (from pandas) (2.8.1)
Requirement already satisfied: six>=1.5 in c:\users\xxxx\hello\.venv\lib\site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)
Run Code Online (Sandbox Code Playgroud)

尝试过:

sudo pip install pandas
Run Code Online (Sandbox Code Playgroud)

并得到了

(.venv) PS C:\Users\xxxx\hello> sudo pip install pandas

sudo : The term 'sudo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ sudo pip install pandas
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (sudo:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我还尝试按照此答案更改工作区设置下的 python 路径。其中C:\Users\xxxx\AppData\Local\Microsoft\WindowsApps\python.exe是我在命令提示符中找到的 python 路径,where python但没有工作。

然后我尝试了

python -m venv .venv
Run Code Online (Sandbox Code Playgroud)

返回的

(.venv) PS C:\Users\xxxx\hello> python -m venv .venv

Error: [Errno 13] Permission denied: 'C:\\Users\\xxxx\\hello\\.venv\\Scripts\\python.exe'
Run Code Online (Sandbox Code Playgroud)

更新:

尝试过

python3.8.5 -m pip install pandas
Run Code Online (Sandbox Code Playgroud)

并返回

(.venv) PS C:\Users\xxxx\hello> python3.8.5 -m pip install pandas

python3.8.5 : The term 'python3.8.5' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python3.8.5 -m pip install pandas
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (python3.8.5:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

小智 23

这比我们想象的要容易:

在此输入图像描述

这张图解释了如何解决这个问题。

  • 在最新版本的vscode中,移至右上角 (6认同)

Gri*_*ret 5

  1. 从此链接下载 anaconda 解释器
  2. 安装后,打开anaconda提示符(anaconda3)并执行此代码conda install ipykernel。它将安装所有必需的软件包。
  3. 重新启动 vs code 并将解释器更改为基础 conda 和 voala!


Lea*_*one 5

问题(至少在我的情况下)是我在默认 Python 版本下安装了一个包,但我在 Visual Studio Code (VS Code) 中为不同的 Python 版本设置了解释器。有 2 个选项可以解决此问题。

  1. 更改 VS Code 解释器:VS Code -> 查看 -> 命令面板... (Ctrl+Shift+P) -> Python: 选择解释器 -> 选择“Python: 选择解释器”(或 Enter) -> 选择基于解释器在我们选择的 Python 版本上安装了该软件包。
  2. 在正确的Python版本下安装包,这意味着更改默认的Python版本并再次重复安装过程。要更改默认的 Python 版本(适用于 Windows 10):右键单击此电脑 -> 属性 -> 高级系统设置(在右侧面板中) -> 环境变量 -> 系统变量(窗口底部) -> 双-单击“路径”-> 选择所需 Python 版本的第一行并将其向上移动,然后对第二行执行相同的操作。如果您想查看/使用新的默认 Python 版本,我建议您重新启动(关闭并再次打开)命令提示符会话。

安装注意事项:以下命令(在命令提示符中)对我有用:pip3 install pandas --user


小智 1

解决方案看起来相当简单!首先,先做事!

从您的帖子来看,您似乎已经按照指南安装了Pandas. 这没有什么问题,但我必须首先指出,根据您提供给我们的信息,您似乎运行Windows Powershell PS C:\Users\xxxx\hello>the error format matches Powershell. 因此,sudo无法识别,因为它是类似系统sudo的管理命令,这就是为什么它不是有效命令!Unix-basedDebian, Ubuntu, and so on

但正确安装的方法如下:(我假设您运行的是 Windows,但如果情况并非如此,请纠正我,我会给您 Unix 版本!)

1 - Windows 密钥,搜索CMD并运行它,因为administrator这对于避免权限问题很重要!

2 - 运行pip3 install pandas python3 -m pip3 install pandas