What shebang to use for python scripts is done under pyenv virtualenv

When it is assumed that a python script is being run from pyenv virtualenv, what is the correct shebang for the file?

As an example of a test case, python is not installed by default for my system (OSX) pandas. Pyenv virtualenv venv_namedoes. I tried to get the path to the python executable from virtualenv.

$ pyenv activate venv_name
(venv_name)$ which python
/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 to run the script, I received an error message:

(venv_name) $ ./script.py
./script.py: line 2: import: command not found
./script.py: line 3: print: command not found


Although running this path on the command line works fine:

(venv_name) $ /Users/username/.pyenv/shims/python script.py
success

(venv_name) $ python script.py # also works
success

What is needed for this? Ideally, I want something in common to point to the python of any of my current venv.

+34
5

, , , python, , :

#!/usr/bin/env python

, python.

+47

, #! shebang, script, Python .

#!/bin/bash
"exec" "pyenv" "exec" "python" "$0" "$@"
# the rest of your Python script can be written below

- Python docstring... .

.

+4

, python shebang / , script, script.

VPfB , /Users/username/.pyenv/shims/python script, exec $pyenv_python. echo $pyenv_python .

: https://unix.stackexchange.com/questions/209646/how-to-activate-virtualenv-when-a-python-script-starts

pyenv virtualenvs .

shebang :

#!/Users/username/.pyenv/python/versions/venv_name/bin/python
import pandas as pd
print 'success'

... script ( ) :

(venv_name) $ ./script.py 
success
(venv_name) $ pyenv activate non_pandas_venv 
(non_pandas_venv) $ ./script.py
success
(non_pandas_venv) $ . deactivate
$ ./script.py
success
$

, python, python . (. Per virtualenv?)

+3

, :

sudo chmod +x script.py
0

, ephiement, , , . , :

#!/bin/sh
#
# Choose the python we need. Explanation:
# a) '''\' translates to \ in shell, and starts a python multi-line string
# b) "" strings are treated as string concat by python, shell ignores them
# c) "true" command ignores its arguments
# c) exit before the ending ''' so the shell reads no further
# d) reset set docstrings to ignore the multiline comment code
#
"true" '''\'
PREFERRED_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python
ALTERNATIVE_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
FALLBACK_PYTHON=python3

if [ -x $PREFERRED_PYTHON ]; then
    echo Using preferred python $ALTERNATIVE_PYTHON
    exec $PREFERRED_PYTHON "$0" "$@"
elif [ -x $ALTERNATIVE_PYTHON ]; then
    echo Using alternative python $ALTERNATIVE_PYTHON
    exec $ALTERNATIVE_PYTHON "$0" "$@"
else
    echo Using fallback python $FALLBACK_PYTHON
    exec python3 "$0" "$@"
fi
exit 127
'''

__doc__ = """What this file does"""
print(__doc__)
import platform
print(platform.python_version())
0

Source: https://habr.com/ru/post/1677564/


All Articles