1

When I use bash script to create Python virtual environment, all environment (interpreter, pip and other packages) are system-wide packages.

#!/bin/bash
virtualenv <venv name> --python python3

I check it via the following commands:

which python3
which pip3

and

pip3 list

However, if I type the command manually, it works. I got an isolated environment.

Can someone tell me why it happens and how to use scripts to create Python virtual environments? (Because I want to make it automatic via scripts). Thanks a lot.

NOTE: I am not trying to activate the environment in the wrong way. I do use source to activate it and get the mark in the head of my command line. I am asking about why creating the virtual environment in a bash script will not copy the binary file of interpreter and pip !!! That will miss the point to use a virtual environment !!! And to my understanding, no matter in which bash session (the current one or the subprocess) I execute the creating command, the result should be the same. That is the problem.

SaltedFishLZ
  • 157
  • 1
  • 9
  • https://stackoverflow.com/search?q=%5Bvirtualenv%5D+activate+bash+script – phd Apr 23 '19 at 10:17
  • Hi, this question is about to activate a virtual environment. However, my problem is to create a virtual environment. And to my understanding, no matter I create the virtual environment in which bash session, it should be the same. I wonder why it isn't. I do activate the environment using 'source', and after activating it, I found the python interpreter is not isolated, but the mark is still in front of the command line. – SaltedFishLZ May 07 '19 at 22:23

1 Answers1

0

Source the script instead of executing it.

Check out https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it

danrodlor
  • 1,329
  • 8
  • 16
  • Hi, all these clarify why activating the environment in bash script doesn't work. But, I think no matter which environment, the creating process should work the same. That is the problem. I do activate the environment using 'source', and after activating it, I found the python interpreter is not isolated, but the mark is still in front of the command line. – SaltedFishLZ May 07 '19 at 22:25
  • Mmmm...okey, now the question is far more clear! So, if I understood, you're creating your virtual environment (virtualenv -p python3 ) and then you're activating it (source /bin/activate) in the proper way, but it results in a non-isolated python environment (as it should), right? Check if the environment variable "PYTHONPATH" is set in your shell, if it is, unset it before activating the virtual environment. – danrodlor May 08 '19 at 06:46
  • I am not sure about whether your suggestion works. After a group server update, the problem cannot be reproduced. I don't know why. – SaltedFishLZ May 14 '19 at 07:47