Running poetry fails with /usr/bin/env: ‘python’: No such file or directory
Asked Answered
T

5

18

I just installed poetry with the following install script

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

However, when I execute poetry it fails with the following error

$ poetry
/usr/bin/env: ‘python’: No such file or directory

I recently upgraded to ubuntu 20.04, is this an issue with the upgrade or with poetry?

Theological answered 20/5, 2020 at 20:3 Comment(1)
Related link with similar solutions: github.com/python-poetry/poetry/issues/…Oller
T
40

poetry is dependent on whatever python is and doesn't attempt to use a specific version of python unless otherwise specified.

The above issue will exist on ubuntu systems moving forward 20.04 onwards as python2.7 is deprecated and the python command does not map to python3.x

You'll find specifying an alias for python to python3 won't work ( unless, perhaps you specify this in your bashrc instead of any other shell run command file ) as poetry spins it's own shell to execute commands.

Install the following package instead

sudo apt install python-is-python3

It should be noted that you can install python2.7 if you want to and poetry should run fine.

Theological answered 20/5, 2020 at 20:3 Comment(4)
With one of the next releases, the poetry installer will add the available python interpreter to the shebang. See this merged PR. For me, a system that hasn't a python executable, is broken. But sadly PEP394 allows this, so we have to take care about it.Wortham
I agree with that sentiment, and was surprised to see 20.04 ship without a python. Looking over that pep, it's surprising to see a lack of python permissible even when python 2 or 3 is supportedTheological
How to solve this issue on MacOS?Express
@Express run which python3 to get python3 executable path and create a symlink with this path to a bin directory named python. You can always echo $PATH to check what directory you can create the symlink in. The command looks like this on my computer ln -s /opt/homebrew/bin/python3.11 ~/bin/python Bilge
P
12

FOR MAC USERS

Run this:

ls -l /usr/local/bin/python*

You should get something like this:

lrwxr-xr-x  1 irfan  admin  34 Nov 11 16:32 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3
lrwxr-xr-x  1 irfan  admin  41 Nov 11 16:32 /usr/local/bin/python3-config -> ../Cellar/python/3.7.5/bin/python3-config
lrwxr-xr-x  1 irfan  admin  36 Nov 11 16:32 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.5/bin/python3.7
lrwxr-xr-x  1 irfan  admin  43 Nov 11 16:32 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.5/bin/python3.7-config
lrwxr-xr-x  1 irfan  admin  37 Nov 11 16:32 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.5/bin/python3.7m
lrwxr-xr-x  1 irfan  admin  44 Nov 11 16:32 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.5/bin/python3.7m-config

Change the default python symlink to the version you want to use from above. Note that, we only need to choose the one that ends with python3.*. Please avoid using the ones' that end with config or python3.*m or python3.*m-config.

Run this:

ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python

Check if it's working:

python --version # Should output Python 3.7.5
Prayerful answered 26/11, 2022 at 12:4 Comment(1)
This seems dangerous. Some Python programs might expect Python 2.7. You will only notice that when you run them and they either (hopefully) fail or (hopefully not) change their behavior.Doti
M
6

Also an issue on some other Ubuntu versions/variants (Mint 19.3 here).

The python-is-python3 answer from arshbot is a good option, alternatively I found just tweaking the script that invokes poetry fixed it for me: A more delicate approach, but also more fragile in case the script gets updated (so overwritten) in future. So anyway here's that lightweight/fragile option:

Edit the script,

vi ~/.poetry/bin/poetry

(other editors are available etc) and change the top line:

#!/usr/bin/env python

becomes

#!/usr/bin/env python3

sorted!

This is only likely to be needed as a temporary workaround considering finswimmer's comment, from which it seems poetry will be more intelligent about using python3 in future in this situation.

Meteorograph answered 8/7, 2020 at 21:8 Comment(1)
Still using this, so I guess is not as temporary as it may seem.Hognut
E
0

It can be caused by a broken venv, which is trying to use a generic call to "python" instead of the versionated "python3" command.

For me, the solution was to only:

Remove the current venv:

rm -r venv/

Then, recreate the venv using python3:

python3 -m venv ./venv
Estate answered 22/10, 2023 at 20:0 Comment(0)
C
0

When you get for poetry install - No such file or directory: 'python' Try to update your python command with update-alternatives:

$ sudo update-alternatives --config python

There are 5 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/libexec/no-python
   2           /usr/bin/python2
   3           /usr/bin/python3
   4           /usr/bin/python3.9
   5           /usr/bin/python3.11

The default was "no-python", so after I changed it to "python3.11" poetry install worked!

Croix answered 26/3, 2024 at 16:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.