Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add python poetry support #995

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

doolio
Copy link

@doolio doolio commented Oct 13, 2022

Resolves: #592

PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
if [[ ! -f "$PYPROJECT_TOML" ]]; then
log_error "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
poetry init
Copy link
Author

@doolio doolio Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included poetry init here as you queried whether such a command existed for pipenv in PR #314. This line could be improved further with something like the following:

# Specify the active python rather than the one used to install poetry in case they are not the same.
poetry init --python ^$(python3 --version 2>/dev/null | cut -d' ' -f2 | cut -d. -f1-2)

But I didn't include it based on your reservations in PR #770.

@doolio
Copy link
Author

doolio commented Oct 13, 2022

I'm only learning to programme (starting with python) so I suggest you take extra care in considering this PR. It seems to work for me with this in my direnvrc. I basically used layout_pipenv as a template. I tried to follow the existing style i.e. using $var rather than ${var}, $( ) rather than "$( )" etc. I did not create any tests (not sure I would be able to anyway) as there was none for layout_pipenv I could use as a starting point.

I noted in PRs #371 and #510 that stdlib.go was also updated with content similar to that added to stdlib.sh but it seems the contents of stdlib.go has been greatly reduced. Apologies, I don't yet understand go to know if other files should be updated as part of this PR.

With regard to the man files I noted the copyright is still given as 2019. I thought about changing this to the range 2019-2022 but decided in the end to ask first. I can update it to the range if you agree and update this PR.

I also took the liberty of updating the CHANGELOG as well. I hope that is okay.

Thanks for your time and this package.

Edit: If this PR is accepted I can updated the wiki as well.

