indygreg / indygreg/PyOxidizer
`TypeError: expected str, not NoneType` when using `tkinter` due to `sys.argv = [None]`
- Dominant language
- Rust
- Stars
- 6.2k
- Forks
- 256
- PR merge metrics
- No merged PRs in 30d
Description
Hi, thanks for the awesome project!
Some info:
* Platform: Windows 10
* Tested with both Python 3.8 and 3.9 (though my system Python has always been just 3.8)
* Didn't test with Python 3.10, but from what I can see from `tkinter`'s source code, nothing has changed.
I have a pretty basic `pyoxidizer.bzl` ([gist](https://gist.github.com/fofoni/284c2e79657465d65356bd434988fd17)). When the executable reaches the line `root = tkinter.Tk()` in my `.py` module (i.e. the interpreter has already successfully imported `tkinter`), it fails [here](https://github.com/python/cpython/blob/v3.8.12/Lib/tkinter/__init__.py#L2265) with `TypeError: expected str, bytes or os.PathLike object, not NoneType`. This is because apparently PyOxidizer is setting `sys.argv = [None]`, but `tkinter.Tk.__init__` expects `sys.argv[0]` to be a string.
Fortunately for me, `tkinter` [doesn't actually *use*](https://github.com/python/cpython/blob/v3.8.12/Modules/_tkinter.c#L3227) `sys.argv[0]`, it's apparently just dead code in Python's implementation. My problem goes away when I do `tkinter.Tk(baseName="")` instead of `tkinter.Tk()`.
I have two questions. First is: I searched a lot but didn't find this in the documentation (in particular, not [here](https://pyoxidizer.readthedocs.io/en/stable/pyoxidizer_packaging_tkinter.html)), and also didn't find this reported anywhere. Am I doing something too different from what people usually do to use tkinter from PyOxidizer? Why hasn't anyone tripped on this before?
Second question: shouldn't PyOxidizer set `sys.argv[0]` to a string? Either something like `""` or `""`, or maybe the path to the `.exe` being executed, or even just the name of the `.exe` file? Or does this make no sense on Windows? (I'm not used to coding on Windows.)
If the answer to the second question is yes, I'd be willing to send a PR. Otherwise, I'm not sure how I could help; maybe try patching the Python used in `default_python_distribution()` to allow `tkinter` to work with `sys.argv[0]=None`? I see you haven't released Python 3.8.13 yet.
Thanks again!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the linked pyoxidizer.bzl reproduction and the tkinter.Tk() failure described at CPython Lib/tkinter/__init__.py line 2265. Read the PyOxidizer tkinter packaging documentation and the default_python_distribution() entry point to determine where sys.argv is initialized. Done means the Windows reproduction no longer raises the TypeError, with the intended sys.argv[0] behavior documented or covered by an appropriate test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100