[Tutor] Fwd: Re: Setting Command Line Arguments in IDLE

Mats Wichmann mats at wichmann.us
Sun May 26 11:37:49 EDT 2019


> On 26/05/2019 02:55, Richard Damon wrote:
>> I am working on a python script that will be provided arguments when run
>> from the system command line. Is there any place in IDLE to provide
>> equivalent arguments for testing while developing in IDLE?
>>
> 
> There used to be a dialog for that but in the latest version
> of IDLE I can't find it. I wonder when it disappeared and why?
> 
> The best place to ask is probably on the IDLE-dev list.
> 
> It can be found here:
> 
> https://mail.python.org/mailman/listinfo/idle-dev

I've seen this question come up on stack overflow, can't recall I've
seen a completely satisfactory answer.

I would suggest, however, that doing the testing you're considering
should be written as unit tests.  You can invoke unit tests from inside
the program by adding something like this (I'm a pytest fan, but it
could be unittest as well of course):

import pytest

# your code here

if __name__ == "__main__":
    pytest.main(["--capture=sys", "name-of-unittest-script.py"])

You can write your own argument array by fiddling with sys.argv; pytest
also provides a mechansim for injecting arguments (I think it's called
pytest_addoption).

The somewhat hacky way for a script to find out that it's running inside
IDLE (note: every time someone asks how to do this, a crowd of people
pop up and say "you don't want to be doing that".  But enabling a
testing scenario might actually be a time you want to?):

import sys

if "idlelib" in sys.modules:
    print("We're running in IDLE")



These aren't really an answer to what you're asking for, but maybe some
tools you might use to think further about the problem?



More information about the Tutor mailing list