15

I recently switched from bash to zsh (MacOS). I haven't used pipenv since the switch. Now when I run any pipenv command I get the following error:

$ pipenv install
zsh: /usr/local/bin/pipenv: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory

I use pyenv for Python dependency management:

$ which python
/Users/ryan.payne/.pyenv/shims/python

My default python version is 3.7.3:

$ pyenv versions
  system
  2.7.16
  3.6.8
* 3.7.3 (set by /Users/ryan.payne/.pyenv/version)

It seems like pipenv is not using my pyenv version of Python. How do I get pipenv working again?

Ryan Payne
  • 5,249
  • 4
  • 28
  • 69
  • 1
    "Bad interpreter" means exactly what it says. Check the first line of the script that fails for the "shebang" -- the specification of what interpreter is to be used to run that script. Make sure that shebang points to an interpreter that actually exists. – Charles Duffy Aug 20 '20 at 16:02
  • 4
    ...if you want it to traverse your PATH to find a copy of `python` to use (and thus, to use the same one `which` finds), then you need it to start with `#!/usr/bin/env python`; whereas right now it presumably starts with `#!/usr/local/opt/python/bin/python3.7`, which the error message indicates doesn't exist or is missing runtime dependencies.. – Charles Duffy Aug 20 '20 at 16:03
  • You did not mention OS you're using. Is it MacOS? – Alex Yu Feb 12 '21 at 02:28
  • @AlexYu yes, it is a MacOS. I've updated my question accordingly. – Ryan Payne Feb 12 '21 at 14:29

5 Answers5

13

You don't need to uninstall anything. Simply change the interpreter at /usr/local/bin and have your current python path in pyenv handy:

type python3

copy the path

vi /usr/local/bin/pipenv

It will look something like this:

enter image description here

Once it's changed, you will probably have to download pipenv again. Don't worry, your env is fine.

pip install pipenv

Go play in your env

Tyler Gallenbeck
  • 538
  • 3
  • 10
  • I had the same problem with `black` on mac after `brew upgrade`. The similar procedure worked for me. Thnx – A D Dec 01 '21 at 16:53
9

I had this same error with awscli. The solution was to install python@3.7 via homebrew and then cp that installation into the directory awscli expected.

brew install python@3.7
cp -r /usr/local/opt/python@3.7/bin/python3.7 /usr/local/opt/python/bin/python3.7
Ryan Payne
  • 5,249
  • 4
  • 28
  • 69
Matthew Hinea
  • 1,872
  • 19
  • 31
4

If you installed pipenv with pipenv with pipx, then you can reinstall pipenv via pipx reinstall pipenv which should detect any top level changes in your python environment.

autodidacticon
  • 1,310
  • 2
  • 14
  • 33
2

Run in terminal:

brew install python3 && cp /usr/local/bin/python3 /usr/local/bin/python
1

After brew installation, sometimes it may not work. Depending on whether you tried to install other python versions, the links might not be working any more, and therefore running the command

brew install python3 && cp /usr/local/bin/python3 /usr/local/bin/python

Might give an error as below

Error: Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists.

Try to remove the file 2to3

rm '/usr/local/bin/2to3'

and run the above code again Alternatively, you can force the linkage

brew link --overwrite python@<version>

but you can first see the files that will be deleted by this forced linking using the command

brew link --overwrite --dry-run python@<version>

I hope this gives more light

Michael Elimu
  • 61
  • 2
  • 7
  • I had python3 already installed so I just ran this command and it worked : cp /usr/local/bin/python3 /usr/local/bin/python – Tomas.R Apr 09 '22 at 17:29