An interactive pip requirements upgrader that also updates the version in your requirements.txt file
—
Virtual environment detection and validation to ensure packages are installed in the appropriate environment.
Detects whether the current Python process is running within a virtual environment using multiple detection methods.
def is_virtualenv():
"""
Check if currently running in a virtual environment.
Returns:
bool: True if running in a virtual environment
Detection methods:
1. sys.base_prefix vs sys.prefix comparison (venv, virtualenv)
2. sys.real_prefix attribute existence (virtualenv)
3. VIRTUAL_ENV environment variable (conda, virtualenv)
Compatibility:
- Works with venv (Python 3.3+)
- Works with virtualenv (all versions)
- Works with conda environments
- Cross-platform (Windows, macOS, Linux)
"""Validates virtual environment status and warns users when not in a virtual environment.
def check_for_virtualenv(options):
"""
Check for virtualenv and warn if not activated.
Args:
options (dict): Command-line options containing:
--skip-virtualenv-check (bool): Skip validation if True
--skip-package-installation (bool): Skip validation if True
Raises:
KeyboardInterrupt: If not in virtualenv and checks are not skipped
Warning behavior:
- Displays colored warning message with recommendations
- Suggests activating virtualenv or using skip options
- Allows user to abort installation
"""Uses sys.base_prefix vs sys.prefix comparison:
import sys
# In regular Python: base_prefix == prefix
# In venv: base_prefix != prefix
if getattr(sys, 'base_prefix', sys.prefix) != sys.prefix:
return TrueChecks for sys.real_prefix attribute:
import sys
# virtualenv sets sys.real_prefix
# Regular Python and venv don't have this attribute
if hasattr(sys, 'real_prefix'):
return TrueChecks VIRTUAL_ENV environment variable:
import os
# Set by virtualenv, venv, and conda
if os.environ.get('VIRTUAL_ENV'):
return TrueWhen not in a virtual environment:
It seems you haven't activated a virtualenv.
Installing packages directly in the system is not recommended.
Activate your project's virtualenv, or re-run this command with one of the following options:
--skip-virtualenv-check (install the packages anyway)
--skip-package-installation (don't install any package. just update the requirements file(s))Message features:
Validation is skipped when:
if options.get('--skip-virtualenv-check', False) or \
options.get('--skip-package-installation', False):
return # No check neededfrom pip_upgrader.virtualenv_checker import is_virtualenv
# Check if in virtual environment
if is_virtualenv():
print("Running in virtual environment")
else:
print("Not in virtual environment")from pip_upgrader.virtualenv_checker import check_for_virtualenv
# Validate environment with standard options
options = {
'--skip-virtualenv-check': False,
'--skip-package-installation': False
}
try:
check_for_virtualenv(options)
print("Environment validation passed")
except KeyboardInterrupt:
print("Environment validation failed or user cancelled")# Skip validation explicitly
options = {'--skip-virtualenv-check': True}
check_for_virtualenv(options) # Returns immediately
# Skip validation when not installing packages
options = {'--skip-package-installation': True}
check_for_virtualenv(options) # Returns immediatelyCreated with:
python -m venv myenv
source myenv/bin/activate # Unix
myenv\Scripts\activate # WindowsDetection characteristics:
sys.base_prefix != sys.prefixVIRTUAL_ENV environment variable setsys.real_prefix attributeCreated with:
virtualenv myenv
source myenv/bin/activate # Unix
myenv\Scripts\activate # WindowsDetection characteristics:
sys.real_prefix attribute existsVIRTUAL_ENV environment variable setsys.base_prefix != sys.prefixCreated with:
conda create -n myenv python=3.9
conda activate myenvDetection characteristics:
VIRTUAL_ENV environment variable set (usually)CONDA_DEFAULT_ENV environment variableRegular Python installation without virtual environment:
Detection characteristics:
sys.base_prefix == sys.prefix (if base_prefix exists)sys.real_prefix attributeVIRTUAL_ENV environment variable# Activation scripts
myenv\Scripts\activate.bat # Command Prompt
myenv\Scripts\Activate.ps1 # PowerShell
# Detection works with Windows paths
VIRTUAL_ENV=C:\Users\user\myenv# Activation script
source myenv/bin/activate
# Detection works with Unix paths
VIRTUAL_ENV=/home/user/myenvsys.prefix paths are platform-appropriateCalled early in the main CLI workflow:
def main():
options = get_options()
Windows.enable(auto_colors=True, reset_atexit=True)
try:
# Virtual environment check happens first
check_for_virtualenv(options)
# Continue with requirements detection, etc.
# ...
except KeyboardInterrupt:
print(Color('\n{autored}Upgrade interrupted.{/autored}'))When validation fails:
KeyboardInterruptTwo ways to bypass validation:
Installing packages directly to system Python can:
Virtual environments provide:
The validator recommends virtual environments because:
{autoyellow}It seems you haven't activated a virtualenv.
Installing packages directly in the system is not recommended.
{automagenta}Activate your project's virtualenv{/automagenta}, or {automagenta}re-run this command{/automagenta} with one of the following options:
--skip-virtualenv-check (install the packages anyway)
--skip-package-installation (don't install any package. just update the requirements file(s)){/autoyellow}Color coding:
Install with Tessl CLI
npx tessl i tessl/pypi-pip-upgrader