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?

asked Sep 6, 2019 at 8:48
1
  • PYTHONPATH is an environment variable may be this post will work Commented Sep 6, 2019 at 9:02

1 Answer 1

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.

answered Sep 6, 2019 at 9:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.