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

poetry shell does not activate virtualenv #571

Closed
2 tasks done
timonbimon opened this issue Oct 28, 2018 · 41 comments
Closed
2 tasks done

poetry shell does not activate virtualenv #571

timonbimon opened this issue Oct 28, 2018 · 41 comments
Labels
area/cli Related to the command line area/venv Related to virtualenv management kind/bug Something isn't working as expected
Milestone

Comments

@timonbimon
Copy link

timonbimon commented Oct 28, 2018

  • 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/pythonpoetry shell
Spawning shell within /Users/timon/Library/Caches/pypoetry/virtualenvs/YOLO-SAR-py3.7which python
/Users/timon/.pyenv/shims/pythonsource /Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/activatewhich 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 :)

@itsthejoker
Copy link

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 shell to do that.

@seansfkelley
Copy link

You might be experiencing #497.

@itsthejoker
Copy link

@seansfkelley Confirmed, am experiencing #497. Good catch!

@sobolevn
Copy link
Contributor

sobolevn commented Nov 18, 2018

I am also experiencing this.
I have all my setup opensourced, so one can follow all the steps of my zsh/pyenv/poetry shell madness. Here it is: https://github.com/sobolevn/dotfiles

Related parts:

Things I have tried:

if [[ -z "$VIRTUAL_ENV" ]]; then
  eval "$(pyenv init -)"
fi

Now, it just ignores pyenv when new shell is created by poetry shell, but is still does not work as it should. Maybe we can have a look at how pipenv handles this? It is working for me.

@sdispater
Copy link
Member

The shell command 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 activate file for now until a better solution comes up.

@sdispater sdispater added kind/bug Something isn't working as expected area/cli Related to the command line area/venv Related to virtualenv management labels Nov 19, 2018
@sdispater sdispater added this to the 1.0 milestone Nov 19, 2018
@sobolevn
Copy link
Contributor

sobolevn commented Nov 19, 2018

Maybe something like source "$(poetry venv)"? So, there would be no need in shell command.

@betafcc
Copy link

betafcc commented Dec 3, 2018

Just in case someone else came googling for a quick but reliable temporary alternative that works in all shells:

# on .{bash,zsh,wtv}rc
alias poetry_shell='. "$(dirname $(poetry run which python))/activate"'

@betafcc
Copy link

betafcc commented Dec 5, 2018

@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 #497 (comment)

@drcongo
Copy link

drcongo commented Jan 2, 2019

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 shell to do anything I also had to pip3 install six attrs

poetry install --no-dev ran OK, I have a .venv in my project, but there's no activate script at .venv/bin/activate

@jbmusso
Copy link

jbmusso commented Jan 23, 2019

@drcongo Are you sure that .venv/bin/activate is missing? It could be that it doesn't have execution permissions.
I just encountered this and Google'd my way to this Github issue. The following did not complain:

$ chmod u+x .venv/bin/activate

Unsure about why the execution permissions are not being properly set at install time though.

@drcongo
Copy link

drcongo commented Jan 27, 2019

@jbmusso Yeah, positive it wasn't there. Not sure what the issue was now, I ended up dropping pyenv from my build so that may have been a contributing factor.

@Congee
Copy link

Congee commented Feb 2, 2019

I know it's hard to make a decision. Can you update docs to let users know this issue before it's resolved? @sdispater

@dogweather
Copy link

dogweather commented Mar 1, 2019

I came here to figure out what shell (and run) is actually supposed to do. I've had the same experience as the others, that it doesn't seem to work like virtualenv. The docs are silent as to this. E.g., I just want the basics, like:

  • How to run a python interpreter in the prepared environment?
  • How to / best practices for running an IDE like VS Code in the prepared environment?

I've been spending a lot of time debugging these two use cases, and haven't found a solution. To me, these are the most important commands, actually using the results of all the config work.

@wfa207
Copy link

wfa207 commented Mar 7, 2019

