29

Running the following

poetry shell

returns the following error

/home/harshagoli/.poetry/lib/poetry/_vendor/py2.7/subprocess32.py:149: RuntimeWarning: The _posixsubprocess module is not being used. Child process reliability may suffer if your program uses threads.                                                                                                                                                                                    
  "program uses threads.", RuntimeWarning)                                                                                                                                                    
The currently activated Python version 2.7.17 is not supported by the project (^3.7).                                                                                                         
Trying to find and use a compatible version.                                                                                                                                                  
Using python3 (3.7.5)                                                                                                                                                                         
Virtual environment already activated: /home/harshagoli/.cache/pypoetry/virtualenvs/my-project-0wt3KWFj-py3.7

How can I get past this error? Why doesn't this command work?

arshbot
  • 12,535
  • 14
  • 48
  • 71

4 Answers4

39

I'd have to do the following

source "$( poetry env list --full-path | grep Activated | cut -d' ' -f1 )/bin/activate"
mr.bjerre
  • 2,384
  • 2
  • 24
  • 37
  • 1
    This should be an accepted answer – melaanya Sep 28 '21 at 10:57
  • 1
    You can set this up as alias in your profile as - `alias activate_poetry="source \"\$(poetry env list --full-path | grep Activated | cut -d' ' -f1 )/bin/activate\""`. Make sure you are in the project directory (dir with pyproject.toml) when you run this. – Ankit Jain Apr 10 '22 at 12:57
32

poetry shell is a really buggy command, and this is often talked about among the maintainers. A workaround for this specific issue is to activate the shell manually. It might be worth aliasing the following

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

so you need to paste this into your .bash_aliases or .bashrc

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

Now you can run acpoet to activate your poetry env (don't forget to source your file to enable the command)

arshbot
  • 12,535
  • 14
  • 48
  • 71
  • 2
    This does not work at all, see mr.bjerre answer for something more reliable – Leogout Oct 01 '21 at 06:41
  • You can set this up as alias in your profile as - `alias activate_poetry="source \"\$(poetry env list --full-path | grep Activated | cut -d' ' -f1 )/bin/activate\""`. Make sure you are in the project directory (dir with pyproject.toml) when you run this. – Ankit Jain Apr 10 '22 at 12:57
5

It's similar to activating a normal virtual environment. But we need to find out path of the poetry virtual env.

We can find the path via poetry env info --path

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

Explained in this poetry demo.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
-1

If you've installed poetry using the traditional pip command, poetry will be limited to create virtual environments for the python version for which it has been installed.

Try uninstalling poetry using:

pip3 uninstall poetry

Then reinstall using:

brew install poetry if you have a Mac.

This worked for me.

azizbro
  • 3,069
  • 4
  • 22
  • 36