commit | 5cda24fbb171f62bf59f3065de5119c40b185df0 | [log] [tgz] |
---|---|---|
author | Shuhei Takahashi <nya@chromium.org> | Fri Jul 07 18:43:23 2017 +0900 |
committer | chrome-bot <chrome-bot@chromium.org> | Wed Jul 19 01:23:38 2017 -0700 |
tree | 1c54521ed51476af483104319a006ad96270fad4 | |
parent | 704dbb824410432fcd5902ed41509f40735e7fd8 [diff] |
infra_virtualenv: Introduce python_venv. This change will introduce a new script `python_venv` which transparently executes a Python interpreter in a virtualenv, possibly creating a new virtualenv if it has not been created yet. This script is a better replacement to `find_virtualenv.sh` because: 1. `python_venv` does not require to be sourced from bash. This allows any program (e.g. Python) to run a Python interpreter in a virtualenv. Also it makes it easy to run an interactive Python shell in a virtualenv, which is convenient to manually inspect the virtualenv environment for debug. 2. `python_venv` can be called from any working directory, which makes the caller code simpler. BUG=chromium:733103 TEST=bin/run_tests TEST=bin/turtle Change-Id: I94e85a3f3e5ec31d29094e273f0ff9466a8405a2 Reviewed-on: https://chromium-review.googlesource.com/563279 Commit-Ready: Shuhei Takahashi <nya@chromium.org> Tested-by: Shuhei Takahashi <nya@chromium.org> Reviewed-by: Shuhei Takahashi <nya@chromium.org>
This repository provides a common Python virtualenv interface that infra code (such as chromite) can depend on.
Virtualenv users should mimic this repository, which itself uses virtualenv for running unit tests.
venv
directory. All packages and modules in this directory will be importable inside the virtualenv.requirements.txt
file inside venv
to list external packages to install.bin/python_venv
and bin/turtle
which serve as templates.To add packages to this repository, run:
$ pip wheel -w path/to/pip_packages -r path/to/requirements.txt
Commit the changes and make a CL.
The bin/create_venv
script prepares a virtualenv using a requirements.txt
file.
$ bin/create_venv requirements.txt
The script will print the path to the virtualenv to stdout. Note that the output ends with a newline; Bash handles this, but Python does not.
To run the virtualenv Python, call bin/python
under the virtualenv directory.
Together, this might look up:
$ venv=$(bin/create_venv requirements.txt) $ ${venv}/bin/python
NOTE: it is not generally safe to run the other scripts in the virtualenv's bin
directory due to hard-coded paths. Instead of running bin/pip
for example, use bin/python -m pip
.
NOTE: Do not use this for third party dependencies (stuff not owned by ChromiumOS)! This should only be used to set up imports for stuff we own. For example, importing python-MySQL SHOULD NOT use this, but importing chromite from Autotest MAY use this.
This should be handled by the minimum amount of code in the package's __init__.py
file.
Example:
"""Autotest package.""" import sys # Use the minimum amount of logic to find the path to add _chromite_parent = 'site-packages' sys.path.append(_chromite_parent)
A solid understanding of the Python import system is recommended (the link is for Python 3, but it is informative).
In brief, __init__.py
is executed whenever a package is imported. A package is imported before any submodule or subpackage is imported. A package is only imported once per Python process; future imports are looked up in sys.modules
. Thus, __init__.py
will modify sys.path
exactly once and is guaranteed to be run before anything in that package is imported.