首页 > 解决方案 > ModuleNotFoundError:没有名为“pandas”的模块尝试了从互联网上找到的所有内容

问题描述

一旦我在 py 文件中运行代码,例如

a.py在里面: import pandas as pd ......其他代码

然后运行它:

gunicorn main:app -b 0.0.0.0:7000

它因以下错误而崩溃:

$ gunicorn main:app -b 0.0.0.0:7000
[2021-06-29 23:51:28 +0000] [302255] [INFO] Starting gunicorn 20.0.2
[2021-06-29 23:51:28 +0000] [302255] [INFO] Listening at: http://0.0.0.0:7000 (302255)
[2021-06-29 23:51:28 +0000] [302255] [INFO] Using worker: sync
[2021-06-29 23:51:28 +0000] [302260] [INFO] Booting worker with pid: 302260
[2021-06-29 23:51:28 +0000] [302260] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 66, in wsgi
    self.callable = self.load()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ubuntu/api/main.py", line 5, in <module>
    from answor import Answor
  File "/home/ubuntu/api/answor.py", line 2, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'
[2021-06-29 23:51:28 +0000] [302260] [INFO] Worker exiting (pid: 302260)
[2021-06-29 23:51:28 +0000] [302255] [INFO] Shutting down: Master
[2021-06-29 23:51:28 +0000] [302255] [INFO] Reason: Worker failed to boot.

但是如果我从终端运行导入代码,它可以工作:

~$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> a = pd.DataFrame([1,2,3])
>>> a
   0
0  1
1  2
2  3
>>> 

用 conda 安装 pandas

conda install -n base pandas

然后:

conda list

我可以看到熊猫

pandas                    1.2.5                    pypi_0    pypi

错误仍然存​​在。

所以卸载并重新安装(只是熊猫):

conda uninstall --force pandas
conda install pandas

我目前的 conda env 是(base)并且这是有意的。

然后我运行了这个:

$ pip show pandas
Name: pandas
Version: 1.2.5
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author: None
Author-email: None
License: BSD
Location: /home/ubuntu/miniconda3/lib/python3.8/site-packages
Requires: pytz, python-dateutil, numpy
Required-by: fastai

我可以找到有 2 个与熊猫相关的文件夹

pandaspandas-1.2.5.dist-info

所以我检查了蟒蛇在哪里

~$ which python
/home/ubuntu/miniconda3/bin/python

还跑了:

pip freezepip3 freeze

pandas到处都可以。

我也跑

pip install pandas
pip3 install pandas

问题仍然存在。

我不想卸载所有东西,因为有另一个应用程序正在运行。

任何想法?


更新 1

要查看gunicorn安装位置:

~$ 哪个gunicorn /home/ubuntu/.local/bin/gunicorn

我确实在第一名注意到了这一点,因此跑了:

~$ conda install -n base gunicorn
Collecting package metadata (current_repodata.json): done
Solving environment: | 
The environment is inconsistent, please check the package plan carefully
The following packages are causing the inconsistency:

  - fastchan/noarch::fastai==2.4=py_0
done

## Package Plan ##

  environment location: /home/ubuntu/miniconda3

  added / updated specs:
    - gunicorn


The following NEW packages will be INSTALLED:

  pandas             pkgs/main/linux-64::pandas-1.2.5-py38h295c915_0


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

这次我发现了一个不同之处,那就是它还安装了 pandas :)。

在 conda 下运行此安装后gunicorn,我运行

$ which gunicorn
/home/ubuntu/.local/bin/gunicorn

但它仍然显示旧路径。

然后我运行了以下命令:

$ python -m pip install pandas gunicorn
Requirement already satisfied: pandas in /home/ubuntu/miniconda3/lib/python3.8/site-packages (1.2.5)
Requirement already satisfied: gunicorn in /home/ubuntu/.local/lib/python3.8/site-packages (20.0.2)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/ubuntu/.local/lib/python3.8/site-packages (from pandas) (2.8.1)
Requirement already satisfied: pytz>=2017.3 in /home/ubuntu/miniconda3/lib/python3.8/site-packages (from pandas) (2021.1)
Requirement already satisfied: numpy>=1.16.5 in /home/ubuntu/.local/lib/python3.8/site-packages (from pandas) (1.20.3)
Requirement already satisfied: six>=1.5 in /home/ubuntu/miniconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas) (1.16.0)
Requirement already satisfied: setuptools>=3.0 in /home/ubuntu/miniconda3/lib/python3.8/site-packages (from gunicorn) (52.0.0.post20210125)

但不幸的是,我尝试gunicorn再次运行我的应用程序,问题仍然存在。

标签: pythonpandaspipconda

解决方案


从您的基本 conda 环境中,尝试:

# Install packages
python -m pip install pandas
python -m pip install gunicorn

# Run the program
python -m gunicorn main:app -b 0.0.0.0:7000

如果这不起作用,请尝试像这样创建一个新的 conda 环境,然后运行上面的安装步骤。

# Replace myenv with any name
conda create -n myenv python

推荐阅读