File size: 1,142 Bytes
3883c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys

from .commands import run_command, get_python
from .os import is_windows
import os

venv_name = 'venv'
venv_activate_path = f'{venv_name}/' + ('Scripts/activate.bat' if is_windows() else 'bin/activate')


def get_base_prefix_compat():
    """Get base/real prefix, or sys.prefix if there is none."""
    return getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix


def in_venv():
    in_conda = os.environ.get('CONDA_PREFIX') is not None
    return (get_base_prefix_compat() != sys.prefix) or in_conda



def activate_venv():
    if in_venv():
        return
    if not os.path.isdir(venv_name):
        print('no venv found, creating venv')
        run_command(f'"{get_python()}"', '-m venv venv')
    run_command([('call' if is_windows() else 'source', venv_activate_path), ('python', ' '.join([f'"{arg}"' for arg in sys.argv]))])  # Launch the main.py with the venv
    exit()  # Exit after the venv'ed version exits (maximum depth will be 2 because the venv is already activated in that case)


def ensure_venv():
    if not in_venv():
        print('activating venv')
        activate_venv()