# X.py: Remove Python 2 support

**URL:** <https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045>\
**Category:** compiler\
**Created:** [July 19, 2021, 6:57pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045 "2021-07-19T18:57:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbeuw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cbeuw/32/7998_2.png) [@cbeuw](https://internals.rust-lang.org/u/cbeuw)\
**Post date:** [July 19, 2021, 6:57pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/1 "2021-07-19T18:57:17Z")

</div>

`bootstrap.py` was migrated from Python 2.7 to work with Python 3 and new features was written in a way that works both with Python 2.7 and 3 (except for XZ support used to download LLVM artifacts from CI). The [dev guide](https://rustc-dev-guide.rust-lang.org/building/prerequisites.html) promises 2.7 support as well. This meant that we can't even use things like f-strings. Although the Python side of the bootstrapper isn't constantly being changed I think sufficient time has passed since Python 2.7's EOL for us to fully commit to Python 3 and remove the baggage for any future changes to the script.

My motivation is that I'm currently rewriting `x.py clean` to be implemented in pure python (because it happened to me several times that I need to wait for a good 30 seconds for the Rust bootstrapper to compile, just to delete some files). Due to filesystem compatibility shenanigans I need a [feature](https://docs.python.org/3/library/stat.html#stat.FILE_ATTRIBUTE_REPARSE_POINT) added in Python 3.5. This cannot be backported in pure Python as it interacts with Windows API.

In terms of code change for "dropping support", we simply need to remove 2.7 from documentations and modify `x.py` such that it errors out when ran with 2.7 with appropirate message. Code style improvements can be made gradually later on.

---

<div class="post-metadata">

**Author:** ![tschuett](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tschuett/32/8380_2.png) [@tschuett](https://internals.rust-lang.org/u/tschuett)\
**Post date:** [July 19, 2021, 7:49pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/2 "2021-07-19T19:49:18Z")

</div>

I would go with removing python 2.7 support from the documentation and making it a hard error in x.py if you try something other than python3. Continue from there. You can later discus the minimal python3 supported version.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [July 19, 2021, 10:21pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/3 "2021-07-19T22:21:00Z")

</div>

I'd love to see this. I'd also love to see `x.py` automatically handling systems where `python3` exists and `python` (normally the entry point for Python 2, though some systems break backwards compatibility by pointing it at Python 3) does not.

---

<div class="post-metadata">

**Author:** ![cbeuw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cbeuw/32/7998_2.png) [@cbeuw](https://internals.rust-lang.org/u/cbeuw)\
**Post date:** [July 20, 2021, 8:20am UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/4 "2021-07-20T08:20:55Z")

</div>

I'm pretty sure `x.py` already does that: [Choose the version of python at runtime (portable version) by jyn514 · Pull Request #80625 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/80625)

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [July 20, 2021, 8:47am UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/5 "2021-07-20T08:47:40Z")

</div>

That handles the case where `python` == `python2` and `python3` also exists, it doesn’t handle the case where there is no executable named `python` at all.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [July 20, 2021, 12:14pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/6 "2021-07-20T12:14:28Z")

</div>

To handle `python` not existing at all, the most robust solution would probably be to write `x.sh` and `x.bat` wrappers that look first for `python3` and maybe also `python3.x` (x ∈ some plausible range of minor versions) before falling back to `python` and `py`.

---

<div class="post-metadata">

**Author:** ![jyn514](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jyn514/32/13245_2.png) [@jyn514](https://internals.rust-lang.org/u/jyn514)\
**Post date:** [July 20, 2021, 12:47pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/7 "2021-07-20T12:47:15Z")

</div>

> the most robust solution would probably be to write x.sh

And now you end up with `src/tools/x`, which is already in tree.

> I would go with removing python 2.7 support from the documentation and making it a hard error in x.py if you try something other than python3.

Please make an MCP for this. I still have a few machines running python2 by default, and I know several others on the compiler team doing the same. There should at least be a heads-up for them, so they can explain why they're using python2, instead of it suddenly being a hard error.

> I'd love to see this. I'd also love to see `x.py` automatically handling systems where `python3` exists and `python` (normally the entry point for Python 2, though some systems break backwards compatibility by pointing it at Python 3) does not.

These are separate issues, please don't confuse them. x.py will have trouble with python/python3 even if it completely drops supports for python2 (because `python3` doesn't exist on Windows): [Rust doesn't build without "python" name being available · Issue #71818 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/71818#issuecomment-623291126)

> My motivation is that I'm currently rewriting `x.py clean` to be implemented in pure python (because it happened to me several times that I need to wait for a good 30 seconds for the Rust bootstrapper to compile, just to delete some files).

This seems like a very strange use case. Why do you need to run x.py clean before even building bootstrap? If you do need to run it, why is `rm -r build` not sufficient? I would like to move as much functionality _out_ of x.py as possible; eventually I want to make it possible to `cargo run` bootstrap directly using `rust-toolchain`.

See also the discussion in [https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/RIIR%20contributor%20entry%20point/near/209247624](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/RIIR%20contributor%20entry%20point/near/209247624) and [https://rust-lang.zulipchat.com/#narrow/stream/238009-t-compiler.2Fmeetings/topic/planning.20meeting.202020.2E09.2E04/near/209245771](https://rust-lang.zulipchat.com/#narrow/stream/238009-t-compiler.2Fmeetings/topic/planning.20meeting.202020.2E09.2E04/near/209245771).

---

<div class="post-metadata">

**Author:** ![cbeuw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cbeuw/32/7998_2.png) [@cbeuw](https://internals.rust-lang.org/u/cbeuw)\
**Post date:** [July 20, 2021, 9:02pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/8 "2021-07-20T21:02:38Z")

</div>

> [@jyn514](#):
>
> I still have a few machines running python2 by default, and I know several others on the compiler team doing the same.

Are you saying `x.py` is ran with Python 2 first by the shebang line and then it's able to run `bootstrap.py` with Python 3 under a different executable name, or that these machines simply don't have Python 3 installed? If it's the former then nothing will break because we can retain the old `x.py` behaviour and only error out if Python 3 can't be found on the system under any sane name.

> [@jyn514](#):
>
> Why do you need to run x.py clean before even building bootstrap?

I don't, but I did need to run `x.py clean` on some occasions during my edit-build-debug cycle (mostly due to incremental compilation bugs), and sometimes this triggered a bootstrapper rebuild. The underlying reason was likely due to `rust-analyzer` running `cargo check` on save with some different flags than what `x.py` uses to build the bootstrapper and messing up Cargo's cache. I can't just do `rm -rf build` because one big point of `x.py clean` is that it retains the llvm artifacts, be that compiled locally or downloaded.

Changing parts of `config.toml` will also unnecessarily trigger a bootstrapper rebuild on running `x.py`, mostly in target-specific build sections (e.g. `[target.x86_64-unknown-linux-gnu]`) because they change things supplied to `cargo build` for the bootstrapper.

Another (minor) thing is that on Windows, a running executable cannot be deleted hence an executable cannot delete itself, so `x.py clean` can't remove `bootstrap.exe` (which is gracefully detected within the rust-written bootstrapper and prints a warning), leaving a non-empty `build` folder even when the user runs `x.py clean --all`, whose point is to remove `build` folder, including llvm artifacts.

**That said** there is an alternative to rewriting cleaning logic in Python, that is to "short circuit" and avoid building the bootstrapper for `x.py clean` if the bootstrap binary is already present, counting on the fact that any changes will not affect the cleaning behaviour. `x.py clean --all` can still be implemented in Python (requiring few lines of code) to fix the left-over `bootstrap.exe`.

---

<div class="post-metadata">

**Author:** ![jyn514](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jyn514/32/13245_2.png) [@jyn514](https://internals.rust-lang.org/u/jyn514)\
**Post date:** [July 20, 2021, 11:14pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/9 "2021-07-20T23:14:10Z")

</div>

> [@cbeuw](#):
>
> Are you saying `x.py` is ran with Python 2 first by the shebang line and then it's able to run `bootstrap.py` with Python 3 under a different executable name

Yes, that's what I mean. Ok, I'm less opposed to this change then (but I still think it should go through an MCP).

> I don't, but I did need to run `x.py clean` on some occasions during my edit-build-debug cycle (mostly due to incremental compilation bugs), and sometimes this triggered a bootstrapper rebuild. The underlying reason was likely due to `rust-analyzer` running `cargo check` on save with some different flags than what `x.py` uses to build the bootstrapper and messing up Cargo's cache.

Are you sure it's not just because `x.py clean` also cleans bootstrap artifacts? That wouldn't be solved by moving `clean` x.py; it would make the `clean` command faster, but everything else would still need a rebuild. Not cleaning the bootstrap binary by default seems like it would be more likely to help, I'm in favor of that.

> Changing parts of `config.toml` will also unnecessarily trigger a bootstrapper rebuild on running `x.py` , mostly in target-specific build sections (e.g. `[target.x86_64-unknown-linux-gnu]` ) because they change things supplied to `cargo build` for the bootstrapper.

That seems like a bug in itself, bootstrap should be built with the same flags regardless of what targets your building. Only the `build` option should affect it (and `build` is broken right now anyway, I've been meaning to make a PR).

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [July 24, 2021, 1:55pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/10 "2021-07-24T13:55:52Z")

</div>

> [@cbeuw](#):
>
> I can't just do `rm -rf build` because one big point of `x.py clean` is that it retains the llvm artifacts, be that compiled locally or downloaded.

For that reason I do `rm build/*/stage* -rf`.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 22, 2021, 1:56pm UTC](https://internals.rust-lang.org/t/x-py-remove-python-2-support/15045/11 "2021-10-22T13:56:03Z")

</div>

Closed on October 22, 2021, 1:56pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
