Dev Note: Cheats Lists on Virtual Environment Building with conda

Problems:

If you built virtual environment with conda and pyenv, sometimes configuration files might conflict, thus pip install packages in global environment instead of local one. I just encountered this problem which motivates me to record how I fixed it.

Error

  • Status: testing cross-device function of application
  • Git clone from my private repo and created env with conda create -n myenv python=3.11
  • However, after run pip install -r requirements.txt, Python scripts could not be executed due to ModuleNotFound Error

Solution

I fixed the error by following steps:

  • Check PATHs including pyenv
  • Change the version of python

Check PATH of conda, python, pip

Almost all ModuleNotFound Errors are resulted from wrong install path, thus I checked PATHs

1
2
3
4
5
which conda 
# outputs: /Users/user_name/anaconda/condabin/conda

which python
# outputs: /Users/user_name/anaconda/conda/my_env/bin

-> PATHs of conda and python were correct.

However, when I checked PATH of pip, it showed:

1
2
which pip
# outputs: /Users/user_name/bin

-> pip PATH is anchored to the global but not local.

Potential problem

Then I typed following steps (ordinary process):

  • Unless changed python version from 3.11 to 3.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# remove previous conda virtual environment
conda remove --name my_env --all

# create new virtual environment
conda create -n my_env python=3.10

# install pip within conda
conda install pip

# check PATHs
which conda
which python
which pip

# outputs were correct

# use pip to install requirements
python -m pip install -r requirements.txt

-> This error probably due to python 3.11

Conclusion

This kind of ModuleNotFound Error due to python 3.11, configuration files might conflict. Thus, it is stable to install python 3.10 to keep PATHs of conda, python and pip in correspond dir.


Keywords:
#dev_note #conda #python