I am trying to activate my virtualenv (already existing) using the following python code:
Test.py
import os, sys
filename = "activate"
exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals)
print(os.system('pwd'))
if hasattr(sys, 'real_prefix'):
print('success')
else:
print('failed')
I then run this script via the terminal:
python Test.py
which then produces this error:
Traceback (most recent call last):
File "activate_this.py", line 3, in <module>
exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals)
File "activate", line 4
deactivate () {
^
SyntaxError: invalid syntax
I can activate the virtualenv successfully by executing cd env/bin
and then source activate
TLDR
Activating virtualenv from python script is throwing a syntax error from within the activate file.