mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
activate_this.py has always documented that it should be exec()ed with
locals = globals, and in virtualenv 16.0.0 it raises a NameError
otherwise.
As a simplified demonstration of the weird things that can go wrong
when locals ≠ globals:
>>> exec('a = 1; print([a])', {}, {})
[1]
>>> exec('a = 1; print([a for b in [1]])', {}, {})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'a' is not defined
>>> exec('a = 1; print([a for b in [1]])', {})
[1]
Top-level assignments go into locals, but from inside a new scope like
a list comprehension, they’re read out of globals, which doesn’t work.
Fixes #12030.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
19 lines
666 B
Python
19 lines
666 B
Python
"""
|
|
Use libraries from a virtualenv (by modifying sys.path) in production.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
venv = os.path.join(BASE_DIR, "zulip-py3-venv")
|
|
if sys.prefix != venv:
|
|
activate_this = os.path.join(venv, "bin", "activate_this.py")
|
|
# this file will exist in production
|
|
if os.path.exists(activate_this):
|
|
activate_locals = dict(__file__=activate_this)
|
|
exec(open(activate_this).read(), activate_locals)
|
|
if not os.path.exists(activate_locals["site_packages"]):
|
|
raise RuntimeError(venv + " was not set up for this Python version")
|