Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 486 – Make the Python Launcher aware of virtual environments

Author:
Paul Moore <p.f.moore at gmail.com>
Status:
Final
Type:
Standards Track
Created:
12-Feb-2015
Python-Version:
3.5
Post-History:
12-Feb-2015
Resolution:
Python-Dev message

Table of Contents

Abstract

The Windows installers for Python include a launcher that locates thecorrect Python interpreter to run (seePEP 397). However, thelauncher is not aware of virtual environments (virtualenv[1] or PEP405 based), and so cannot be used to run commands from the activevirtualenv.

This PEP proposes making the launcher “virtualenv aware”. This meansthat when run without specifying an explicit Python interpreter touse, the launcher will use the currently active virtualenv, if any,before falling back to the configured default Python.

Rationale

Windows users with multiple copies of Python installed need a means ofselecting which one to use. The Python launcher provides thisfacility by means of apy command that can be used to run either aconfigured “default” Python or a specific interpreter, by means ofcommand line arguments. So typical usage would be:

# Run the Python interactive interpreterpy# Execute an installed modulepy-mpipinstallpytestpy-mpytest

When using virtual environments, thepy launcher is unaware that avirtualenv is active, and will continue to use the system Python. Sodifferent command invocations are needed to run the same commands in avirtualenv:

# Run the Python interactive interpreterpython# Execute an installed module (these could use python -m,# which is longer to type but is a little more similar to the# launcher approach)pipinstallpytestpy.test

Having to use different commands is error-prone, and in many casesthe error is difficult to spot immediately. The PEP proposes makingthepy command usable with virtual environments, so that the firstform of command can be used in all cases.

Implementation

Bothvirtualenv and the corevenv module set an environmentvariableVIRTUAL_ENV when activating a virtualenv. This PEPproposes that the launcher checks for theVIRTUAL_ENV environmentvariable whenever it would run the “default” Python interpreter forthe system (i.e., when no specific version flags such aspy-2.7are used) and if present, run the Python interpreter for thevirtualenv rather than the default system Python.

The “default” Python interpreter referred to above is (as perPEP 397)either the latest version of Python installed on the system, ora version configured via thepy.ini configuration file. When theuser specifies an explicit Python version on the command line, thiswill always be used (as at present).

Impact on Script Launching

As well as interactive use, the launcher is used as the Windows fileassociation for Python scripts. In that case, a “shebang” (#!)line at the start of the script is used to identify the interpreter torun. A fully-qualified path can be used, or a version-specific Python(python3 orpython2, or evenpython3.5), or the genericpython, which means to use the default interpreter.

The launcher also looks for the specific shebang line#!/usr/bin/envpython. On Unix, theenv program searches for acommand on$PATH and runs the command so located. Similarly, withthis shebang line, the launcher will look for a copy ofpython.exeon the user’s current%PATH% and will run that copy.

As activating a virtualenv means that it is added toPATH, nospecial handling is needed to run scripts with the active virtualenv -they just need to use the#!/usr/bin/envpython shebang line,exactly as on Unix. (If there is no activated virtualenv, and nopython.exe onPATH, the launcher will look for a defaultPython exactly as if the shebang line had said#!python).

Exclusions

The PEP makes no attempt to promote the use of the launcher forrunning Python on Windows. Most existing documentation assumes theuser ofpython as the command to run Python, and (for example)pip to run an installed Python command. This documentation is notexpected to change, and users who choose to manage theirPATHenvironment variable can continue to use this form. The focus of thisPEP is purely on allowing users who prefer to use the launcher whendealing with their system Python installations, to be able to continueto do so when using virtual environments.

Reference Implementation

A patch implementing the proposed behaviour is available athttp://bugs.python.org/issue23465

References

[1]
https://virtualenv.pypa.io/

Copyright

This document has been placed in the public domain.


Source:https://github.com/python/peps/blob/main/peps/pep-0486.rst

Last modified:2025-02-01 08:59:27 GMT


[8]ページ先頭

©2009-2025 Movatter.jp