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?

2

1 Answer 1

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.

6
  • 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? Jun 14, 2018 at 4: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, 2021 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. Nov 26, 2021 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 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, 2021 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 sourceing, 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. Nov 26, 2021 at 13:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.