'how to activate virtualenv created by pyenv in bash script?
I need to write a script to start gunicorn + django site which developed with pyenv the script is something like
#!/bin/bash
pyenv activate .ve
pyenv exec gunicorn config.wsgi:application --name mini2 --workers 8 \
--workers 8 --bind=unix:/tmp/run/mini2.socket \
--log-level=debug --log-file=-
and the error message
2021-12-06 14:09:00 [mini2@s017 mini2]$ ./prodution.sh
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
2021-12-06 14:09:55 [mini2@s017 mini2]$
I can run the script line by line. so there must be something wrong in the script , but I have no clue what's going wrong.
I had already append couple lines about pyenv in my bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
so is there anyway to activate virtualenv create by pyenv in bash script ? any suggestions ??
update:
according to Failed to activate virtualenv with pyenv
I update my .bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
#if command -v pyenv 1>/dev/null 2>&1; then
# eval "$(pyenv init -)"
#fi
but still not working.
my test script
#!/bin/bash
pyenv activate .ve
and the error
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Solution 1:[1]
There is no need to pyenv activate
the virtual environment in your script. Because of how virtual environments work [1], you need only use the absolute path to the Python interpreter binary. You can confirm this path using $ pyenv which python
whilst your environment is activated. The most substantive thing that pyenv activate
does is prepend the interpreter binary to your PATH
such that python
, python3
, etc. will resolve to it [2]. The pyenv
initialization commands in your .bashrc
only serve enable shell auto-completion, rehash shims, and define a pyenv
shell function [3].
TLDR:
"${PYENV_ROOT}/versions/<venv name>/bin/python" script.py
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | scienceplease |