Search code examples
pythonpipconda

Pip & Conda : Pip installing in generic environement not conda


today I noticed that sometimes for no reason when I use pip to install a package inside a conda env it actually gets installed in my global env (python). I always ensure the env is activated and then use pip to install if the package is unavailable under conda install. Any explanation for this weird behavior since there is no error message and I only became aware of it after testing the concerned package. Here is the concerned package (but not the only one with which it happened, had it happen, with streamlit too). This behavior is random: Sometimes it gets properly installed sometimes not. enter image description here


Solution

  • One explanation would be that the environment isn't getting activated correctly. I've seen this happen in specific terminal contexts, in particular, with the VSCode and tmux terminals.

    The most robust method for ensuring the proper pip is used is to not rely on shell state by instead using conda run. E.g.,

    conda run -n ETC_tools python -m pip install PyPDF2
    

    You may also want a --live-stream and/or --no-capture-output flag to observe real-time what pip is doing or respond to prompts. See conda run --help.