0
votes

The tutorial requires the installation of Flask using Pipenv on an empty folder (my file path /Users/matthewnickolls/Desktop/Flask). I am installing via the Terminal using a zsh shell but I am getting the following 'No such file or directory' error message shown below:

Screenshot

I can see that the error is a result of a failure to load the path for (what I assume is) creating the virtual environment:

(/bin/sh: /Users/matthewnickolls/.local/share/virtualenvs/matthewnickolls-ydr5OSCv/bin/pip: /Users/matthewnickolls/.local/share/virtualenvs/matthewnickolls-ydr5OSCv/bin/p: bad interpreter: No such file or directory)

But I cannot work out how to resolve that problem.

I have noticed there is a warning earlier on during the installation that I should be using Python 3.7 but I am in fact, according to the warning message, using Python 3.9. I wasn't aware I was using 3.9 and thought my default would be 3.7.

Would this be the problem? If so is there a way of specifying that I wish Pipenv to install using Python 3.7?

For additional information, I also had difficulties installing Pipenv following the instructions from the same tutorial. Eventually managed it using an alternative method by first installing Homebrew (which I installed on the recommendation of the Pipenv webpage). I don't know whether that is relevant to solving the above problem, but given it is a Homebrew installed Pipenv that I am using to try to install Flask, I thought it might be worth mentioning in case it is.

1
You could maybe try editing your requirements.txt file to say Python 3.9.0 instead of 3.7Mark Setchell

1 Answers

0
votes

When creating a virtual environment, Pipenv just creates a symlink to whatever python installation you tell it to use. So on a Mac, if you are using python from Homebrew, then the virtual env would just contain a symlink to the Homebrew-installed Python (typically in /usr/local/opt/). Then, the errors you got happens when:

  1. You create a virtual env using a specific Python installation
  2. Somehow that Python installation is uninstalled or moved to a different path
  3. You use that same virtual env and do pipenv install

Demo:

  1. I already created a virtual env using Homebrew's [email protected]

    TEMP$ brew info [email protected]
    ...
    Python has been installed as
      /usr/local/opt/[email protected]/bin/python3
    
    TEMP$ pipenv --py
    /Users/me/.venvs/TEMP-fjWX8BKZ/bin/python
    TEMP$ $(pipenv --py) --version
    Python 3.7.9
    
    TEMP$ cat Pipfile | grep python_version
    python_version = "3.7"
    
  2. Check that virtual env python is just a symlink to brew's [email protected]

    TEMP$ pushd $(pipenv --venv)/bin
    ~/.venvs/TEMP-fjWX8BKZ/bin ~/TEMP
    
    bin$ ls -l python
    lrwxr-xr-x  1 me  staff  39 Jan  9 15:29 python -> /usr/local/opt/[email protected]/bin/python3.7
    
    bin$ popd
    ~/TEMP
    
  3. Accidentally, uninstall [email protected] (Probably you used python@3 when you installed with Homebrew, which refers to whatever latest Python version Homebrew supports. So, you originally had Python3.7, but then you updated it but Homebrew's python@3 is now Python3.9, and that replaced your 3.7 installation)

    TEMP$ brew uninstall --ignore-dependencies [email protected]
    
  4. Now, when you invoke pipenv it will use another python installation. In this case, let's say you installed Homebrew's [email protected]:

    TEMP$ brew install [email protected]
    ...
    
    TEMP$ pipenv install Flask
    Warning: Your Pipfile requires python_version 3.7, but you are using 3.9.1 (/usr/local/Cellar/pipenv/2/l/bin/python).
      $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
      $ pipenv check will surely fail.
    Installing Flask...
    ⠋ Installing...Failed to load paths: /bin/sh: /Users/me/.venvs/TEMP-fjWX8BKZ/bin/python: No such file or directory
    
    Output: 
    ⠙ Installing Flask...Failed to load paths: /bin/sh: /Users/me/.venvs/TEMP-fjWX8BKZ/bin/python: No such file or directory
    
    Output: 
    Failed to load paths: /bin/sh: /Users/me/.venvs/TEMP-fjWX8BKZ/bin/python: No such file or directory
    
    Output: 
    Error:  An error occurred while installing Flask!
    Error text: 
    /bin/sh: /Users/me/.venvs/TEMP-fjWX8BKZ/bin/pip: /Users/me/.venvs/TEMP-fjWX8BKZ/bin/python: bad interpreter: No such file or directory
    
    ✘ Installation Failed 
    

There you'll get the same error because your virtual env's python was still symlinked to some Python installation that does not exist anymore. Pipenv does not automatically update it when you change your Python installations (because that's not its job). What complicates the problem is that you now changed from Python 3.7 (as registered in your Pipfile) to a different Python version 3.9.

The solution to such errors is usually simple: just re-create your virtual environment using whatever is now the active/correct Python installation. Pipenv even hints at it:

Warning: Your Pipfile requires python_version 3.7, but you are using 3.9.1 (/usr/local/Cellar/pipenv/2/l/bin/python).
  $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
  $ pipenv check will surely fail.

So..

  1. Just go to the directory and delete the old virtual env

    TEMP$ pipenv --rm
    Removing virtualenv (/Users/cerberus/.venvs/TEMP-fjWX8BKZ)...
    
  2. Re-check your Python installations

    • I don't know what happened to your 3.7, but re-installing it (and specifying an exact version) would help

      TEMP$ brew install --force [email protected]
      
    • Get the path

      TEMP$ brew info [email protected]
      ...
      Python has been installed as
        /usr/local/opt/[email protected]/bin/python3
      
  3. Re-install everything but specify path to python

    TEMP$ pipenv install --dev --python=/usr/local/opt/[email protected]/bin/python3
    
    • --python tells Pipenv which version of Python to use (and create a symlink to it)
    • If you are changing Python versions, edit your Pipfile. That would get rid of the warning at the start that "Your Pipfile requires python_version X.Y, but you are using A.B.C"
      [requires]
      python_version = "3.7"  
      
  4. Check

    TEMP$ $(pipenv --py) --version
    Python 3.7.9
    
    TEMP$ ls -l $(pipenv --venv)/bin/python
    lrwxr-xr-x  1 me  staff  39 Jan  9 16:07 /Users/me/.venvs/TEMP-fjWX8BKZ/bin/python -> /usr/local/opt/[email protected]/bin/python3.7