poetry: poetry shell does not activate virtualenv
- I am on the latest Poetry version.
- I have searched the issues of this repo and believe that this is not a duplicate.
- OS version and name: Mac OS X, High Sierra
- Poetry version: 0.12.5
Issue
Similar to pipenv shell, I would have expected that when running poetry shell the virtualenv gets activated, but apparently this is not the case…
➜ which python
/Users/timon/.pyenv/shims/python
➜ poetry shell
Spawning shell within /Users/timon/Library/Caches/pypoetry/virtualenvs/YOLO-SAR-py3.7
➜ which python
/Users/timon/.pyenv/shims/python
➜ source /Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/activate
➜ which python
/Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/python
for comparison
➜ poetry run which python
/Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/python
Am I misunderstanding something and this is expected behaviour or is it a bug?
Thanks a lot already for your time 😃
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 79
- Comments: 41 (4 by maintainers)
Commits related to this issue
- Fix for poetry `poetry shell` fails when using it with pyenv https://github.com/sdispater/poetry/issues/571 — committed to tuxedocat/dotfiles by tuxedocat 5 years ago
- Fix for poetry `poetry shell` fails when using it with pyenv https://github.com/sdispater/poetry/issues/571 — committed to tuxedocat/dotfiles by tuxedocat 5 years ago
- Work around a poetry/pyenv path conflict See https://github.com/sdispater/poetry/issues/571 Also, fix my prompt in the process — committed to jacobian/dotfiles by jacobian 5 years ago
- Easy way to activate poetry virtual env in bash See sdispater/poetry#571 — committed to gnattishness/dotfiles by gnattishness 5 years ago
- Add support for poetry Issues: * https://github.com/sdispater/poetry/issues/505 * https://github.com/sdispater/poetry/issues/571 — committed to Sharparam/dotfiles by Sharparam 5 years ago
- Try to activate virtualenv before running tests Pytest is not found, so I don't think the virtualenv created by poetry has been activated. I found a hacky solution I'm trying to this here: https://gi... — committed to BluEye-Robotics/blueye.protocol by aksell 5 years ago
Just in case someone else came googling for a quick but reliable temporary alternative that works in all shells:
tl; dr — Move your
pyenvandpoetryconfiguration settings to~/.bash_profile(make surepoetryconfigs come afterpyenv’s) and out of~/.bashrcif you’re usingbash.I got stuck on this issue in
bashand the main issues seem to stem from the interaction ofpyenvandpoetry’sPATH-setting configuration found in my dotfiles:I found that moving these scripts to
~/.bash_profile(as opposed to~/.bashrc) helps resolve this issue. The problem in bash stems from the fact that~/.bash_profilegets executed only on login (OS X registers new windows / panes as logins), whereas~/.bashrcgets executed each time a shell is started. Becausepoetry shellis running abashshell under the hood, it will also source~/.bashrc(and therefore thepyenvscript) when it is run, thus prependingpyenv’s shims toPATHafter the virtual env is activated. By moving these configuration settings to~/.bash_profile, we avoid this issue and the virtual env can prepend its path toPATHunfettered.Anyone else find it odd that it’s so tricky to get a virtualenv up and activated? To me, that’s a main use case of poetry/pipenv/venv.
Came to file the same issue. My shell shows that a virtualenv is activated, but it’s empty; if I manually activate the .venv (installing locally to the project directory) then it works, but I would expect
poetry shellto do that.This is my vote too 😀
Oh! The following is already supported in latest
masterat least - I think this is plenty.This is more performant (relevant if you want to do something like ‘try/ensure’ virtualenv activated before every invocation of
python, etc, in your scripts) –So then IMHO all we really need is
poetry shellfor deprecation.export POETRY_ACTIVE=1toactivatescript, upon any virtualenv creation. (otherwisepoetry runwill havePOETRY_ACTIVEbut not when you just source theactivatescript.)This was still broken in 1.0.0b7, but has been resolved in 1.0.0b8 (and is still working in 1.0.0b9). Thank you.
@ziofil For fish, try:
alias poetry_shell='. (dirname (poetry run which python))/activate.fish'How would this work in the fish shell?
The
shellcommand has been problematic from the start due to different shell configurations.I think the best approach would be to stick with executing the virtualenv’s
activatefile for now until a better solution comes up.Starting with poetry 1.0.0beta2 you can do:
If you’re on Poetry 0.12.x that doesn’t have
env infobut onlydebug:info, you can use:(rg = ripgrep, though you could do it with grep and sed too)
or in alias form with escaping
The
shellcommand has been improved in the latest1.0.0version and virtual environments should now be properly activated.@dogweather That is not a constructive comment. You’re suggesting there’s some “natural” way to do this. There are many conflicting preferences people have about this.
If you want to add some detail to a usage, that’s constructive. For example, could you say if this works for you - currently it is, “source $(poetry env info -p)/bin/activate” if you want to just activate without going into subshell. (Or simply “poetry shell” if interactive subshell works for you). Don’t like this? Please be constructive in offering your take on a. more convenient workflow. “source $(poetry env activation)”? Etc.
Anyways, if that is your main use case, you should just look at autoenv and related. stdlib + venv is all you need if your comment is to be taken seriously.
Poetry/etc are about robust dependency management. Niceties about virtualenv are a small aspect. And keep in mind many of us want to use the same tools for multiple projects; too much “automatic assumptions for everyone” will hurt many multi-project use cases.
I know it’s hard to make a decision. Can you update docs to let users know this issue before it’s resolved? @sdispater
@sdispater senpai please note it’s actually pretty easy to make it work in a subshell for posix at least, there are only a few decisions to be made about windows compability and extra deps see https://github.com/sdispater/poetry/issues/497#issuecomment-443619684
Maybe something like
source "$(poetry venv)"? So, there would be no need inshellcommand.@wfa207 more details at https://github.com/sdispater/poetry/issues/198#issuecomment-430742299, which is linked from the (arguable duplicate of this issue) https://github.com/sdispater/poetry/issues/497.
I don’t have an activate script inside
.venv/bin/This is Poetry 0.12.10 installed via pip3 because of #721
In order to then get
poetry shellto do anything I also had topip3 install six attrspoetry install --no-devran OK, I have a.venvin my project, but there’s no activate script at.venv/bin/activateI am also experiencing this. I have all my setup opensourced, so one can follow all the steps of my
zsh/pyenv/poetry shellmadness. Here it is: https://github.com/sobolevn/dotfilesRelated parts:
Things I have tried:
Now, it just ignores
pyenvwhen new shell is created bypoetry shell, but is still does not work as it should. Maybe we can have a look at howpipenvhandles this? It is working for me.I am experiencing this issue in Poetry version 1.0.2;
poetry shelldoes not seem to activate the venv (I get eg.zsh: command not found: django-adminwhen trying to run django-admin), and runningpoetry shella second time tells me it’s already activated but stilldjango-adminis not found. Butsource .venv/bin/activatedoes work, and adds the(.venv)text to my command line prompt.I use the setting that puts the venv in the local directory. The venv was created with
poetry init.I have some pyenv environment variable stuff in my
.zshrc, and only this line in my.zprofile:export PATH="$HOME/.poetry/bin:$PATH"Note I also have miniconda installed, though I don’t use it much.
I tried moving these two lines to before poetry’s stuff in
.zprofile, and out of.zshrc:But now if I run
poetry shellI get this error due to not escaping my path that has a space in it:For those of you in a Windows shop, the following works in Powershell. I have something similar in my GitLab CI configuration to take advantage of the venv caching of poetry, among other beautiful things that poetry does