Python 找不到已安装的模块,但是 pip 可以

Python can't find installed module, but pip can

使用单独的虚拟环境 conda
该环境中的所有命令都是 运行。

❯ pip install bs4
Requirement already satisfied: bs4 in /home/v4g4x/miniconda3/envs/Scraping/lib/python3.7/site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in /home/v4g4x/miniconda3/envs/Scraping/lib/python3.7/site-packages (from bs4) (4.9.3)
Requirement already satisfied: soupsieve>1.2 in /home/v4g4x/miniconda3/envs/Scraping/lib/python3.7/site-packages (from beautifulsoup4->bs4) (2.2.1)
❯ python
Python 3.7.10 (default, Mar  8 2021, 16:45:57) 
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'bs4'
>>> 

pip freeze 的输出:

❯ pip freeze
beautifulsoup4==4.9.3
bs4==0.0.1
certifi==2021.5.30
charset-normalizer==2.0.3
html5lib==1.1
idna==3.2
numpy==1.21.1
pandas==1.3.1
pathlib==1.0.1
python-dateutil==2.8.2
pytz==2021.1
requests==2.26.0
six==1.16.0
soupsieve==2.2.1
urllib3==1.26.6
webencodings==0.5.1
wget==3.2

在这里我们可以看到 bs4(因此 beautifulsoup4)已经安装并且应该可用。但是Python执行的时候找不到。

我使用 pip、conda 和 python envs 已经有一段时间了。但是我不知道为什么它找不到那个包裹。

所有类似的问题似乎都在pip和pip3之间挣扎,但我没有那个问题,所以更难理解。

尝试which pip,也许python和pip不匹配。

或者您可以使用 python -m pip install

安装它

显然甚至在 conda activate <env_name>
之后 which python 似乎出于某种原因指向 /opt/splunk/bin/python。 而 which pip 指向 <env_name>.

就是这个问题。