tl; dr — Move your pyenv and poetry configuration settings to ~/.bash_profile (make sure poetry configs come after pyenv's) and out of ~/.bashrc if you're using bash.

I got stuck on this issue in bash and the main issues seem to stem from the interaction of pyenv and poetry's PATH-setting configuration found in my dotfiles:

# Initialize pyenv when a new shell spawns
eval "$(pyenv init -)"

# Modify path for Python's poetry dependency management system
export PATH="$HOME/.poetry/bin:$PATH"

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_profile gets executed only on login (OS X registers new windows / panes as logins), whereas ~/.bashrc gets executed each time a shell is started. Because poetry shell is running a bash shell under the hood, it will also source ~/.bashrc (and therefore the pyenv script) when it is run, thus prepending pyenv's shims to PATH after 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 to PATH unfettered.

@seansfkelley
Copy link

@wfa207 more details at #198 (comment), which is linked from the (arguable duplicate of this issue) #497.

@floer32
Copy link

floer32 commented Mar 15, 2019

@betafcc :

Just in case someone else came googling for a quick but reliable temporary alternative that works in all shells:

# on .{bash,zsh,wtv}rc
alias poetry_shell='. "$(dirname $(poetry run which python))/activate"'

Or as a bash/zsh function ('functions' are a bit more flexible than alias, in scripting). I've renamed to poetry_activate to show it is not a subshell

function poetry_activate () {
    local _python="python${PYTHON_VERSION:-}"  # you can do PYTHON_VERSION=2.7, PYTHON_VERSION=3.7 etc
    local activation_script="$( dirname $( poetry run which ${_python} ) )/activate"
    . "${activation_script}"
}

update: 👇 #571 (comment) 👇

@floer32
Copy link

floer32 commented Mar 15, 2019

Maybe something like source "$(poetry venv)"? So, there would be no need in shell command.

This is my vote too 😀


Oh! The following is already supported in latest master at least - I think this is plenty.

source "$( poetry env info --path )/bin/activate"

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) --

[ -z "${VIRTUAL_ENV:-}" ] && source "$( poetry env info --path )/bin/activate"

So then IMHO all we really need is

  1. small addition to the docs to suggest this usage pattern.
  2. mark poetry shell for deprecation.
  3. poetry should append export POETRY_ACTIVE=1 to activate script, upon any virtualenv creation. (otherwise poetry run will have POETRY_ACTIVE but not when you just source the activate script.)

@seansfkelley
Copy link

seansfkelley commented Mar 18, 2019

If you're on Poetry 0.12.x that doesn't have env info but only debug:info, you can use:

source "$(poetry debug:info | rg '.*Path:[^/]+(.*)' -r '$1')/bin/activate"

(rg = ripgrep, though you could do it with grep and sed too)

or in alias form with escaping

alias activate=$'source "$(poetry debug:info | rg \'.*Path:[^/]+(.*)\' -r \'$1\')/bin/activate"'

@vastus
Copy link

vastus commented Apr 3, 2019

If you're on Poetry 0.12.x that doesn't have env info but only debug:info, you can use:

source "$(poetry debug:info | rg '.*Path:[^/]+(.*)' -r '$1')/bin/activate"

Just a heads up for this to work make sure you've enabled the creation of virtualenvs:

poetry config settings.virtualenvs.create true

@harrybiddle
Copy link

harrybiddle commented May 31, 2019

For those on a Poetry version without the env command (like me, for example, on 0.12.16), you can also parse debug:info. I have this in my bashrc:

[ -z "${POETRY_ACTIVE}" ] || source "$(poetry debug:info | grep Path | awk '{print $3}')/bin/activate"

gnattishness added a commit to gnattishness/dotfiles that referenced this issue Jun 6, 2019
@maw maw mentioned this issue Jun 26, 2019
2 tasks
@jthetzel
Copy link

jthetzel commented Jul 4, 2019

@ziofil For fish, try:
alias poetry_shell='. (dirname (poetry run which python))/activate.fish'

@nicola-zanardi
Copy link

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

invoke-expression $($(poetry debug:info | sls Path | Out-String -Width 1000 | % {$_.replace("* Path: ", "").trim()}) + "\Scripts\activate.ps1")

@oleastre
Copy link

Just for info, while waiting for a proper fix, I'm using the following alias:

