-
Notifications
You must be signed in to change notification settings - Fork 624
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
layout poetry doesn't work #592
Comments
Had similar error, because previously used Have changed
|
@rolep If there are no poetry environments at all, that will result in "VENV=/bin", and |
You are right. Added empty var check - |
Are there any plans to add |
I had no success with the version on the wiki so I ended up with this one, assembling ideas from here and there. 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 VENV=$(poetry env info --path)
if [[ -z $VENV || ! -d $VENV/bin ]]; then
log_error 'No poetry virtual environment found. Use `poetry install` to create one first.'
exit 2
fi
export VIRTUAL_ENV=$VENV
export POETRY_ACTIVE=1
PATH_add "$VENV/bin"
} Edit 2022-10-13: - local VENV=$(poetry env list --full-path | cut -d' ' -f 1)
+ local VENV=$(poetry env info --path) |
Would a PR to add Poetry support into |
@Diaoul am I right in that your solution requires you to |
If this is a new project, you would've to If this is an existing project, In all cases, that means |
I have the latest (stable) My workflow:
So as you can see quite a lot of I had been working on the following solution (based on 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."
poetry init
fi
VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
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
} With this my workflow is as follows:
So with this only 3 or 4 steps before starting to work. The only issue after the poetry lock file is created I get the following error(?) message:
Though this might be expected. For the
We could also include the To avail of the project structure provided by
So 4 steps before starting to work. With this workflow I don't get the error message after the poetry lock file is written. I think I will submit this solution as a PR for consideration so Edit: Update to |
This is correct. I happened to have a virtualenv already existing for that path from a prior test so it picked it up again. |
Here's what I've done to fix this. Based on the official Poetry recipe in the wiki, change: VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true) to: VIRTUAL_ENV="$(poetry env list --full-path | head -1)" it still doesn't activate the virtualenv on first run though so exit the directory and re-enter. An update to the recipe on the wiki that works properly would be welcome. |
Why would it? That line in the wiki is determining whether the VIRTUAL_ENV already exists and if it does to set the ENV variable appropriately. Your change is providing the same information. I used The function provided in the wiki creates and activates the virtual environment on first use. |
One thing that might work would be to add a file watcher to your
That will reload direnv when that file changes. This seems to work for me with Poetry 1.4. It also detects when you change Python version with |
Sourced from: direnv/direnv#592 (comment)
Here is what I have ended up with. 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
LOCK="$PWD/poetry.lock"
watch_file "$LOCK"
local VENV=$(poetry env info --path)
if [[ -z $VENV || ! -d $VENV/bin ]]; then
log_status 'No poetry virtual environment found. Running `poetry install` to create one.'
poetry install
VENV=$(poetry env info --path)
else
HASH="$PWD/.poetry.lock.sha512"
if ! sha512sum -c $HASH --quiet >&/dev/null ; then
log_status 'poetry.lock has been updated. Running `poetry install`'
poetry install
sha512sum "$LOCK" > "$HASH"
fi
fi
export VIRTUAL_ENV=$VENV
export POETRY_ACTIVE=1
PATH_add "$VENV/bin"
} One downside is that |
Describe the bug
It doesn't activate the existing poetry virtual environment.
To Reproduce
Steps to reproduce the behavior:
Create .envrc with layout poetry in it after adding the function to ~/.config/direnv/direnvrc. Restart terminal. Observe that it doesn't load the environment.
Expected behavior
Loads the environment.
Environment
direnv version: 2.21.2
The text was updated successfully, but these errors were encountered: