pipcondarequirements

Transform conda-generated requirements.txt file to a format that works with pip


My goal is to transform conda generated requirement.txt files to .txt files that can be used with pip. I know about the requirement.txt files that are conda generated but I am working on a cluster and can't use conda.

  1. Is there any procedure that can transform such files into files that are pip compatible?
  2. Is there a set of rules that would allow me to decompose manually the conda generated requirement.txt files into pip compatible requirement.txt files? For example, if the following line is present in the (conda generated) requirements.txt file,

python3-sklearn-lib=0.18-5~pn0

I know that

  1. "=" should become "==",
  2. "python3-" should disappear
  3. "sklearn" should become "scikit-learn"

But I don't know in this example what is the signification of "~pn0" and how I should "translate" that for the pip requirements.txt file.

I am aware of the question addressed here but my situation is different: I have the conda generated requirements.txt files and try to transform them into pip compatible files.


Solution

  • No and no. The relation between a Conda package name to the PyPI package name is effectively arbitrary since there are no formal rules for mapping them. Sometimes they are equal; sometimes they involve prefixes/suffixes like py or python, with and without hyphens. Further, many Conda packages are not Python packages. A trivial example is python itself. Since there is nothing in the metadata of the packages to indicate the PyPI equivalent (if any), it is impossible to determine this without an actual brute force iteration over all the packages to extract this mapping. Perhaps someone might generate such a database someday, but for now I am unaware of its existence.

    If you must have a pip requirements file, then export that from the environment, as in this thread. If you only have the file from Conda, but no environment, then the easiest path is probably to recreate the environment using Conda, then do the pip list --format=freeze.

    I'd also note that I agree with the comments: it sounds like the question is solving the wrong problem. Conda is fully operational without elevated privileges and is almost ubiquitously used on HPC servers I use. Look into installing Miniforge.