我需要帮助才能将在 vscode 上运行的 jupyter 笔记本中的 python 代码打印/编译为 pdf

问题描述 投票:0回答:1

我遇到了一个又一个的错误,并且已经达到了我可以尝试解决这个问题的不同路径的极限。我用的是 Mac。

我尝试安装markupsafe 2.0.1,我安装了mactex,我更新了python,这是我收到的最新错误代码:

17:07:44.011 [info] Handle Execution of Cells 0 for Untitled-1.ipynb
17:07:44.019 [info] Kernel acknowledged execution of cell 0 @ 1692824864014
17:07:44.022 [info] End cell 0 execution @ 1692824864019, started @ 1692824864014, elapsed time = 0.005s
17:07:59.077 [warn] Failed to get activated env vars for /usr/local/bin/python3 in 13ms
17:07:59.080 [info] Process Execution: /usr/local/bin/python3 -m jupyter nbconvert /var/folders/4j/rxmwt5nj01g532n5ztgy_1j40000gn/T/0976b6df-319c-4d3f-ba63-87d72de123d7/Untitled-1.ipynb --to pdf --output tmp-15429pgHoZrRAJUEG.pdf --output-dir /var/folders/4j/rxmwt5nj01g532n5ztgy_1j40000gn/T --debug
17:07:59.416 [error] Export failed [Error: Traceback (most recent call last):
  File "~/opt/anaconda3/bin/jupyter-nbconvert", line 7, in <module>
    from nbconvert.nbconvertapp import main
  File "~/opt/anaconda3/lib/python3.9/site-packages/nbconvert/__init__.py", line 4, in <module>
    from .exporters import *
  File "~/opt/anaconda3/lib/python3.9/site-packages/nbconvert/exporters/__init__.py", line 3, in <module>
    from .html import HTMLExporter
  File "~/opt/anaconda3/lib/python3.9/site-packages/nbconvert/exporters/html.py", line 16, in <module>
    import jinja2
  File "~/opt/anaconda3/lib/python3.9/site-packages/jinja2/__init__.py", line 12, in <module>
    from .environment import Environment
  File "~/opt/anaconda3/lib/python3.9/site-packages/jinja2/environment.py", line 25, in <module>
    from .defaults import BLOCK_END_STRING
  File "~/opt/anaconda3/lib/python3.9/site-packages/jinja2/defaults.py", line 3, in <module>
    from .filters import FILTERS as DEFAULT_FILTERS  # noqa: F401
  File "~/opt/anaconda3/lib/python3.9/site-packages/jinja2/filters.py", line 13, in <module>
    from markupsafe import soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (~/opt/anaconda3/lib/python3.9/site-packages/markupsafe/__init__.py)

    at TS.executeCommand (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:343034)
    at async US.export (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:346899)
    at async zS.exportToFormat (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:351370)
    at async zS.performNbConvertExport (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:351198)
    at async zS.performExport (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:350873)
    at async zS.exportImpl (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:350501)
    at async zS.export (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:350257)
    at async rk.export (~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-x64/out/extension.node.js:24:490878)
    at async a.h (/private/var/folders/4j/rxmwt5nj01g532n5ztgy_1j40000gn/T/AppTranslocation/A9D29D6E-7367-41D7-AE99-64A59DD0B6D5/d/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:102:77769)]
17:07:59.416 [error] If you have not installed xelatex (TeX) you will need to do so before you can export to PDF, for further instructions please look https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex. 
To avoid installing xelatex (TeX) you might want to try exporting to HTML and using your browsers "Print to PDF" feature.
python macos visual-studio-code jupyter pdf-generation
1个回答
0
投票

简单有效的方法是将你的jupyter导出为

.html
文件,然后用浏览器打开并打印为pdf。

如果想直接使用vscode导出为pdf,需要安装Tex。更多信息可以参考这个答案

© www.soinside.com 2019 - 2024. All rights reserved.