Skip to content

Fix for when sys.argv is used in examples #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2017

Conversation

lesteve
Copy link
Member

@lesteve lesteve commented Jun 15, 2017

Fix #251.

The fix is not the nicest one (maybe changing sys.argv inside a try clause and changing it back in except would make it a bit better) but it works ...

@lesteve
Copy link
Member Author

lesteve commented Jul 20, 2017

ping @Titan-C @choldgraf.

CHANGES.rst Outdated
@@ -4,6 +4,7 @@ Change Log
git master
----------

<<<<<<< 5eb83a7c61a79656f24e61f5c90c631082265359
Copy link
Member

Choose a reason for hiding this comment

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

Left over from rebase?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed.

@@ -0,0 +1,14 @@
"""
Using ``sys.argv`` in examples
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be an RST heading

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed


argv_orig = sys.argv[:]
if block_vars['execute_script']:
print('Executing file %s' % src_file)
Copy link
Member

Choose a reason for hiding this comment

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

We currently use the Sphinx logger not the print statement. The execution of the file is stated in generate_dir_rst and not here anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed the print statement. I think that is what you were aiming at.

argv_orig = sys.argv[:]
if block_vars['execute_script']:
print('Executing file %s' % src_file)
sys.argv[0] = src_file
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? sys.argv[0] corresponds to the executing binary, most probably it would be python as example scripts are not executable on their own (there is no shebang). I wouldn't like to have this hint to the src_file, because people might start abusing it. We already don't provide the __file__ variable because of incompatibility with the Jupyter notebooks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Otherwise sys.argv[0] is sphinx-build leading to a very confusing error message like the one shown in #251 (comment).

Copy link
Member

@Titan-C Titan-C left a comment

Choose a reason for hiding this comment

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

There are some issues pending. Maybe the try block is not necessary, changing the sys.argv value can be enough.

@Titan-C Titan-C merged commit e91ce43 into sphinx-gallery:master Jul 20, 2017
@Titan-C
Copy link
Member

Titan-C commented Jul 20, 2017

Thanks, merging

@lucyleeow
Copy link
Contributor

lucyleeow commented Nov 12, 2019

@lesteve could you explain this a bit more? My understanding is that you cannot use any sys args in your examples .py files as they are set to []. But this doesn't correspond to the example in the documentation where it seems a sys arg is used?

@lesteve lesteve deleted the fix-sys-argv branch November 13, 2019 07:54
@lesteve
Copy link
Member Author

lesteve commented Nov 13, 2019

My understanding is that you cannot use any sys args in your examples .py files as they are set to [].

You are right, basically what happens is the equivalent of python your-example.py (i.e. no arguments are provided to the example script).

But this doesn't correspond to the example in the documentation where it seems a sys arg is used?

It is consistent with this example, parsed args: Namespace(option='default') means this is the default value of the parameters so the same thing as what you get when doing python your-example.py. In other words:

❯ python examples/plot_sys_argv.py
sys.argv: ['examples/plot_sys_argv.py']
parsed args: Namespace(option='default')

@lucyleeow
Copy link
Contributor

Ah I see, thank you for explaining. You mentioned:

FYI it happens in scikit-learn where some examples are using sys.argv.

And I noticed that there are scikit-learn examples that do use sys.argv (e.g. here). Is this because they are using different configurations or...?

@lesteve
Copy link
Member Author

lesteve commented Nov 13, 2019

Is this because they are using different configurations or...?

I think that the example you mention is not run by sphinx-gallery. AFAIK scikit-learn sphinx-gallery config only use the examples folder and your file insomewhere in doc/tutorial.

@lucyleeow
Copy link
Contributor

Ah okay - that makes sense - thanks for clarifying.

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.

Problem with examples using sys.argv e.g. through argparse.ArgumentParser
3 participants