1

I am connected to my Raspberry Pi 4B via Remote-SSH using Visual Studio Code on Windows 10. I wrote a shell script venv.sh remotely from my Windows machine:

#!/bin/bash
# Activate the virtual environment
source .VENV/bin/activate

When I activate the virtual environment directly in the console using source .VENV/bin/activate it works as expected.

enter image description here

However, when I run the shell script with bash venv.sh including source .VENV/bin/activate it does not activate the virtual environment.

enter image description here


Solution:

source venv.sh works

1 Answers1

0

"Activating Virtual Environment" means (or at least includes) adjusting the current interactive shell (e.g. its prompt) somehow. .VENV/bin/activate is a script containing shell code to do so.

A shell can only adjust itself; this is why normally you source the mentioned script directly. See What is the difference between executing a Bash script vs sourcing it?

By putting source .VENV/bin/activate inside another script and running it with bash venv.sh (or even with ./venv.sh, if set up right), you create an additional bash process that interprets the script. The process is not your current interactive shell, it sources the original script, configures itself and exits. Your current interactive shell is unaffected.

With source venv.sh you make your current interactive shell interpret the shell code inside venv.sh. Then the only uncommented line there (source .VENV/bin/activate) makes the shell interpret the code from .VENV/bin/activate, so everything works.

Creating an additional script you need to source anyway seems overcomplicated. If you want to type less, consider defining a function (in ~/.bashrc):

acti () { source .VENV/bin/activate; }

Then just run it: acti.

Note .VENV/bin/activate is a relative path, so the function will properly work only in some specific directory. Maybe this is by design (i.e. the original script expects you to be in this exact directory when sourcing it), I don't know; but if not, then consider using the absolute path of the activate script.