You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

4 years ago
  1. """Activate virtualenv for current interpreter:
  2. Use exec(open(this_file).read(), {'__file__': this_file}).
  3. This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
  4. """
  5. import os
  6. import site
  7. import sys
  8. try:
  9. __file__
  10. except NameError:
  11. raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")
  12. # prepend bin to PATH (this file is inside the bin directory)
  13. bin_dir = os.path.dirname(os.path.abspath(__file__))
  14. os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
  15. base = os.path.dirname(bin_dir)
  16. # virtual env is right above bin directory
  17. os.environ["VIRTUAL_ENV"] = base
  18. # add the virtual environments site-package to the host python import mechanism
  19. IS_PYPY = hasattr(sys, "pypy_version_info")
  20. IS_JYTHON = sys.platform.startswith("java")
  21. if IS_JYTHON:
  22. site_packages = os.path.join(base, "Lib", "site-packages")
  23. elif IS_PYPY:
  24. site_packages = os.path.join(base, "site-packages")
  25. else:
  26. IS_WIN = sys.platform == "win32"
  27. if IS_WIN:
  28. site_packages = os.path.join(base, "Lib", "site-packages")
  29. else:
  30. site_packages = os.path.join(base, "lib", "python{}".format(sys.version[:3]), "site-packages")
  31. prev = set(sys.path)
  32. site.addsitedir(site_packages)
  33. sys.real_prefix = sys.prefix
  34. sys.prefix = base
  35. # Move the added items to the front of the path, in place
  36. new = list(sys.path)
  37. sys.path[:] = [i for i in new if i not in prev] + [i for i in new if i in prev]