Python Pandas "Error: Could not install packages due to an OSError: No such file or directory:"

Python Pandas "Error: Could not install packages due to an OSError: No such file or directory:"

System:Win10
IDE: VSCode
Python path: C:\Users\user\AppData\Local\Continuum\miniconda3

在尝试通过 VSCode 终端将 pandas 和 geopandas 安装到 运行 Jupyter Notebook 时,我遇到了问题,因此我创建了一个 conda 环境并安装了 pandas 和 geopandas 通过命令提示符,他们成功了。现在 Jupyter Notebook 工作了,我的 pythonfiles.py) 在 VSCode 上找不到 pandas.

> PS C:\Users\user\folder> python pythonfile.py
> Traceback (most recent call last):   File
> "pythonfile.py", line 1, in <module>
>     import pandas as pd ModuleNotFoundError: No module named 'pandas'
> 

当我检查 VSCode 中的“pip list”时,结果:

Package         Version
--------------- -------------------
asn1crypto      0.23.0
certifi         2017.11.5
cffi            1.11.2
chardet         3.0.4
conda           4.3.31
cryptography    2.1.4
idna            2.6
menuinst        1.4.10
numpy           1.19.5
pip             21.0.1
pycosat         0.6.3
pycparser       2.18
pyOpenSSL       17.5.0
PySocks         1.6.7
python-dateutil 2.8.1
pytz            2021.1
pywin32         221
requests        2.18.4
ruamel-yaml     0.11.14
setuptools      36.5.0.post20170921
six             1.11.0
urllib3         1.22
wheel           0.30.0
win-inet-pton   1.0.1
wincertstore    0.2

当我在命令提示符中检查 pip 列表时,结果

(env1) C:\Users\user>pip list
Package          Version
---------------- -------------------
attrs            20.3.0
backcall         0.2.0
certifi          2020.12.5
chardet          4.0.0
click            7.1.2
click-plugins    1.1.1
cligj            0.7.1
colorama         0.4.4
cycler           0.10.0
decorator        4.4.2
Fiona            1.8.18
GDAL             3.2.2
geopandas        0.9.0
idna             2.10
ipykernel        5.5.0
ipython          7.18.1
ipython-genutils 0.2.0
jedi             0.17.2
jupyter-client   6.1.7
jupyter-core     4.6.3
kiwisolver       1.3.1
matplotlib       3.3.4
munch            2.5.0
numpy            1.20.1
pandas           1.2.3
parso            0.7.0
pickleshare      0.7.5
Pillow           8.1.2
pip              21.0.1
prompt-toolkit   3.0.8
Pygments         2.7.1
pyparsing        2.4.7
pyproj           3.0.1
python-dateutil  2.8.1
pytz             2021.1
pywin32          227
pyzmq            19.0.2
requests         2.25.1
setuptools       52.0.0.post20210125
Shapely          1.7.1
six              1.15.0
tornado          6.0.4
traitlets        5.0.5
urllib3          1.26.4
wcwidth          0.2.5
wheel            0.36.2
wincertstore     0.2

似乎 pandas 和 geopandas 安装在环境中,但是,运行宁 pythoncode.py 仍然 returned:

> Traceback (most recent call last):   File
> "C:\Users\user\folder\pythonfile.py", line 1, in <module>
>     import pandas as pd ModuleNotFoundError: No module named 'pandas'
> 

当我 return 到 VSCode 并尝试再次安装 pandas 时,出现以下错误:

> PS C:\Users\user\folder> pip install pandas Collecting pandas  
> Using cached pandas-1.1.5-cp36-cp36m-win_amd64.whl (8.7 MB)
> Requirement already satisfied: numpy>=1.15.4 in
> c:\users\user\appdata\local\continuum\miniconda3\lib\site-packages
> (from pandas) (1.19.5) Requirement already satisfied: pytz>=2017.2 in
> c:\users\user\appdata\local\continuum\miniconda3\lib\site-packages
> (from pandas) (2021.1) Requirement already satisfied:
> python-dateutil>=2.7.3 in
> c:\users\user\appdata\local\continuum\miniconda3\lib\site-packages
> (from pandas) (2.8.1) Requirement already satisfied: six>=1.5 in
> c:\users\user\appdata\local\continuum\miniconda3\lib\site-packages
> (from python-dateutil>=2.7.3->pandas) (1.11.0) ERROR: Could not
> install packages due to an OSError: [Errno 2] No such file or
> directory:'c:\users\user\appdata\local\continuum\miniconda3\lib\site-packages\pytz-2021.1.dist-info\METADATA'

如何让 pandas 和 geopandas 工作?

原因是VS Code内部终端使用的python和你当前在VS Code中select的python环境不一样(左下角显示) VS 代码)。

我注意到你使用的是powershell终端,它在VS Code中默认不会自动激活conda环境

  1. 请selectVS Code中的conda环境,它会显示在VS Code的左下角。 (F1, Python: Select Interpreter)

  2. 请在 VS Code 终端中输入“Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process”,然后重新打开 VS Code 终端(Ctrl+Shift+`)。使用命令“python --version”时,显示的python版本与左下角显示的版本相同

  3. 如果本环境没有安装该模块,请自行安装。 (pip install pandas)

更新:

另外,请尝试使用其他终端,例如'cmd'终端。 Can I use Cmder's shell with the terminal on Windows?

参考:Python environments in VS Code.