Repository navigation
timeit basic examples are not compatible for Windows (CMD/ PowerShell) #106232
Description
Activity
both the examples worked for me in PowerShell 7.3.4. What version of PowerShell did you try it with?
PS Version 5.1.19041.3031
When runningpython3 -m timeit '"-".join(str(n) for n in range(100))'
I'm gettingoption -. not recognized use -h/--help for command line helpYes I just tested it on another machine, on Powershell v5.1 its not working but works on v7.3
Newer versions of PowerShell rewrite a command line that contains a single-quoted string to instead use double quotes, with literal double quote characters backslash escaped as needed. For example,
'"-".join(str(n) for n in range(100))'->"\"-\".join(str(n) for n in range(100))". This allows the C runtime in the Windows version of Python to parse the double-quoted string as a single command-line argument that contains literal double quote characters.In contrast, the CMD shell has no special handling of single quote characters in this context. It passes the command line exactly as written to the spawned process. The C runtime in the spawned Python process has no special handling of single quote characters when parsing a command line into an argument array. For example,
'"-".join(str(n) for n in range(100))'is parsed as an array of 5 arguments,["'-.join(str(n)", 'for', 'n', 'in', "range(100))'"], which obviously won't work.Also, most Python installations on Windows have no "python3" command. The exception, at least for official PSF releases, is the app distribution in the Microsoft Store, which enables a "python3.exe" appexec alias by default.
Examples should use the generic 'python' and work on Windows Command Prompt. People should substitute 'python' with 'python3', 'python3.y', 'py', 'py -3', 'py -3.y'. I presume " outside ' works on *nix. Working on PR.
- added a commit that references this issue
on Jul 1, 2023 Main already has 'python', not 'python3'. However, 'python' was 'python3' in 3.11, changed in 3.12. Hence a backport would require redoing the patch.
Documentation
on this page
https://docs.python.org/3/library/timeit.html
the example
python3 -m timeit '"-".join(str(n) for n in range(100))'should be
python3 -m timeit "'-'.join(str(n) for n in range(100))"because CMD and PowerShell do not support the first format
Linked PRs