jupyter / nbconvert

Jupyter Notebook Conversion
https://nbconvert.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.69k stars 558 forks source link

nbconvert fails to convert to pdf/latex #326

Open bpajusco opened 8 years ago

bpajusco commented 8 years ago

I'm trying to save a notebook as pdf via latex and I'm failing to do so either via command line 'jupyter nbconvert --to pdf ' or via the notebook's menu buttons. The error message tracebook is reported below but it complains that it can't create a .tex file

Traceback (most recent call last): File "/home/bruno/anaconda3/bin/jupyter-nbconvert", line 6, in main() File "/home/bruno/anaconda3/lib/python3.5/site-packages/jupyter_core/application.py", line 267, in launch_instance return super(JupyterApp, cls).launch_instance(argv=argv, _kwargs) File "/home/bruno/anaconda3/lib/python3.5/site-packages/traitlets/config/application.py", line 596, in launch_instance app.start() File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 293, in start self.convert_notebooks() File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 457, in convert_notebooks self.convert_single_notebook(notebook_filename) File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 428, in convert_single_notebook output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer) File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 357, in export_single_notebook output, resources = self.exporter.from_filename(notebook_filename, resources=resources) File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/exporters/exporter.py", line 165, in from_filename return self.from_file(f, resources=resources, _kw) File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/exporters/exporter.py", line 183, in from_file return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw) File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/exporters/pdf.py", line 165, in from_notebook_node tex_file = self.writer.write(latex, resources, notebook_name=notebook_name) File "/home/bruno/anaconda3/lib/python3.5/site-packages/nbconvert/writers/files.py", line 127, in write with io.open(dest, 'w', encoding='utf-8') as f: FileNotFoundError: [Errno 2] No such file or directory: 'Documents/Analysis/notebook.tex'

I'm struggling to understand this as I've installed both latex and pandoc and that I'm able to save a latex file and then compiling into a pdf via pdflatex.

takluyver commented 8 years ago

Do you have any config for nbconvert or the notebook? What exact command line are you using? Does nbconvert at the command line work with --to latex?

bpajusco commented 8 years ago

the command I run is --to pdf as I want a pdf but it fails on both (--to pdf and --to latex). I don't have any custom config for nbconvert or the notebook but use anaconda as my package manager.

On 19/06/16 18:00, Thomas Kluyver wrote:

Do you have any config for nbconvert or the notebook? What exact command line are you using? Does nbconvert at the command line work with |--to latex|?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/nbconvert/issues/326#issuecomment-227008059, or mute the thread https://github.com/notifications/unsubscribe/AGB5YfrtZNBulCxkwysOSq4dp3IIB6SXks5qNXWjgaJpZM4I5IyM.

takluyver commented 8 years ago

the command I run is --to pdf

I mean, what is the full command line you entered, including the path to the notebook?

Does the directory Documents/Analysis exist relative to the cwd where you ran it?

bpajusco commented 8 years ago

The command I run is jupyter nbconvert --to pdf Documents/Analysis/Test_Exercise.ipynb where Documents/... is the location of the ipython notebook I want to convert to pdf. As you can see from the traceback I copied in my original message, it complains that it can't create notebook.tex file (which I presume is what it needs to convert the notebook to a latex file and then save that as pdf). I can however create .tex files and also use pdflatex outside jupyter so I'm confused as to why it thinks I don't have the relevant tools in place.

On 19/06/16 18:22, Thomas Kluyver wrote:

the command I run is --to pdf

I mean, what is the full command line you entered, including the path to the notebook?

Does the directory |Documents/Analysis| exist relative to the cwd where you ran it?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/nbconvert/issues/326#issuecomment-227009203, or mute the thread https://github.com/notifications/unsubscribe/AGB5Ya93f_Zf4HcnpK_vhcReaiMv2ZYYks5qNXrmgaJpZM4I5IyM.

takluyver commented 8 years ago

Usually when you get a FileNotFoundError while trying to write a file, it means that the directory that would contain it doesn't exist. That doesn't appear to be the case here, so I don't understand what's going wrong.

bpajusco commented 8 years ago

I appreciate that. Any suggestions on how to fix the problem? I've tried my best but couldn't fix it but then again, I'm not an expert. What would you do in my place?

On 19/06/16 21:40, Thomas Kluyver wrote:

Usually when you get a |FileNotFoundError| while trying to write a file, it means that the directory that would contain it doesn't exist. That doesn't appear to be the case here, so I don't understand what's going wrong.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/nbconvert/issues/326#issuecomment-227019123, or mute the thread https://github.com/notifications/unsubscribe/AGB5YRGARcgt9SNboNIENhXuvEMzP8bjks5qNalKgaJpZM4I5IyM.

takluyver commented 8 years ago

Maybe try to cd into the directory where the notebook is and run nbconvert from there? I don't know why it would make a difference, but maybe somehow it would.

bpajusco commented 8 years ago