@doolio doolio force-pushed the add-pypoetry-support branch 2 times, most recently from 3df1721 to 8bb214b Compare October 13, 2022 15:52
stdlib.sh Outdated Show resolved Hide resolved
stdlib.sh Show resolved Hide resolved
layout_poetry() {
PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
if [[ ! -f "$PYPROJECT_TOML" ]]; then
log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems log_msg does not exist but log_status does so have used that instead to avoid alarming the user.

Copy link

@thomasjo thomasjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! It looks quite similar to my own implementation. I prefer less automatic command execution, especially for commands that might potentially run for several minutes on large projects.

I also proposed a somewhat hackish short-circuit condition that avoids the need for invoking poetry env info. The poetry CLI is slow, and I've found that invoking it when loading the direnv environment is quite annoying/jarring.

Feel free to ignore my comments 😅

PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
if [[ ! -f "$PYPROJECT_TOML" ]]; then
log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
poetry init
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better (and less surprising) if we inform the user that they need to run poetry init instead of automatically executing that command.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed the older revisions on the wiki had only a log_error message advising the user to either execute poetry new or poetry init to create the pyproject.toml file. I included the poetry init command as @zimbatm had queried whether such a command existed for pipenv in PR #314. I presumed they would make the same query about poetry if unfamiliar with it. I can remove it and revert back to just a log_error message if that is preferable.

My only argument for keeping it as is, is that with a log_error message we exit the layout_poetry() function and so the remainder of the function is not executed unless the user cds out of and then back into their project directory which is inefficient. I suppose one could argue poetry users would know they should follow a poetry new or poetry init command with something like a poetry install command before they start to work but beginners (such as myself) may not. (I would of course know having learnt how these tools work in putting this PR together.)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running poetry init in a non-Poetry-project directory starts an interactive guide which asks the user questions about how they want to configure their new project. I personally think it's not a good idea to run such a thing automatically.

Running poetry init --quiet or poetry init --no-interaction will create a minimal project without asking questions, but still better to let the user run the command how they themselves choose in my opinion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running poetry init in a non-Poetry-project directory starts an interactive guide which asks the user questions about how they want to configure their new project.

One could argue that in running that command the user intends to covert their project into a poetry managed project and the command interaction allows them to tailor it to their tastes.

Running poetry init --quiet or poetry init --no-interaction will create a minimal project without asking questions

Yes, and it is an improvement I mentioned in the wiki. I've not included in the PR yet as I was waiting to hear @zimbatm thoughts about such options considering they asked about such options when the pipenv support was being added.

but still better to let the user run the command how they themselves choose in my opinion.

Happy to revert to just a log_status message if that is the majority view. Please see my reasons for including it in the first place here.


if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
log_status "No virtual environment exists. Executing \`poetry install\` to create one."
poetry install
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my earlier comment on automatically executing commands. I realize this is not how e.g., layout_pipenv() is implemented, but I would prefer less automatic execution of commands that might potentially run for several minutes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, and some context provided in my earlier reply. I've not found poetry install to be slow but I really just have it install the active python version to the virtual environment and add my projects dependencies or dev dependencies later with poetry add. But I agree others may have it install everything they need.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poetry install has a multitude of options to install different groups of packages etc. While an argument could be made to install the default set of packages, how do you know if the user wants to run poetry install or perhaps poetry install --no-root? Better to let the user choose here as well I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to let the user choose here as well I think.

Happy to revert to just a log_status message if that is the majority view. Please see my reasons for including it in the first place here.

stdlib.sh Outdated
poetry init
fi

VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my own custom version of layout_poetry() I've added a short-circuiting branch here to avoid invoking the poetry CLI because it's so slow. Note that my short-circuit approach is entirely based upon running Poetry with the virtualenvs.in-project config option set to true. It might be possible to adapt it for the false case as well, but I haven't invested any time towards that.

Suggested change
VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
# Try to short-circuit the need to invoke poetry because it's slow.
# If we find an "in-project" virtual env, assume it's what we're looking for.
if [[ -d ".venv" ]]; then
VIRTUAL_ENV="$(pwd)/.venv"
else
VIRTUAL_ENV="$(poetry env info --path 2>/dev/null ; true)"
fi

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poetry CLI because it's so slow

Indeed, I even suggested in the wiki to use the --no-interaction option with the init command (i.e. poetry --no-interaction init) to get a basic pyproject.toml rather than have the user answer the prompts to create it and risk direnv complain the process is taking too long for its liking.

I've not found poetry install to be slow but I really just have it install the active python version to the virtual environment and add my projects dependencies or dev dependencies later with poetry add.

It might be possible to adapt it for the false case as well, but I haven't invested any time towards that.

Which false case do you mean here. The case where the virtualenvs.in-project setting is false but the .venv exists or where the setting is false and the virtual environment exists in {cache-dir}/virtualenvs. Would the former case cause issues for poetry in knowing where to install packages? The latter case may be difficult as poetry names the virtual environment based on what the user has named their project directory.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my own custom version of layout_poetry() I've added a short-circuiting branch here to avoid invoking the poetry CLI because it's so slow.

This is a great idea. We also use .venv in projects, so this has fixed my issue, as I was just digging into why this step was slow and thought it was the poetry env step. I think it's a great tip, so I added it to the wiki as a note.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe include it in the layout_poetry function in the wiki directly? I can update the PR in the same way.

Copy link
Author

@doolio doolio Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, PR updated to reflect this check.

Edit: Wiki updated in the same way.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this check for .venv. This is very idiosyncratic. Some people call it venv; others call it ENV. See https://github.com/github/gitignore/blob/4488915eec0b3a45b5c63ead28f286819c0917de/Python.gitignore#L123-L127

Also, direnv's stdlib makes no mention of what seems like an arbitrary choice of .venv. Here's what's used instead:

VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version

It seems this would be mixing different layout models, no?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it might make sense to support a custom VIRTUAL_ENV that's already defined, as in:

direnv/stdlib.sh

Lines 822 to 826 in 7608dae

if [[ -n "${VIRTUAL_ENV:-}" ]]; then
local REPLY
realpath.absolute "$VIRTUAL_ENV"
VIRTUAL_ENV=$REPLY
else

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this check for .venv. This is very idiosyncratic. Some people call it venv; others call it ENV.

The check is on .venv because that is what poetry will look for when the virtual environment is in-project. See here.

It seems this would be mixing different layout models, no?

Not really. One can either allow direnv or poetry to manage the virtual environments. There will be no conflict but it is probably best practice to be consistent in your choice to avoid confusion.

@shosca
Copy link

shosca commented Jan 7, 2023

I have this layout_poetry in my user direnvrc config https://github.com/shosca/dotfiles/blob/0464e533b4ea237d8d6bdd7873c787e12d4ca90d/direnv/.config/direnv/direnvrc that works pretty well for me, hth

@dagadbm
Copy link

dagadbm commented Jan 11, 2023

I had to change the script I found on the wiki because it doesnt work if you never installed poetry...

so this is how it looks like now:

# https://github.com/direnv/direnv/wiki/Python#poetry
layout_poetry() {
	if [[ ! -x "$(command -v poetry)" ]]; then
		curl -sSL https://install.python-poetry.org | python3 -
	fi

	PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
	if [[ ! -f "$PYPROJECT_TOML" ]]; then
		log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
		poetry init
	fi

	if [[ -d ".venv" ]]; then
		VIRTUAL_ENV="$(pwd)/.venv"
	else
		VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
	fi

	if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
		log_status "No virtual environment exists. Executing \`poetry install\` to create one."
		poetry install
		VIRTUAL_ENV=$(poetry env info --path)
	fi

	PATH_add "$VIRTUAL_ENV/bin"
	export POETRY_ACTIVE=1
	export VIRTUAL_ENV
}

could this also be included in this PR?

@doolio
Copy link
Author

doolio commented Jan 11, 2023

I'd be happy to update the PR in this regard if @zimbatm agrees but do we want direnv to be responsible for ensuring such tools are available. In addition, is this how all users would want to install poetry?

@dagadbm
Copy link

dagadbm commented Jan 11, 2023

uff I didnt think about that you are right.

That could just be documented as well or you could add some optional value to auto install...

This is completely out of my depth so I will just see where the thread goes :)

