How to disconnect virtualenv from bash script

I am new to shell scripts, but I want to make a bash script to activate / deactivate a virtual environment using virtualenv. Then I want to use this script as a service in Ubuntu, copying it inside the /etc/init.d folder.

In my script, I have a variable like this: VENV=/opt/odoo/odoo_server/venv_oddo/bin

This variable represents the bin path in my virtual environment.

Inside the script, I can activate the virtual environment using this statement:. . ${VENV}/activate

This is possible because you activate the file in the bin directory in the virtual environment.

But I do not know the statement that will be used in my script to deactivate my virtual environment. I can not do this:. . ${VENV}/deactivate

The problem is that there is no file with the deactivated, but the function inside the bin / activate file in the virtual environment is deactivated.

+9
source share
5 answers

It is difficult to make such a service useful.

 . ${VENV}/activate # note the dot 

or

 source ${VENV}/activate 

there will be source activate script, i.e. run its contents as if they were part of a shell or script where you submit them . virtualenvironment activate intended for this use . In contrast, just executing a script usually with

 ${VENV}/activate # note: NO dot and NO 'source' command 

will run its content in a subshell and will not have any beneficial effect.

However, your script service will already be running in its sub-tuning. Therefore, with the exception of any python commands that you run as part of the process of starting the service, this will have no effect.

On the plus side, you donโ€™t even have to worry about deactivating the environment if you do not want to run even more python elements during the service launch process, but outside of your virtualenv.

+4
source

Just deactivate . It will work in the script as well as on the command line if you use bash.

Edit: In most cases, it is also best to write the full python path in your scripts and services. It is stateless, more portable and works almost everywhere. Therefore instead of doing

 . $VENV/bin/activate /path/to/my/script.py --parameters 

usually preferable

 $VENV/bin/python /path/to/my/script --parameters 

Believe me, this will save you from debugging time)

+7
source

The deactivate command provided by virtualenvwrapper is actually a shell function, similar to workon . If you have a virtual env active, you can list the names of these functions with typeset -F .

To use them in a script, they need to be defined there, because shell functions do not extend to child shells.

To define these functions, send a virtualenvwrapper.sh script to the shell of the script where you are going to call these functions, for example:

 source $(which virtualenvwrapper.sh) 

This allows you to call these functions in your shell script, as you would in a shell:

 deactivate 

Update: What I described works for other functions provided by virtualenvwrapper (e.g. workon ). I incorrectly assumed that this would work for deactivation, but this is a more complicated case, because it is a function that will be defined only in the shell in which workon or activate were executed.

+3
source

copy deactivate code to $ {VENV} / activate.

insert your ~ / .bashrc

 deactivate() { # reset old environment variables if [ -n "$_OLD_VIRTUAL_PATH" ] ; then PATH="$_OLD_VIRTUAL_PATH" export PATH unset _OLD_VIRTUAL_PATH fi if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME" export PYTHONHOME unset _OLD_VIRTUAL_PYTHONHOME fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then hash -r fi if [ -n "$_OLD_VIRTUAL_PS1" ] ; then PS1="$_OLD_VIRTUAL_PS1" export PS1 unset _OLD_VIRTUAL_PS1 fi unset VIRTUAL_ENV if [ ! "$1" = "nondestructive" ] ; then # Self destruct! unset -f deactivate fi } 

execute the command.

 $ $VENV/activate $ deactivate 

I selectively used python 2.7 and python 3.5 without problems in this way.

I want to know the reason for the negative rating.

0
source

Use below:

 declare -Ff deactivate && deactivate 

Since the deactivation function created using the ~ / bin / activ source cannot be detected by the usual means of searching for such a command in ~ / bin, you might want to create one that simply performs the deactivation function.

The problem is that a script called deactivate containing one deactivate command will cause an infinite loop if it is accidentally executed without being in venv. A common mistake.

This can be avoided by performing deactivation only if the function exists (i.e. was created by activating the source).

0
source

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


All Articles