alias poetry_activate='bash --rcfile <(echo ". \"$HOME/.bashrc\"; . $(dirname $(poetry run which python))/activate")'

Advantage is that, just like pipenv shell, it launches a new shell (in my case, bash) with the venv activated.

aksell added a commit to BluEye-Robotics/blueye.protocol that referenced this issue Sep 29, 2019
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: python-poetry/poetry#571 (comment)
@merwok
Copy link

merwok commented Nov 13, 2019

FTR:

Are you sure that .venv/bin/activate is missing? It could be that it doesn't have execution permissions.

The activate script must be sourced, not run, in order to edit the current shell configuration rather than run a sub-shell. That’s why it does not have the executable bit. (If Python install layouts had standard data directories like $prefix/share on unix, the script would be there, avoiding the confusion created by putting it in bin.)

Editing the current shell has issues and proves confusing, so there are other approaches based on sub-shells exist, like inve or poetry shell.

@2tunnels
Copy link

2tunnels commented Dec 9, 2019

In my case pyenv initialization was happening every time Poetry spawned a new shell.
As a workaround I moved pyenv initialization from .zshrc to .profile, so that it will initialize only once, not on every shell spawn, appending shims directory to $PATH over and over.
I think same fix can be used with .bashrc or removing pyenv from plugins, if you're using something like Oh My Zsh.

@severinsimmler
Copy link

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.

@sdispater
Copy link
Member

The shell command has been improved in the latest 1.0.0 version and virtual environments should now be properly activated.

@phoenixeliot
Copy link

phoenixeliot commented Jan 28, 2020

I am experiencing this issue in Poetry version 1.0.2; poetry shell does not seem to activate the venv (I get eg. zsh: command not found: django-admin when trying to run django-admin), and running poetry shell a second time tells me it's already activated but still django-admin is not found. But source .venv/bin/activate does 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:

export PATH="$(pyenv root)/shims:$PATH"
export PYENV_VERSION=3.6.4
export PATH="$HOME/.poetry/bin:$PATH"

But now if I run poetry shell I get this error due to not escaping my path that has a space in it:

(base) ➜  hue_app git:(master) ✗ poetry shell
Spawning shell within /Users/phoenix/Code/_personal projects/hue_app/.venv
Identity added: /Users/phoenix/.ssh/id_rsa_phoenix (/Users/phoenix/.ssh/id_rsa_phoenix)
(base) ➜  hue_app git:(master) ✗ . /Users/phoenix/Code/_personal projects/hue_app/.venv/bin/activate
.: no such file or directory: /Users/phoenix/Code/_personal

@tonymj76
Copy link

tonymj76 commented May 7, 2020

Am just looking for a way to deactivate the env poetry created. even when i close the poetry shell and run poetry env list it still show (Activated) how can i deactivate this? or is it ok to just leave it activated?

@jerryasher
Copy link

@sdispater I am experiencing this still on poetry 1.0.5 running on Ubuntu 20.04 under WSL2.

the virtual environment is in ~/.cache/pypoetry/virtualenvs

@brechtm
Copy link

brechtm commented Jun 16, 2020

Whoever ends up here might be interested in setting up automatic Poetry virtualenv activation using direnv.

@leonbloy
Copy link

leonbloy commented Jul 25, 2021

I've experienced this with Poetry 1.1.7, in Fedora34 under WSL2

I tracked it down these lines in /etc/profile.d/00-remix.sh :

if (command -v cmd.exe >/dev/null 2>&1); then
  ...
  wHomeWinPath=$(cmd.exe /c 'cd %SYSTEMDRIVE%\ && echo %HOMEDRIVE%%HOMEPATH%' 2>/dev/null | tr -d '\r')
 

If the child shell executes those lines (as per default in Fedora34 Remix for WSL ), then, after poetry shell, my new sheel is not virtualized as expected (path and prompt are not modified)

I'm not sure where lies the problem, but it does not seem to be related to the PATH only (as #497 seems to suggest), it's the whole environment .

My fix was to modify that script so it does NOT run on child shells.
All works as expected with that fix.

Copy link

github-actions bot commented Mar 2, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/cli Related to the command line area/venv Related to virtualenv management kind/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests