[Python] What shebang to use for Python scripts run under a pyenv virtualenv

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    When a Python script is supposed to be run from a pyenv virtualenv, what is the correct shebang for the file?

    As an example test case, the default Python on my system (OS X) does not have pandas installed. The pyenv virtualenv venv_name does. I tried getting the path of the Python executable from the virtualenv.

    pyenv activate venv_name
    which python


    Output:

    /Users/username/.pyenv/shims/python


    So I made my example script.py:

    #!/Users/username/.pyenv/shims/python
    import pandas as pd
    print 'success'


    But when I tried running the script (from within 'venv_name'), I got an error:

    ./script.py


    Output:

    ./script.py: line 2: import: command not found
    ./script.py: line 3: print: command not found


    Although running that path directly on the command line (from within 'venv_name') works fine:

    /Users/username/.pyenv/shims/python script.py


    Output:

    success


    And:

    python script.py # Also works


    Output:

    success


    What is the proper shebang for this? Ideally, I want something generic so that it will point at the Python of whatever my current venv is.

    Continue reading...

Compartilhe esta Página