1

Basically I would like to know if those 2 snippets do the same thing :

conda install -n myEnv myPackage

VS

conda activate myEnv
pip install myPackage

Or in a different way, does a pip install when a conda environment is activated equal doing a conda install on myEnv ?

EDIT : I thought it was obvious but => more precisely, does the second snippet only install the package on the environment or on the overall system ?

PS : Asking because there's a package available with pip but not with conda and I want it to only be installed on myEnv

Atralb
  • 724
  • 2
  • 8
  • 17
  • 1
    Possible duplicate of [Using Pip to install packages to Anaconda Environment](https://stackoverflow.com/questions/41060382/using-pip-to-install-packages-to-anaconda-environment) – Aurum Sep 09 '19 at 09:21
  • 2
    @Mohan the suggested duplicate has many rather confusing answers - I think this is a simpler question which can be answered much more clearly. – nekomatic Sep 09 '19 at 13:21

2 Answers2

2

The Anaconda docs make it clear that if you use conda as your virtual environment manager, you should stick to conda install to install new packages as far as possible:

Unfortunately, issues can arise when conda and pip are used together to create an environment, especially when the tools are used back-to-back multiple times, establishing a state that can be hard to reproduce. … Running conda after pip has the potential to overwrite and potentially break packages installed via pip. Similarly, pip may upgrade or remove a package which a conda-installed package requires.

If you can't get all the packages you need from a conda channel, they say this, which is good advice even if you don't use pip:

If there is an expectation to install software using pip along-side conda packages it is a good practice to do this installation into a purpose-built conda environment to protect other environments from any modifications that pip might make.

Finally the same document notes:

Use conda environments for isolation

  • create a conda environment to isolate any changes pip makes
  • environments take up little space thanks to hard links
  • care should be taken to avoid running pip in the “root” environment

Provided you activate the correct conda environment first, the pip install command(s) should use that environment's pip and install only into that environment.

nekomatic
  • 5,988
  • 1
  • 20
  • 27
  • You are not answering my question at all. I'm aware of best practices in programming. I made this post precisely for this purpose. The second text you are quoting is exactly the root of my concern. There's a package (`pdf2image`) which I can't install with `conda`, but is possible to install with `pip`. However I only want it to be installed on the virtual env, hence why I'm asking this question here to know if doing a `pip install` when a conda env is activated only installs it on the given environment and not on the system itself. – Atralb Sep 10 '19 at 12:29
  • See my edit (and read the linked doc all the way to the end ;-) – nekomatic Sep 10 '19 at 13:34
0

Yes and no. pip downloads and installs the package from PyPI whereas conda does the same from Anaconda repositories. There are packages in PyPI not present in Anaconda, and the other way around. For managing the environment I would choose one way or the other, since with pip you can freeze into a requirements.txt (pip freeze > requirements.txt) and conda you can either export the whole environment (conda env export) or the list of packages (conda list --export > requirements.txt). However if you try to use a conda-generated file from pip, it will most probably fail.

fernandezcuesta
  • 2,390
  • 1
  • 15
  • 32
  • I want to install a package in a conda env but which is not available with conda, hence my question. Does pip always install system-wide or only in the currently activated environment ? – Atralb Sep 10 '19 at 12:38
  • 1
    In that case you have to install pip in your environment (`(env) conda install pip`), then use it to install whatever packages you need. Doublecheck with `pip -V` to show if you're invoking global/env pip or `pip list -v` to show where the packages are being installed. – fernandezcuesta Sep 10 '19 at 13:53