为什么导入我在 python 中的任何库都会出现错误?

Why does importing any library i have in python keep brining up errors?

我正在尝试将 2 个库导入 python,但似乎总是有问题。

我什至尝试导入其他库以查看问题是否与我要使用的特定库有关,但我仍然遇到同样的问题。如果有帮助,我需要使用 Pandas 和 Matplotlib。

我总是输入:

import pandas as pd
import matplotlib as plt

对于 matplotlib,我得到这个错误:

    PS C:\Users\mghaf\Documents\GitHub\Modelling-Cell-Growth-in-CFU> & C:/Users/mghaf/Anaconda3/python.exe c:/Users/mghaf/Desktop/ter/Covid-File-For-CC.py
Traceback (most recent call last):
  File "c:/Users/mghaf/Desktop/ter/Covid-File-For-CC.py", line 1, in <module>
    import matplotlib
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\matplotlib\__init__.py", line 107, in <module>
    from . import cbook, rcsetup
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\matplotlib\cbook\__init__.py", line 28, in <module>
    import numpy as np
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\numpy\__init__.py", line 148, in <module> 
    from . import _distributor_init
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\numpy\_distributor_init.py", line 26, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Users\mghaf\Anaconda3\lib\ctypes\__init__.py", line 381, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

对于 pandas 我得到这个错误:

PS C:\Users\mghaf\Documents\GitHub\Modelling-Cell-Growth-in-CFU> & C:/Users/mghaf/Anaconda3/python.exe c:/Users/mghaf/Desktop/ter/Covid-File-For-CC.py
Traceback (most recent call last):
  File "c:/Users/mghaf/Desktop/ter/Covid-File-For-CC.py", line 1, in <module>
    import pandas
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\pandas\__init__.py", line 11, in <module> 
    __import__(dependency)
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\numpy\__init__.py", line 148, in <module> 
    from . import _distributor_init
  File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\numpy\_distributor_init.py", line 26, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Users\mghaf\Anaconda3\lib\ctypes\__init__.py", line 381, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

我正在使用 VS 代码编写我的代码。我使用 Anaconda 作为我的库的基础。但是,我也多次使用 pip install 和 uninstall 来查看问题是否出自那里。无济于事,它没有用。 python 的设置可能存在一些问题,因为我同时安装了 Anaconda 和 Python V 3.7.x。

如果我需要在我的设置中做任何事情或其他任何事情,那么如果有人能告诉我如何做,我将不胜感激。提前致谢!

这是一个由多个 python 路径引起的问题。详细地说,从您执行的命令中可以看出,您期望的解释器是 C:/Users/mghaf/Anaconda3/python.exe

PS C:\Us ... in-CFU> & C:/Users/mghaf/Anaconda3/python.exe c:/Us ... CC.py

不过,从错误日志中可以看出,它正在使用 C:\Users\mghaf\AppData\Roaming\Python\Python38 中的备用 python,如此处所示

 File "C:\Users\mghaf\AppData\Roaming\Python\Python38\site-packages\pandas\__init__.py", line 11, in <module> 

我建议您从 Windows 机器中删除所有 python 路径,除了与 Anaconda 解释器相关的任何内容。