Created
October 20, 2017 18:40
-
-
Save zzeleznick/542da0cdff29778cc6e2bdb8120ec147 to your computer and use it in GitHub Desktop.
Nested Virtualenvs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import pip | |
import imp | |
import pkg_resources | |
from os.path import join, expanduser, dirname, realpath | |
import virtualenv | |
def check_venv(expected_path): | |
"""Check if we are running in a virtual env""" | |
found_path = os.environ.get('VIRTUAL_ENV') | |
if found_path: | |
assert found_path == expected_path, ("virtualenv was not at " | |
"expected path {}, instead found {}".format(expected_path, found_path)) | |
return | |
print ("This file {} needs to be run within a virtualenv.\n" | |
"Activating now...").format(__file__) | |
activate_venv(expected_path) | |
def install_test_packages(prefix): | |
# pip install a package using the venv as a prefix | |
pip.main(["install", "--prefix", prefix, "async"]) | |
pip.main(["install", "--prefix", prefix, "xmltodict==0.10.0"]) | |
# os.system("pip install --prefix {} async".format(prefix)) | |
# os.system("pip install --prefix {} xmltodict".format(prefix)) | |
def activate_venv(venv_dir, create_environment=True): | |
print "\nGoing to activate venv within {}".format(venv_dir) | |
# sys.path = sys.path[:0] | |
# sys.executable = join(venv_dir, "bin", "python") | |
if create_environment: | |
# create and activate the virtual environment | |
virtualenv.create_environment(venv_dir) | |
activate_path = join(venv_dir, "bin", "activate_this.py") | |
execfile(activate_path, {'__file__': activate_path}) | |
# activate_path = join(venv_dir, "bin", "activate") | |
# os.system("source {}; printenv | grep VIRTUAL_ENV".format(activate_path)) | |
# print os.environ['PATH'] | |
# print sys.path | |
os.environ['VIRTUAL_ENV'] = venv_dir | |
''' | |
def test_import(): | |
try: | |
import async | |
except ImportError as e: | |
print "Cannot import async as expected: {}".format(e) | |
else: | |
raise Exception("Something went wrong...") | |
''' | |
start_venv = join(dirname(realpath(__file__)), "venv") | |
dest_venv = join(dirname(realpath(__file__)), "testenv") | |
check_venv(start_venv) | |
print list(pip.commands.freeze.freeze()) | |
print sys.modules.get("async", "No async") | |
xm1 = imp.load_source("xm1", imp.find_module("xmltodict")[1]) | |
for p in ["appnope", "async", "xmltodict", "pip"]: | |
try: | |
print imp.find_module(p) | |
# dist = pkg_resources.get_distribution(p) | |
# print "{} - {} {}".format(dist.project_name, dist.version, dist.location) | |
except Exception as e: | |
print e | |
print xm1.__version__ | |
print sys.executable | |
activate_venv(dest_venv) | |
check_venv(dest_venv) | |
install_test_packages(dest_venv) | |
print list(pip.commands.freeze.freeze()) | |
print sys.modules.get("async", "No async") | |
xm2 = imp.load_source("xm2", imp.find_module("xmltodict")[1]) | |
for p in ["appnope", "async", "xmltodict", "pip"]: | |
try: | |
print imp.find_module(p) | |
v = imp.load_source(p, imp.find_module(p)[1]) | |
# dist = pkg_resources.get_distribution(p) | |
# print "{} - {} {}".format(dist.project_name, dist.version, dist.location) | |
except Exception as e: | |
print e | |
print xm1.__version__, xm2.__version__ | |
print sys.executable | |
""" | |
activate_venv(start_venv) | |
check_venv(start_venv) | |
xm1 = imp.load_source("xmltodict", imp.find_module("xmltodict")[1]) | |
print xm1.__version__, xm2.__version__, sys.modules["__xm"].__version__ | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmltodict | |
ipython | |
virtualenv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment