2

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.

phd
  • 57,284
  • 10
  • 68
  • 103
Jordan Savage
  • 176
  • 2
  • 15
  • why not do this properly and activate it from a shell script and then run your python in there? not to mention `activate` is a `sh` script so not sure how you plan to execute it with python – gold_cy Jan 29 '19 at 11:56
  • ^^ somehow I highly doubt that, in all my work experience I have never seen it done once this way, typically you make a `sh` script that activates your env and then runs your python program – gold_cy Jan 29 '19 at 11:59
  • 2
    You cannot `compile()` a shell script... – Ludo Jan 29 '19 at 12:00
  • why are you trying to use compile on a bash script (activate)? apart of that, activate changes env variables of the current env to set the requested python executable and paths - running this from (another?) python executable ([even if it works](https://docs.python.org/2/library/os.html#os.environ)) seems completely nonsensical - why would you do that? – Yuri Feldman Jan 29 '19 at 12:04
  • ok then you need to [make a system call](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python), probably `source activate.sh` should work – Yuri Feldman Jan 29 '19 at 12:09
  • 2
    Possible duplicate of [Activate a virtualenv with a python script](https://stackoverflow.com/questions/6943208/activate-a-virtualenv-with-a-python-script) – phd Jan 29 '19 at 15:24
  • https://stackoverflow.com/search?q=%5Bvirtualenv%5D+activate+python+script – phd Jan 29 '19 at 15:24
  • @phd my post is activating a virtualenv from within a python script – Jordan Savage Jan 29 '19 at 15:26
  • Yes, this is exactly what you are trying to do. – phd Jan 29 '19 at 15:27

1 Answers1

1

The very 1st line of activate (note that VEnv is installed on Win, but this shouldn't be a problem):

# This file must be used with "source bin/activate" *from bash* 

That, and the lines below should tell you that activate is a (Bourne) shell file.

[Python 3]: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) on the other hand, works with Python source code.

So, in order to execute the file, you'd need to use other ways, e.g. [Python 3]: subprocess - Subprocess management.
You can check how I've used it: [SO]: How to effectively convert a POSIX path to Windows path with Python in Cygwin? (@CristiFati's answer).

But, I really don't see the point of doing all this, you probably misunderstood your colleague's suggestion.
Also note that even if you do manage to do it this way, all the environment variables will only be set in the calling process, so it will pretty much be unusable (well, unless you also execute your script from there too).

You should go the recommended way ([PyPA]: Virtualenv - User Guide), and that is (from bash):

source /path/to/Django/ENV/bin/activate
python your_project_startup_script.py  # (as I recall, it's manage.py)
CristiFati
  • 28,721
  • 9
  • 41
  • 63