funciones virtualenvwrapper no disponibles en scripts de shell


Así que, una vez más, hago un buen programa python que hace mi vida cada vez más fácil y ahorra mucho tiempo. Por supuesto, esto implica un virtualenv, hecho con la función mkvirtualenv de virtualenvwrapper. El proyecto tiene requisitos.archivo txt con algunas bibliotecas requeridas (requests too :D) y el programa no se ejecutará sin estas bibliotecas.

Estoy tratando de agregar un script de shell ejecutable bin/run-app que estaría en mi ruta (enlace simbólico en realidad). Ahora, dentro de este guión, necesito cambiar al virtualenv antes de que pueda ejecutar este programa. Así que puse esto en

#!/bin/bash
# cd into the project directory
workon "$(cat .venv)"
python main.py

Un archivo .venv contiene el nombre virtualenv. Pero cuando corro este script, obtengo workon: command not found error.

Por supuesto, tengo la virtualenvwrapper.sh originado en mi bashrc pero no parece estar disponible en este script de shell.

Entonces, ¿cómo puedo acceder a esas funciones virtualenvwrapper aquí? O estoy haciendo mal? ¿Cómo se lanzan sus herramientas de python, cada uno de los cuales tiene su propio virtualenv!?

Author: Shrikant Sharat, 2011-09-24

7 answers

Simplemente obtenga el script virtualenvwrapper.sh en su script para importar las funciones del virtualenvwrapper. Entonces debería ser capaz de usar la función workon en su script.

Y tal vez mejor, podría crear un script de shell (podría nombrarlo venv-run.sh por ejemplo) para ejecutar cualquier script de Python en un virtualenv dado, y colocarlo en /usr/bin, /usr/local/bin, o cualquier directorio que esté en su PATH.

Tal script podría verse así:

#!/bin/sh
# if virtualenvwrapper.sh is in your PATH (i.e. installed with pip)
source `which virtualenvwrapper.sh`
#source /path/to/virtualenvwrapper.sh # if it's not in your PATH
workon $1
python $2
deactivate

Y podría usarse simplemente como venv-run.sh my_virtualenv /path/to/script.py

 61
Author: mdeous,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-22 02:20:06

No puedo encontrar la manera de activar los comandos de virtualenvwrapper en el shell. Pero este truco puede ayudar: asume tu env. el nombre es myenv, luego ponga las siguientes líneas al principio de los scripts:

ENV=myenv
source $WORKON_HOME/$ENV/bin/activate
 7
Author: Coc B.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-03 08:56:51

Añadir estas líneas a su .bashrc or .bash_profile

export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh

Y vuelve a abrir tu terminal y prueba

 1
Author: Rashi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-02 11:11:10

Si tu script Python requiere un virtualenv en particular entonces ponlo/instalarlo en el directorio bin de virtualenv. Si necesita acceso a ese script fuera del entorno, podría crear un enlace simbólico.

Main.py de virtualenv bin:

#!/path/to/virtualenv/bin/python
import yourmodule

if __name__=="__main__":
   yourmodule.main()

Enlace simbólico en su RUTA:

pymain -> /path/to/virtualenv/bin/main.py

En bin / run-app:

#!/bin/sh
# cd into the project directory
pymain arg1 arg2 ...
 0
Author: jfs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-24 12:31:23

Aparentemente, estaba haciendo esto de la manera equivocada. En lugar de guardar el nombre de virtualenv en el.archivo venv, debería poner la ruta del directorio de virtualenv.

(cdvirtualenv && pwd) > .venv

Y en el bin/run-app, puse

source "$(cat .venv)/bin/activate"
python main.py

Y yay!

 0
Author: Shrikant Sharat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-24 14:20:08

También puede llamar directamente al ejecutable python de virtualenv. Primero encuentre la ruta al ejecutable:

$ workon myenv
$ which python
/path/to/virtualenv/myenv/bin/python

Luego llama desde tu script de shell:

#!/bin/bash

/path/to/virtualenv/myenv/bin/python myscript.py
 0
Author: Aaron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-17 16:06:21

Es un problema conocido. Como solución alternativa, puede hacer que el contenido del script sea una función y colocarlo en ~/.bashrc o ~/.profile

function run-app() {
  workon "$(cat .venv)"
  python main.py
} 
 0
Author: reubano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-09 11:40:56