Skip to content

Add '-m mod : run library module as a script' option.#1352

Merged
minrk merged 5 commits into
ipython:masterfrom
bfroehle:run_module
Feb 8, 2012
Merged

Add '-m mod : run library module as a script' option.#1352
minrk merged 5 commits into
ipython:masterfrom
bfroehle:run_module

Conversation

@bfroehle
Copy link
Copy Markdown
Contributor

@fperez is very prescient...

Looks like we have a Pull Request coming from you soon :)

Closes gh-1350.

@minrk
Copy link
Copy Markdown
Member

minrk commented Jan 30, 2012

Should 'run_module' come first or last, assuming more than one such code-to-run is given?

I'm fine with a decision of 'behavior undefined' for such situations (which are probably rare), but a public decision would be useful.

@bfroehle
Copy link
Copy Markdown
Contributor Author

The python executable only allows one of -c or -m (by terminating the options list). shrug

@minrk
Copy link
Copy Markdown
Member

minrk commented Jan 30, 2012

IPython will not stop parsing args at '-c' or '-m', so if that should happen, more work would need to be done:

$> ipython -m easy_install pyzmq --user
<snip>
[TerminalIPythonApp] Bad config encountered during initialization:
[TerminalIPythonApp] Unrecognized flag: '--user'

@bfroehle
Copy link
Copy Markdown
Contributor Author

Well, I don't think there is much that can be done without changing the IPython argument parser. However, it may be possible to one day support syntax like

$ ipython -m easy_install -- pyzmq --user

where the -- separates ipython flags from extra arguments. That is likely beyond the scope of this pull request.

As such, the current patch is useful for running modules which take no arguments, and in the last commit I took the similar sys.argv handling from _exec_file(), which will allow some arguments.

@minrk
Copy link
Copy Markdown
Member

minrk commented Jan 30, 2012

Yes, it will work if no arguments are to be passed, which is already a nice feature.

We already use -- to stop parsing, but even that won't quite work because the first positional argument (the first one past --) is interpreted as file_to_run. This action should probably be skipped if the module is specified.

The relevant line: https://github.com/ipython/ipython/blob/master/IPython/frontend/terminal/ipapp.py#L309

@bfroehle
Copy link
Copy Markdown
Contributor Author

bfroehle commented Feb 3, 2012

Just pushed a small fix so that ipython -m <module> exits after running the module. Interactive mode can be forced with -i as always.

Comment thread IPython/frontend/terminal/ipapp.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module_to_run and code_to_run should probably be treated the same. Do you want to set a flag inside _file_to_run_changed to use here, rather than explicitly checking for self.module_to_run?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do you mean something like

    def _file_to_run_changed(self, name, old, new):
        something_to_run = True
        ...

    # internal, not-configurable
    interact = Bool(True)
    something_to_run=Bool(False)

   ....

    def initialize(self, argv=None):
        ...
        if self.extra_args and not self.something_to_run:
            self.file_to_run = self.extra_args[0]
        ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, precisely.

@minrk
Copy link
Copy Markdown
Member

minrk commented Feb 7, 2012

Nice, thanks! Looks good to me.

minrk added a commit that referenced this pull request Feb 8, 2012
Add '-m mod : run library module as a script' option.
@minrk minrk merged commit 39adc27 into ipython:master Feb 8, 2012
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Add '-m mod : run library module as a script' option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add '-m mod : run library module as a script' option

2 participants