question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

poetry shell does not activate virtualenv

See original GitHub issue
  • 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 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:74
  • Comments:40 (5 by maintainers)

github_iconTop GitHub Comments

73reactions
betafcccommented, 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"'
28reactions
wfa207commented, Mar 8, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

poetry Virtual environment already activated - python
A workaround for this specific issue is to activate the shell manually. It might be worth aliasing the following source $(poetry env info ......
Read more >
Python Poetry: Package and venv Management Made Easy
Activating the virtual environment. You can use the poetry shell command if you want to use the virtual environment interactively. This command ...
Read more >
Configuration | Documentation | Poetry - Python dependency ...
Create the virtualenv inside the project's root directory. If not set explicitly, poetry by default will create virtual environment under {cache-dir}/ ...
Read more >
Can I activate a Python virtual environment(poetry) from GUI ...
What I usually do is, activate the virtual environment, then just run the gui using command inside shell (which the virtual env activated)....
Read more >
Working with virtualenv - PDM
Instead of spawning a subshell like what pipenv and poetry do, pdm-venv doesn't create the shell for you but print the activate command...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found