1

I'm using the Ubuntu app in windows 10 and I'm running into a problem when trying to start the Python environment from a script.

I've installed python and i can run python scripts when i set my environment from the command line but when I try and set it using a script nothing changes.

I can run this from my command line fine:

source /home/bootys/environments/my_env/bin/activate

But when I run the same thing in a script nothing changes:

#!/bin/bash
echo "Setting to Python Environment"
source /home/bootys/environments/my_env/bin/activate
if [ $? -eq 0 ]
then
echo "Environment has been set to Python"
else
echo "Failed to set environment"
fi

I'm expecting to see my environment being set to this, which is what happens when it is run from the command line:

(my_env) bootys@ThisPC:~/MyScripts$

But nothing changes even though the exit status check returns successful.

Is the script run in a sub shell? If so whats the practice to source my environment from a script?

Bobybobp
  • 95
  • 9
  • PYTHONPATH is an environment variable may be [this](https://stackoverflow.com/questions/18247333/python-pythonpath-in-linux) post will work – Akash Kumar Sep 06 '19 at 09:02

1 Answers1

1

source means "execute the script in the current shell", which is why source bin/activate works; it updates the environment variables in the current shell.

But when you run a separate script, that starts a new subshell. The activate script is then run in that subshell, setting the environment to the new virtualenv, but it then exits, leaving your current shell exactly as it was.

You could of course execute your script via source, but at that point you might as well just source the activate script directly.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895