Thanks, that did the trick!!! Incidentally, why would the conversion work if I'm inside the file directory but not when I'm somewhere else in the file system but I specify the correct path?

On 20/06/16 09:13, Thomas Kluyver wrote:

Maybe try to |cd| into the directory where the notebook is and run nbconvert from there? I don't know why it would make a difference, but maybe somehow it would.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/nbconvert/issues/326#issuecomment-227077017, or mute the thread https://github.com/notifications/unsubscribe/AGB5YUzT_5msPKkAAezytDsCOBXWpp_Xks5qNku1gaJpZM4I5IyM.

takluyver commented 8 years ago

Not really, it doesn't make a lot of sense to me. The only thing I can think of is that if you had deleted the working directory, leaving the terminal open, and recreated a directory with the same name, you might get some weird effects. But from the names of your directories, it seems unlikely that you did that.

mpacer commented 7 years ago

If you run off of the current master does this work?

Also is it noteworthy that it couldn't find notebook.tex and your notebook was named TestExercise.ipynb which means that it should have tried to write to TestExercise.tex not notebook.tex. Did you also pass in another flag e.g., --output notebook?

yangvz commented 7 years ago

I have a similar problem, except that in my case --to latex works while --pdf does not. I do have pdflatex. Here's the code:

bogon:bayesian-hackers Yang$ find Prologue Chapter* -name "*.ipynb" | grep -v "PyMC2" | xargs jupyter nbconvert --to pdf --template article
[NbConvertApp] Converting notebook Prologue/Prologue.ipynb to pdf
[NbConvertApp] Writing 28617 bytes to Prologue/notebook.tex
Traceback (most recent call last):
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/bin/jupyter-nbconvert", line 6, in <module>
    main()
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/jupyter_core/application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 293, in start
    self.convert_notebooks()
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 457, in convert_notebooks
    self.convert_single_notebook(notebook_filename)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 428, in convert_single_notebook
    output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/nbconvertapp.py", line 357, in export_single_notebook
    output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/exporters/exporter.py", line 165, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/exporters/exporter.py", line 183, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/exporters/pdf.py", line 165, in from_notebook_node
    tex_file = self.writer.write(latex, resources, notebook_name=notebook_name)
  File "/Users/Yang/.pyenv/versions/miniconda3-4.1.11/lib/python3.5/site-packages/nbconvert/writers/files.py", line 127, in write
    with io.open(dest, 'w', encoding='utf-8') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'Prologue/notebook.tex'
bogon:bayesian-hackers Yang$
mpacer commented 7 years ago

@yangnw If you specify an --output-dir flag value do you still have this problem?

Do you have this problem if you directly pass in your file name rather than piping and using xargs? it doesn't look like the problem is related to pdflatex, as it's not even getting to that step. Right now, it's just failing to find the file that it was attempting to write to.

Edit: Also, when you include an error log, it would be helpful if you use code blocks:

```log```

and not simple inline code markup:

`log` 

so that white space is not normalised to be spaces.

takluyver commented 7 years ago

The white space thing was a result of using single backticks in the markdown instead of triple backticks (for a code block). I've edited the comment and fixed it.

yangvz commented 7 years ago

Thanks for correcting the code block thing.

As of last comment, running without xargs did not work either. But by then I had site packages installed globally and then I reinstalled the entire stack under virtualenv, and now the problem is solved. It may be that some random module related to io is referring to the wrong version of python, etc. I can't reproduce the problem already though.

dunnl commented 7 years ago

I am having this same problem. Calling jupyter nbconvert --to pdf somedirectory/MyNotebook.ipynb fails with FileNotFoundError: [Errno 2] No such file or directory: 'somedirectory/notebook.tex. However cd somedirectory followed by jupyter nbconvert --to pdf MyNotebook.ipynb works.

Nbconvert generates PDF files in two stages -- first compiling to LaTeX and then calling pdflatex on the resulting file. The LaTeX file is created as "notebook.tex" in a temporary directory (see here). The issue appears to be creating the temporary file when nbconvert is called with a filename which includes a filepath.

I am not a really a Python programmer but this sounds like an easy fix. Any one else care to look at this?

dunnl commented 7 years ago

I've looked into this a bit more. As the error message indicates, when nbconvert tries to write the temporary file notebook.tex, it prepends the relative directory (as in somedirectory/notebook.tex). The call to io.open here fails because that directory does not exist inside the temporary directory. I think the most elegant solution would be force it to use simply a temporary file notebook.tex instead of using a temporary directory if possible.

iamsortiz commented 7 years ago

@yangnw If you specify an --output-dir flag value do you still have this problem?

@mpacer Thanks for the hint about --output-dir

In my case going from

jupyter nbconvert --execute --to pdf XXX/YYY.ipynb --output $PWD/ZZZ/YYY.pdf

to

jupyter nbconvert --execute --to pdf XXX/YYY.ipynb --output-dir ZZZ

solved the problem.