28

I need to start venv / pyvenv from within a python script and I know the official documentation is to run:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

But I don't have an activate_this.py file and I can't find anywhere how to create one.

I am running python 3.4.1. Any idea what I need to do?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
clifgray
  • 4,313
  • 11
  • 67
  • 116
  • 1
    For anyone reading this I was using pyvenv and I suppose it just isn't a part of the 3.4 version yet. – clifgray Aug 06 '14 at 16:49
  • 2
    Did you get around this somehow? If so, I would really appreciate an answer here: [How can I activate a pyvenv vitrualenv from python? (activate_this.py was removed?)](http://stackoverflow.com/questions/27462582/how-can-i-activate-a-pyvenv-vitrualenv-from-python-activate-this-py-was-remove) – Chris Cooper Dec 13 '14 at 19:09

1 Answers1

27

As you've noted, pyvenv/the venv module doesn't ship with activate_this.py. But if you need this feature, you can borrow activate_this.py from virtualenv, put it in the expected location (virtualenv_path/bin/activate_this.py), then use it. It seems to work fine. Only issue is that the virtualenv docs are out of date for Python 3 (they claim you use execfile, which doesn't exist). The Python 3 compatible alternative would be:

activator = 'some/path/to/activate_this.py'  # Looted from virtualenv; should not require modification, since it's defined relatively
with open(activator) as f:
    exec(f.read(), {'__file__': activator})

Nothing activate_this.py does is magical, so you could manually perform the same changes without looting from virtualenv (adjusting PATH, sys.path, sys.prefix, etc.), but borrowing makes it much simpler in this case.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 6
    I literally spit up my drink when I read this answer :). But it totally works! If this is all their is to it, why isn't just a part of python venv? – nackjicholson Jun 14 '18 at 04:05
  • I must be missing something but it just does not work for me. In my PWD I create a virtualenv in `.venv` then I create a `file.py` with exactly the code you provided, except the path to `activate_this.py` is changed ofc. Then I run `python3 file.py` but my prompt does not change to `(.venv) ...` I am using Python 3.8.10. Does anybody know what am I doing wrong? Thank you very much. – Paloha Nov 26 '21 at 13:16
  • @Paloha: `activate_this.py` is how you activate the virtual environment from within a Python script, for the life of that script (and its child processes). It's impossible for it to change the shell it's running within (it's a child process, which can't reach back and alter the environment of the parent process). The only way to change the shell itself persistently is the normal route of `source /path/to/venv/bin/activate` (adding an appropriate file extension if needed based on shell), which is typically what you should do; this is for special cases where the script must do it to itself. – ShadowRanger Nov 26 '21 at 13:30
  • @ShadowRanger, thank you very much for your quick reply. I thought this is the case, but this answer seems to state otherwise https://stackoverflow.com/a/14792407/8691571. That is why I was giving this approach a go. In case you are right, I will solve it just by wrapping my python script with a bash script to which I will pass the path and source it from there. Thx. – Paloha Nov 26 '21 at 13:34
  • 1
    @Paloha: Yeah, that answer is talking about three layers: 1) The non-Python shell, 2) The initially launched Python process, 3) Optional child Python processes launched with `subprocess`. `activate_this.py` is used when you don't want to `source` in #1, nor write a wrapper bash script to do the `source`ing, and you need #2 to be in the venv, even though it wasn't active when it was launched (that where the end of that answer comes in). The rest of that answer is focused on #2 remaining outside the venv, and #3 being in it. – ShadowRanger Nov 26 '21 at 13:38
  • @ShadowRanger thanks once again, you saved me quite some time because I am stubborn and would pursue this thinking I am just doing something wrong. – Paloha Nov 26 '21 at 13:42