@haplo
Copy link

haplo commented Jan 11, 2023

is this how all users would want to install poetry?

I cannot speak for all users, but I would definitely never pipe a shell script downloaded from wget or curl, and would be very angry at direnv if it did it for me without asking first. For poetry I use guix or the repositories of whichever GNU/Linux I'd be using.

@offbyone
Copy link

I emphatically agree with haplo here; having a direnv .envrc file reach out to the internet to pull code would be unacceptable to me; in this case, I don't use Poetry, really, but I'd hate to see this precedent merged.

@dagadbm
Copy link

dagadbm commented Jan 11, 2023 via email

@dagadbm
Copy link

dagadbm commented Jan 11, 2023 via email

@offbyone
Copy link

@dagadbm there is a world of difference between a package manager that installs code from relatively trusted package registries through a protocol that is documented and that installs structured contents that you explicitly requested as part of your package and a curl ... | bash with no signing or contents verification, installing an arbitrary and unbounded version of a tool that may or may not be the right thing.

Poetry doesn't, actually, necessarily go to the internet to install packages, although it often does; that's entirely a function of your project configuration.

Anyway, the correct thing here, I think, would be for this direnv tool to gracefully degrade and notify the user that they need to install poetry in their PATH and then to run direnv reload

@dagadbm
Copy link

dagadbm commented Jan 12, 2023 via email

if [[ -d ".venv" ]]; then
VIRTUAL_ENV="$(pwd)/.venv"
else
VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add double quotes:

Suggested change
VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
VIRTUAL_ENV="$(poetry env info --path 2>/dev/null; true)"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I omitted such quotes in keeping with the style already used within the stdlib.sh.

if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
log_status "No virtual environment exists. Executing \`poetry install\` to create one."
poetry install
VIRTUAL_ENV=$(poetry env info --path)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add double quotes:

Suggested change
VIRTUAL_ENV=$(poetry env info --path)
VIRTUAL_ENV="$(poetry env info --path)"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@janrito
Copy link

janrito commented Feb 5, 2023

I've been using this for a couple of days, and it works well. My only issue is that poetry allows for multiple environments to be associated with a project, when one switches from one environment to another, using for example –

poetry env use python3.11

direnv will still have the old environment activated.

I'm not sure whether there is an easy solution to this. Just thought I'd point this out, because it is quite confusing.

@doolio
Copy link
Author

doolio commented Feb 6, 2023

poetry allows for multiple environments to be associated with a project

Does it? I've not had a reason to use this command so don't know but reading the docs doesn't it update your poetry.lock file to reflect the new environment rather than adding an additional python version to the file. It creates a new virtual environment and switches to it but only one is associated with your project at any one time.

direnv will still have the old environment activated.

Right, because it is looking for changes to your .envrc file and not your poetry.lock file.

I'm not sure whether there is an easy solution to this. Just thought I'd point this out, because it is quite confusing.

Rather than using that poetry command, if you edit your .envrc file to reflect the new environment does that switch you to the new environment?

@janrito
Copy link

janrito commented Feb 6, 2023

This is a minimal example:

