This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ofekmeister
Recipients Ofekmeister, paul.moore, steve.dower, tim.golden, zach.ware
Date 2017-06-27.02:23:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1498530184.51.0.0376373958552.issue30783@psf.upfronthosting.co.za>
In-reply-to
Content
The following example shows that we are indeed changing PATH, but the subprocess does not acknowledge it in Windows 7 x64. Also note this works in Linux (Ubuntu 16.04).

-----

import os
import subprocess
from contextlib import contextmanager
from tempfile import TemporaryDirectory


def get_python_path():
    return subprocess.check_output(
        ['python', '-c', 'import sys;print(sys.executable)']
    ).decode().strip()


@contextmanager
def temp_chdir(cwd=None):
    with TemporaryDirectory() as d:
        origin = cwd or os.getcwd()
        os.chdir(d)

        try:
            yield d
        finally:
            os.chdir(origin)


def create_venv(d, pypath=None):
    command = ['virtualenv', d]
    if pypath:
        command.extend(['-p', pypath])
    subprocess.call(command)


@contextmanager
def venv(d):
    if os.path.exists(os.path.join(d, 'bin')):  # no cov
        venv_exe_dir = os.path.join(d, 'bin')
    elif os.path.exists(os.path.join(d, 'Scripts')):
        venv_exe_dir = os.path.join(d, 'Scripts')
    else:
        raise OSError('Unable to locate executables directory.')

    old_path = os.environ['PATH']
    os.environ['PATH'] = '{}{}{}'.format(venv_exe_dir, os.pathsep, old_path)
    yield
    os.environ['PATH'] = old_path


def test_venv():
    with temp_chdir() as d:
        d = os.path.join(d, 'test_env')
        create_venv(d)
        global_python = get_python_path()
        print('PATH', os.environ['PATH'][:140])

        with venv(d):
            print('PATH', os.environ['PATH'][:140])
            venv_python = get_python_path()

        assert global_python != venv_python
        assert global_python == get_python_path()
History
Date User Action Args
2017-06-27 02:23:04Ofekmeistersetrecipients: + Ofekmeister, paul.moore, tim.golden, zach.ware, steve.dower
2017-06-27 02:23:04Ofekmeistersetmessageid: <1498530184.51.0.0376373958552.issue30783@psf.upfronthosting.co.za>
2017-06-27 02:23:04Ofekmeisterlinkissue30783 messages
2017-06-27 02:23:03Ofekmeistercreate