Skip to content
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

pytest example fails on Windows #475

Closed
asteppke opened this issue Mar 30, 2020 · 3 comments · Fixed by #491
Closed

pytest example fails on Windows #475

asteppke opened this issue Mar 30, 2020 · 3 comments · Fixed by #491
Milestone

Comments

@asteppke
Copy link

With the inclusion of the pytest functionality jupytext becomes even more valuable, that is highly appreciated.

The current example fails on Windows though:

jupytext --check 'pytest {}' notebook.ipynb

leads to

Traceback (most recent call last):
  File "C:\Users\alexander\Anaconda3\Scripts\jupytext-script.py", line 9, in <module>
    sys.exit(jupytext())
  File "C:\Users\alexander\Anaconda3\lib\site-packages\jupytext\cli.py", line 292, in jupytext
    exit_code += jupytext_single_file(nb_file, args, log)
  File "C:\Users\alexander\Anaconda3\lib\site-packages\jupytext\cli.py", line 325, in jupytext_single_file
    notebook = read(nb_file, fmt=fmt)
  File "C:\Users\alexander\Anaconda3\lib\site-packages\jupytext\jupytext.py", line 290, in read
    with io.open(fp, encoding='utf-8') as stream:
FileNotFoundError: [Errno 2] No such file or directory: "{}'"

This is probably caused by the difference in behavior when treating quotes by cmd.exe in comparison with other shells or operating systems. Changing the quotes works around this problem and

jupytext --check "pytest {}" notebook.ipynb

runs without issues in both cmd.exe and powershell.

@mwouts
Copy link
Owner

mwouts commented Mar 31, 2020

Hello @asteppke ,

The current example fails on Windows though:

Yes, that is correct... I made the choice to use single quotes as this seems to be more standard in unix-like terminals, but you're right, they don't work on Windows. Maybe we could add a comment saying that single quotes should be replaced with double quotes on Windows - would it have help? Or do you think double quotes are common enough and that we can use them in unix terminals as well?

With the inclusion of the pytest functionality jupytext becomes even more valuable, that is highly appreciated.

Thank you! By the way, I'd be curious to read more, if you wanted to let us know how you're using pytest in the context of a notebook... Does it mean that you can only have functions, rather than code blocks, in your notebook? Or, are you using the active-ipynb tag to run pytest only on selected parts of the notebook?

@asteppke
Copy link
Author

asteppke commented Apr 7, 2020

Both options, either switching to double quotes completely or a short note that this is required on Windows would be fine.
Double quotes should work on both Unix and Windows terminals, their meaning is slightly different though and Python uses the underlying OS convention (expansion in Unix environments vs. handling of the whole command string by the executable in Windows).

Thank you! By the way, I'd be curious to read more, if you wanted to let us know how you're using pytest in the context of a notebook... Does it mean that you can only have functions, rather than code blocks, in your notebook? Or, are you using the active-ipynb tag to run pytest only on selected parts of the notebook?

Usually my code is split up in module.py and test_module.py. With notebooks being more of a tool that allows for some literal programming it feels natural to have the tests in the notebook as you develop the functions. A very basic notebook then looks like this

--------
def f(x):
    return x**2
--------
x = 2
y = f(x)
--------
def test_f():
    assert f(-1) > 0 
--------

When running this through Jupytext with Pytest the tests run fine. For exporting this to a module I use the active-ipynb tags so that the second cell in this example is not exported. This is also necessary if the cells have (unwanted) side effects or exceptions because Pytest chokes if the second cell in the example looks like

--------
x = 2
y = f(x)

with open('not_existing_file', 'r') as f:
    pass
--------

@mwouts mwouts added this to the 1.5.0 milestone Apr 8, 2020
@mwouts
Copy link
Owner

mwouts commented Apr 11, 2020

Both options, either switching to double quotes completely or a short note that this is required on Windows would be fine.

Thank you - I will take the second option.

Thanks for letting us know also how you use pytest in the context of a notebook! Actually I do have the same experience, it is really required to comment out some of the cells with the active-ipynb tag...

In the end, I found out that I was not really using pytest a lot on notebooks, but I had some fun with adding a few commented cells (with the active-ipynb tag) to existing test modules - when we open the test_module.py as a notebook in Jupyter, these cells show how to interactively test or debug the associated functions.

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 a pull request may close this issue.

2 participants