→ bat * .*
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: poetry.lock
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ # This file is automatically @generated by Poetry and should not be changed by hand.
   2   │ package = []
   3   │
   4   │ [metadata]
   5   │ lock-version = "2.0"
   6   │ python-versions = "^3.9"
   7   │ content-hash = "c595a0588c25d58f3e3834ad7169126836d262b925fe6ca9b5d540dcf301d254"
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: poetry.toml
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ [virtualenvs]
   2   │ in-project = false
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: pyproject.toml
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ [tool.poetry]
   2   │ name = "test-poetry-env"
   3   │ version = "0.1.0"
   4   │ description = ""
   5   │ authors = ["name <name@server.ext>"]
   6   │ readme = "README.md"
   7   │
   8   │ [tool.poetry.dependencies]
   9   │ python = "^3.9"
  10   │
  11   │
  12   │ [build-system]
  13   │ requires = ["poetry-core"]
  14   │ build-backend = "poetry.core.masonry.api"
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: .envrc
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ layout poetry

you can then create a few environments with:

poetry env use python3.9 && poetry install
poetry env use python3.10 && poetry install
poetry env use python3.11 && poetry install

So you have three environments associated with the project and 1 "active"

poetry env list
test-poetry-env-os1mIcMj-py3.10
test-poetry-env-os1mIcMj-py3.11 (Activated)
test-poetry-env-os1mIcMj-py3.9

direnv will successfully activate the right environment for you.

you may wan to "activate" a different one

python env use python3.9

but at this point, direnv will still have the python 3.11 environment activated

thomasjo added a commit to thomasjo/dotfiles that referenced this pull request Mar 8, 2023
This patch tracks my custom direnv layout for Poetry projects. There is
ongoing work towards getting a variant of this function added to the
direnv stdlib. Until that work is done, this function is necessary.

Ref: direnv/direnv#995
@jankatins
Copy link

jankatins commented Apr 26, 2023

as poetry env info --path is slow, I added caching to my version of layout_poetry for this output:

cache_output() {

  local file_dependencies=()
  local command=()
  local name="cache"
  local cache_dir=${HOME}/.cache/direnv_cache
  local validation_func=_expect_file
  local capture_stderr=false

  _expect_file(){
    if [[ ! -f ${1} ]] ; then
      log_error "Cached value (${1}) is not a file."
      return 1
    fi
    return 0
  }

  _expect_dir(){
    if [[ ! -d ${1} ]] ; then
      log_error "Cached value (${1}) is not a directory."
      return 1
    fi
    return 0
  }


  while [[ $# -gt 0 ]]; do
    case $1 in
      -n|--name)
        name="$2"
        shift # past argument
        shift # past value
        ;;
      -f|--file-dependency)
        file_dependencies+=("$2")
        shift # past argument
        shift # past value
        ;;
      --cache-dir)
        cache_dir+=("$2")
        shift # past argument
        shift # past value
        ;;
      --expect-file)
        validation_func=_expect_file
        shift # past argument
        ;;
      --expect-dir)
        validation_func=_expect_dir
        shift # past argument
        ;;
      --validation-func)
        validation_func="$2"
        shift # past argument
        shift # past value
        ;;
      --capture-stderr)
        capture_stderr=true
        shift # past argument
        ;;
      --)
        shift # past argument and break: the rest is the command
        break
        ;;
      *)
        log_error  "Unknown option ${1}"
        return 1
        ;;
    esac
  done
  
  local pwd_basename=$(basename ${PWD})
  local hash_part=$(echo "${PWD}" '$$$' "$*" | md5 )
  for f in "${file_dependencies[@]}"
  do
    hash_part=$((cat ${f} ; echo "${hash_part}") | md5)
  done

  mkdir -p "${cache_dir}"
  local cachefile=${cache_dir}/${name}_${pwd_basename}_${hash_part}

  local result=""

  if [[ ! -f "${cachefile}" ]]; then
    log_status "No cache found (${cachefile})."
  else
    local result=$(< "${cachefile}")
    if ! ${validation_func} "${result}" ; then
      rm "${cachefile}"
      log_error "Cachefile content '${result}' did not validate. Deleted cachefile ${cachefile}."
    else
      #log_status "Using cached value for '${*}' from ${cachefile} (${result})"
      log_status "Using cached value for '${*}'"
      printf '%s' "${result}"
      return
    fi
  fi

  # No cachefile or cachefile content didn't validate -> run the command and cache and return the content
  if ! ${capture_stderr} ; then
    result=$(${*} 2>/dev/null);
  else
    result=$(${*} 2>&1);
  fi
  log_status "Caching value '${result}' to '${cachefile}'"
  printf '%s' "${result}" > "${cachefile}"
  printf '%s' "${result}"
  return
}

