Conda环境中Python虚拟环境的行为:Python解释器选择
我正在使用Conda环境来运行一个程序,以确保所有的包都配置正确。但是,我在运行这个程序时遇到了麻烦,因为似乎环境没有选择正确的解释器。
我的conda yaml文件如下:
name: nmrfilter
channels:
- rdkit
- pytorch
dependencies:
- python=3.7
- anaconda
- rdkit
- pytorch=1.4
- torchvision
- pandas
- numpy
- seaborn
- scikit-learn
- pyarrow
- numba
- click
- pip
- ipywidgets
- pip:
- python-igraph
- louvain
- ruffus
- tqdm
- networkx
- tensorflow-cpu
- tensorboardX
- cirpy
- pubchempy
这个文件应该配置所有的Python命令,让它们使用Conda安装的Python3.7。然而,当我在Conda环境中使用Python虚拟环境时,出现了一些无法解释的情况。我的requirements.txt文件,用来创建Python虚拟环境,内容如下:
numpy==1.24
scipy
pandas
rdkit
click
cirpy
scikit-learn
matplotlib
python-igraph
pubchempy
numba
torch
tqdm
igraph
louvain
#!/bin/bash
# Get the directory path where the shell script is located
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Check if the virtual environment already exists in the script's directory
if [ -f "$script_dir/nmrfilter_env/bin/activate" ]; then
echo "NMRfilter_env already exists. Activating..."
source "$script_dir/nmrfilter_env/bin/activate"
else
echo "Creating virtual environment in $script_dir/nmrfilter_env..."
python3 -m venv "$script_dir/nmrfilter_env"
source "$script_dir/nmrfilter_env/bin/activate"
echo "Installing the requirements..."
python -m pip install -r requirements.txt
echo "Installing Jupyter Notebook..."
python -m pip install jupyter
fi
echo "Virtual environment activated."
echo "PYTHONPATH: $PYTHONPATH"
echo "Python executable: $(which python)"
echo "Python3 executable: $(which python3)"
echo "PIP executable: $(which pip)"
echo "Python version: $(python --version)"
echo "PIP version: $(pip --version)"
echo "Pip packages in virtual environment:"
pip list | grep numpy
#start the processing
python3 nmrfilter.py $1
...
...
python3 nmrfilter2.py $1
当我第一次运行这个脚本(也就是创建Python虚拟环境)时,产生了以下输出:
Virtual environment activated.
PYTHONPATH:
Python executable: /home/karl/nmrfilterv15/nmrfilter_env/bin/python
Python3 executable: /home/karl/nmrfilterv15/nmrfilter_env/bin/python3
PIP executable: /home/karl/nmrfilterv15/nmrfilter_env/bin/pip
Python version: Python 3.7.16
PIP version: pip 22.0.4 from /home/karl/nmrfilterv15/nmrfilter_env/lib/python3.7/site-packages/pip (python 3.7)
Pip packages in virtual environment:
Simulating spectra for your compounds...
Traceback (most recent call last):
File "nmrfilter2.py", line 5, in <module>
from clustering import *
File "/home/karl/nmrfilter/clustering.py", line 2, in <module>
from numpy import *
ModuleNotFoundError: No module named 'numpy'
我很困惑,为什么它会选择一个没有安装numpy的解释器,尽管我的Python虚拟环境和Conda的“nmrfilter”环境中都有安装numpy。更让我困惑的是,脚本似乎选择了Python虚拟环境的解释器,但出于某种原因,它的pip列表中却没有包含numpy?
总的来说,当我运行时,requirements.txt中的包没有出现在pip列表中,我尝试过用python3 -m pip list和python -m pip list。
然而,当我完全不使用Conda时,一切都能正确安装和使用。
顺便说一下,我知道如何解决这个问题(完全去掉Python虚拟环境,因为它是多余的),但是我想了解一下它的行为。
1 个回答
0
发现了一个错误 - Conda的nmrfilter环境安装的是Python 3.7,所以当它根据requirements.txt文件创建虚拟环境时,numpy=1.24无法使用,因为它需要Python版本大于等于3.8,这就导致了错误的出现。