为什么 NumPy 和 pandas 包在第一次设置 Anaconda 提供的 Python 解释器时总是有问题

Why NumPy and pandas packages are always problematic when setting up first time Python interpreter as Anaconda provided

下面是尝试 NumPy 的简单代码行

import numpy as np

my_list = [1, 2, 3]
print(my_list)
print(type(my_list))  

my_list_arr = np.array(my_list)  # Here we are converting python list to numpy array
print(my_list_arr)
print(type(my_list_arr))

我的问题是:当我使用 Jupyter-Notebook 执行上面的代码行时,它运行良好,没有任何包问题,但是为什么同一行代码在执行时会抛出 NumPy、pandas 和其他与包依赖相关的问题在 PyCharm 作为 .py 文件

仅供参考, 我将 PyCharm 项目设置为;

python 解释器 --> 指向提供的 Anaconda

已创建 PyCharm 虚拟环境 - venv

继承全局站点包 – 是

请帮助我。我厌倦了使用 conda 安装软件包,并且厌倦了在错误一个接一个地出现时一个一个地进行软件包更正。 请有更好的解决方案!!

以下是 Pycharm

的错误
D:\Mytech\IDE_workspace\PycharmProjects\python_anaconda_base_venv_and_global_site_packages\venv\Scripts\python.exe D:/Mytech/IDE_workspace/PycharmProjects/python_anaconda_base_venv_and_global_site_packages/python_for_data_science/my_NumPyArrays_practice.py
C:\Users\DELL\anaconda3\lib\site-packages\numpy\__init__.py:143: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
Traceback (most recent call last):
  File "C:\Users\DELL\anaconda3\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
    from . import multiarray
  File "C:\Users\DELL\anaconda3\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
    from . import overrides
  File "C:\Users\DELL\anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Mytech\IDE_workspace\PycharmProjects\python_anaconda_base_venv_and_global_site_packages\python_for_data_science\my_NumPyArrays_practice.py", line 1, in <module>
    import numpy as np
  File "C:\Users\DELL\anaconda3\lib\site-packages\numpy\__init__.py", line 145, in <module>
    from . import core
  File "C:\Users\DELL\anaconda3\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "D:\Mytech\IDE_workspace\PycharmProjects\python_anaconda_base_venv_and_global_site_packages\venv\Scripts\python.exe"
  * The NumPy version is: "1.20.3"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.


Process finished with exit code 1

但是当我执行下面的代码时它显示了。它工作。

import sys

print("******************************")
print("This Code using:")
print("Python Version       =", sys.version)
print("Python Interpreter   =", sys.base_prefix)
print("*******************************")

print("*******************************")
This Code using:
Python Version       = 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
Python Interpreter   = C:\Users\DELL\anaconda3
print("*******************************")

Process finished with exit code 0

中所述,inherit-global-site-packages 继承自您的 Global python 安装。这是您的机器附带的安装或“直接”安装到 /usr/bin 或 Windows 等价物中。我认为将解释器指向 Anaconda 不会使其从 Anaconda 继承,因为 Anaconda 不被视为全局 python 安装。

发生的事情是您正在继承一个您可能没有安装任何东西的 python 安装。我可以想到两个解决方案:

  1. global python install (运行ning which python or which python3 在 conda env 之外应该告诉你它在哪里;找到调用全局 python 的命令后,使用该命令进行 pip 安装)

  2. 使用 conda 环境,只需将您的解释器设置为不同的 conda 环境,而不是创建 PyCharm 环境。如果您对此不熟悉,可以运行 conda create -n NAME python=X.XX 其中NAME 是您要命名的环境,X.XX 是版本号。从那里,您可以 conda activate NAME,然后使用 pip 或 conda 安装您的包。