se-jaeger / zsh-activate-py-environment

ZSH plugin that automagically detects and activates your python environments (poetry, virtualenv, conda) while traversing directories.
Apache License 2.0
26 stars 5 forks source link

Extract the environment name from conda file #1

Closed ashanbrown closed 2 years ago

ashanbrown commented 2 years ago

Also prioritize conda ahead poetry (in case they are used together) and support file called environment.yml (with yml suffix instead of yaml).

I'm assuming that poetry would only be used to manage python dependencies while conda might be used as the overarching environment manager. My use case involves having poetry mange the requirements.txt file but conda manage the development environment as a whole (with a call to pip: [ "-r requirements.txt" ]).

se-jaeger commented 2 years ago

Hi @ashanbrown,

thanks a lot for helping develop this further!

There are some very interesting and useful changes. However, the yaml package is not a standard library and is a hard requirement in your PR. That's exactly the reason why I didn't implement the automated detection and activation of conda environments.

Do you think we can somehow reliably find the env name, maybe by reading the yaml and using regex? (Or parsing the output of conda info if such a command exists) As you can see, I'm not so firm with conda 😄

ashanbrown commented 2 years ago

Hi @se-jaeger , thanks for the response. Your reasoning to omit this behavior makes sense to me. I've been using python installs from conda-forge, which seems to include pyyaml as yaml, so it feels like part of the standard library, but I read that it isn't always that way. I remember trying to find a way to determine the conda env name for other reasons using a conda env command but nothing presented itself. conda env has only these commands and none of them spit out information about the default environment file:

usage: conda-env [-h] {create,export,list,remove,update,config} ...

positional arguments:
  {create,export,list,remove,update,config}
    create              Create an environment based on an environment file
    export              Export a given environment
    list                List the Conda environments
    remove              Remove an environment
    update              Update the current environment based on environment file
    config              Configure a conda environment

I hate to use a regex to try to parse a yaml file because I'm sure I can't hit all the corner cases, but that might be the only way. Another option might be to first check ifyaml is installed and only do the yaml parsing if exists. How would you feel about a conditional check to see if yaml.safe_load is available and then falling back to a regex?

se-jaeger commented 2 years ago

In my case, I use my OS python to run this plugins code, which is also why I don't use some of the modern Python's features. Absolutely agree that writing regex is error prone (at least when I try to write it).

Sure, using try/except to test whether yaml is available sounds sensible to handle this. If it doesn't, we could ask for the environment's name and call link(CONDA_TYPE, <user input env name>) to create the .linked_env file. However, it should be possible to cancel this and not ask again.

se-jaeger commented 2 years ago

Thanks so much for you work!