layout_poetry() {
  if [[ ! -f pyproject.toml ]]; then
    log_error 'No pyproject.toml found.  Use `poetry new` or `poetry init` to create one first.'
    exit 2
  fi
  
  local envdir=$(cache_output --file-dependency pyproject.toml --name poetry --expect-dir -- poetry env info --path)

  if [[ -f "${envdir}/bin/activate" ]] ; then
    source "${envdir}/bin/activate"
    export POETRY_ACTIVE=1
    log_status "Using poetry venv '${envdir}'."
  else
    log_error "Poetry venv '${envdir}' does not yet exist, use 'poetry install' to create it."
  fi
}

fi

PATH_add "$VIRTUAL_ENV/bin"
export POETRY_ACTIVE=1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use a generic ENV_VAR name here such as VENV_ACTIVE since POETRY_ACTIVE is set when one executes poetry shell but we don't do this here?

gertvdijk pushed a commit to gertvdijk/crc that referenced this pull request Sep 18, 2023
Convenience for users of direnv, let's keep it out of the repo, especially
that direnv lacks support for Poetry in its stdlib [1, 2].

[1]: https://github.com/direnv/direnv/wiki/Python#poetry
[2]: direnv/direnv#995
gertvdijk added a commit to gertvdijk/crc that referenced this pull request Sep 28, 2023
Convenience for users of direnv, let's keep it out of the repo, especially
that direnv lacks support for Poetry in its stdlib [1, 2].

[1]: https://github.com/direnv/direnv/wiki/Python#poetry
[2]: direnv/direnv#995
gertvdijk added a commit to gertvdijk/crc that referenced this pull request Sep 30, 2023
Convenience for users of direnv, let's keep it out of the repo, especially
that direnv lacks support for Poetry in its stdlib [1, 2].

[1]: https://github.com/direnv/direnv/wiki/Python#poetry
[2]: direnv/direnv#995
gertvdijk added a commit to gertvdijk/crc that referenced this pull request Sep 30, 2023
Convenience for users of direnv, let's keep it out of the repo, especially
that direnv lacks support for Poetry in its stdlib [1, 2].

[1]: https://github.com/direnv/direnv/wiki/Python#poetry
[2]: direnv/direnv#995
Nicoretti pushed a commit to Nicoretti/crc that referenced this pull request Oct 1, 2023
Convenience for users of direnv, let's keep it out of the repo, especially
that direnv lacks support for Poetry in its stdlib [1, 2].

[1]: https://github.com/direnv/direnv/wiki/Python#poetry
[2]: direnv/direnv#995
@dagadbm
Copy link

dagadbm commented Dec 23, 2023

i was noticing that by default the VIRTUAL_ENV of python layout (and i imagine direnv in general?) is actually .direnv

I would like to ask why this is not the case with this script since he is checking for .venv directory and then also creating a new one using poetry instead of using .direnv by default as layout python is doing.

@doolio
Copy link
Author

doolio commented Dec 29, 2023

i was noticing that by default the VIRTUAL_ENV of python layout (and i imagine direnv in general?) is actually .direnv
I would like to ask why this is not the case with this script

Because I based this PR on the layout_pipenv() implementation and not layout_python(). It seemed more appropriate at the time.

checking for .venv directory and then also creating a new one using poetry instead of using .direnv by default as layout python is doing.

It checks for the presence of a .venv directory in case one exists already (popular choice) and thus assumes that is what the user wants to use or has used already if the project already existed. Moreover, it is also the directory expected by poetry if one chooses to have it put the virtual environment inside the project directory (see here). If one does not exist or one has not set virtualenvs.in-project to true it allows poetry to create the virtual environment where poetry users would normally expect to find them namely in the centralised location preferred by poetry i.e. virtualenvs.path = "{cache-dir}/virtualenvs".

If you really want to use a .direnv directory for your virtual environment you can do so by executing the following command inside your project directory (first ensuring virtualenvs.in-project is not set (i.e. null) or set to false) and before creating your .envrc file.

poetry config virtualenvs.path .direnv --local

The --local option makes this configuration local to your project though I believe poetry provides this setting to change the central location for virtual environments. To avoid having to run this command for each new project you could include it when creating your .envrc inside your project directory as follows:

echo -e 'poetry config virtualenvs.path .direnv --local\nlayout poetry' > .envrc

haplo added a commit to haplo/dotfiles that referenced this pull request Jan 31, 2024
Brings in WIP from direnv, see direnv/direnv#995
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

layout poetry doesn't work