'Conda to poetry environment

I have a conda environment that I would like to convert to a poetry environment.

What I have tried is to translate the environment.yaml of the conda environment into a pyproject.toml file that poetry can read. Here you have the steps:

  1. Generate the yaml file

    conda env export --from-history > environment.yaml

    The --from-history flag includes only the packages that I explicitly asked for. Here it is how the file looks like after installing numpy.

    # environment.yaml
    
    name: C:\Users\EDOCIC\Screepts\My_projects\Tests\conda2poetry\condaenv
    channels:
      - defaults
    dependencies:
      - numpy
    
  2. Manually create the pyproject.toml file out of environment.yaml. I added the numpy version, which I got from conda env export. Here it is the result:

    # pyproject.toml
    
    [tool.poetry]
    name = "conda2poetry"
    version = "0.1.0"
    description = ""
    authors = [""]
    
    [tool.poetry.dependencies]
    python = "~3.7"
    numpy = "^1.21.5"
    
    [tool.poetry.dev-dependencies]
    
    [build-system]
    requires = ["poetry-core>=1.0.0"]
    build-backend = "poetry.core.masonry.api"
    
  3. Create the environment with poetry init, which will automatically read the toml file.

The process seems to work but it's quite manual and prone to mistakes. Is there a better way?



Solution 1:[1]

No, there is not a better way. Conda is a generic package manager and does not discern Python versus non-Python packages, therefore this has to be done with manual curation.

Tips

In addition to pulling down the --from-history YAML, it may also help to dump out a pip list --format=freeze. This could help with resolving any tricky packages that use different names in Conda versus PyPI.

If the environment uses any PyPI packages directly, this won't be seen from a conda env export --from-history. However, these will appear when using conda list (entries with channel pypi) or plain conda env export, which would have a dependencies.pip: section if there are any.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1