1

Suppose I have a conda environment folder but no conda nor requirements file. Is there a way to use the environment, i.e. to activate or extract requirements, without installing conda?

Vallo Varik
  • 137
  • 1
  • 10
  • Better to ask it in "is it possible to recover conda env after uninstalling anaconda?" way i guess. – Shayan Feb 13 '22 at 12:01
  • Thanks @Shayan, I edited the question to be the way I intended -- i.e. a bit more general than env recovery. – Vallo Varik Feb 13 '22 at 12:12
  • In principle, [this](https://stackoverflow.com/a/51293330/9056440) seems a way to export requirements using the environment's pip. Recreating a new environment from the requirements, however, failed for me ('Invalid URL given'). Either the failure is irrelevant here and in principle it should work or there's a better way. – Vallo Varik Feb 13 '22 at 13:10

1 Answers1

1

Basic Pip exporting

Let's assume the environment is /path/to/env, with a /path/to/env/bin/python installed. If one only wants the Python packages, and pip is installed, then probably sufficient to do:

/path/to/env/bin/python -s -m pip list --format=freeze > requirements.txt

Note the -s flag insulates the site module from include packages in a user site (e.g., ~/.local/lib/pythonX.Y/site-packages/); if you want those included for some reason, then drop the flag.

Mimicking Conda activation

More diligently, one could simulate a basic activation by

  1. Prepending environment's bin/ to PATH; and
  2. Running any activation shell scripts (found in etc/conda/activate.d/); note that sometimes there are no such scripts and that folder doesn't exist.

Then exporting could be done with python -s -m pip list --format=freeze.

In most cases, I wouldn't expect a difference here, but I include it for completeness. We can't rule out that a package might be out there that manipulates environment variables via activation scripts in such a way that pip list output is changed. Not saying I've seen this, only that it's possible.

merv
  • 67,214
  • 13
  • 180
  • 245