Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to specify Python minor version when running
./pants
(#…
…7187) ## Problem There are three issues when trying to run `./pants3` and switching between Python 3.6 and Python 3.7. 1) You must first `rm -rf build-support/pants_dev_deps.py3.venv` and then `rm -rf ~/.cache/pants/python_cache/interpreters` because the cache will have a bad symlink to the prior Python 3 interpreter. This is inconvenient, and the latter is surprising and difficult to remember. 2) If both Python 3.6 and Python 3.7 are discoverable on the `PATH`, there is no reliable way to specify that you want to use Python 3.7. The `virtualenv` code works by invoking `python3`, which defaults in most systems to the minimum version. The only workaround is to ensure `python3` points first to `python3.7` by messing with the PATH. While this is possible, it is not obvious to the user and it would be best to provide a solution that does not require messing with the PATH. 3) When running `./pants3`, we always override `PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS` to allow Python 3.6, so it is impossible at the moment to set a tighter constraint without modifying source code. Even if we resolve issues 1 and 2, we must address this issue to ensure that spawned subprocesses use the correct interpreter. ## Solution ### Part 1 - renamed `venv` folders Rename `py2.venv` -> `py27.venv` and split out `py3.venv` into `py36.venv` and `py37.venv`. This allows us to have a distinct venv for each Python version, which is necessary to avoid redownloading/resymlinking dependencies anytime we change from 3.6 to 3.7. It is also necessary to fix problem #1 of having to run `rm -rf build-support/pants_dev_deps.py3.venv ~/.cache/pants/python_cache/interpreters` whenever making this change. ### Part 2 - `$PY` env var We refactor `virtualenv` and `pants_venv` to require the caller to pre-set the env var `$PY`. This value stores the bin name, such as `python3.7`, to be used in setting up the venv. If the user wants to constrain `./pants3` to a specific Python 3 interpreter, they may do so by prefixing the command with `PY=python3.7 ./pants3`. Otherwise, `./pants` will default to `python2.7` and `./pants3` will default to `python3` as before. ### Also changed - renames in `.travis.yml` Update `ci.sh` and `.travis.yml` to specify the exact Python versions as Python 2.7 and 3.6, rather than Python 2 and Python 3. `ci.sh` does make a semantic change in that it now requires exactly Python 3.6 for Py3 shards, whereas earlier it only used Py3.6 de facto. Meanwhile, `.travis.yml` changes the OSX rust tests to use Python 2.7 to fix an issue with requiring exactly Python 3.6, updates the cache directories to use the new `venv` naming scheme, and renames `py2`->`py27` and `py3`->`py36` everywhere. This is pre-work to make the Python 3.7 CI pull request easier to review. ## Alternative solutions considered ### `./pants3` interpreter flags Originally this PR added the CI flags `./pants -6` and `./pants -7` to allow forcing Pants to use only Python 3.6 or 3.7, respectively. This solution was rejected for being over-constrained and too complex. ### `./pants37` Originally we considered adding a new script `./pants37` to specify Python 3.7, rather than `./pants3 -7`. This was rejected because it results in too many root-level scripts that are prefixed `./pants`, which is confusing. Further, it is not often that Pants devs will need to switch between Python 3.6 and 3.7—we certainly will not expect them to do this frequently (whereas we do encourage frequently changing between `./pants` and `./pants3`). We merely want the ability to run with Python 3.7, particularly for CI; it is not important for the option to be especially discoverable. ## Result Calling `./pants` or `./pants3` for the first time will delete the legacy venv folders and use the new naming scheme. Scripts now behave as follows: * `./pants` will use Python 2.7 as before * `./pants3` will use the first `python3` bin on the PATH, as before * `PY=python3.6 ./pants3` will use only Python 3.6 * `PY=python3.7 ./pants3` will use only Python 3.7 Switching between these options will trigger an engine rebuild the first time, and then after that will change the interpreter used with no cost.
- Loading branch information