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

Conversion of a simple .ipynb to py then back to ipynb gives "KeyError: 'env'" #792

Closed
liar666 opened this issue May 29, 2021 · 3 comments
Closed
Milestone

Comments

@liar666
Copy link

liar666 commented May 29, 2021

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a=5\n",
    "print('hello',a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "argv": [
    "python",
    "-m",
    "ipykernel_launcher",
    "-f",
    "{connection_file}"
   ],
   "display_name": "Python 3",
   "env": null,
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": null,
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "name": "utils1.ipynb"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

First convertsion work:

$ jupytext --to py:percent --from ipynb utils1.ipynb
[jupytext] Reading utils1.ipynb in format ipynb
[jupytext] Updating the timestamp of utils1.py

Conversion back to original does not

$ jupytext --from py:percent --to ipynb utils1.py
[jupytext] Reading utils1.py in format py:percent
Traceback (most recent call last):
  File "/home/gmuller/Python/DataScience/bin/jupytext", line 8, in <module>
    sys.exit(jupytext())
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/cli.py", line 427, in jupytext
    exit_code += jupytext_single_file(nb_file, args, log)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/cli.py", line 494, in jupytext_single_file
    notebook = read(nb_file, fmt=fmt, config=config)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/jupytext.py", line 435, in read
    return read(stream, as_version=as_version, fmt=fmt, config=config, **kwargs)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/jupytext.py", line 444, in read
    return reads(fp.read(), fmt, config=config, **kwargs)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/jupytext.py", line 384, in reads
    format_name = read_format_from_metadata(text, ext) or fmt.get("format_name")
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/formats.py", line 280, in read_format_from_metadata
    metadata = read_metadata(text, ext)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/formats.py", line 259, in read_metadata
    metadata, _, _, _ = header_to_metadata_and_cell(lines, comment, ext)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/header.py", line 228, in header_to_metadata_and_cell
    recursive_update(metadata, yaml.safe_load("\n".join(jupyter))["jupyter"])
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/header.py", line 148, in recursive_update
    target[key] = recursive_update(target.get(key, {}), value)
  File "/home/gmuller/Python/DataScience/lib/python3.8/site-packages/jupytext/header.py", line 146, in recursive_update
    del target[key]
KeyError: 'env'
@mwouts
Copy link
Owner

mwouts commented May 29, 2021

Thanks @liar666 for reporting this.

Do you have any idea where this "env": null and "metadata": null in your kernelspec come from?

Anyway I agree they should not make Jupytext fail, I'll fix that part.

@mwouts mwouts added this to the 1.11.3 milestone May 29, 2021
mwouts added a commit that referenced this issue May 29, 2021
mwouts added a commit that referenced this issue May 29, 2021
@mwouts mwouts closed this as completed in b759a77 May 30, 2021
@mwouts
Copy link
Owner

mwouts commented May 30, 2021

Hi @liar666 , this should be fixed in the dev version. See https://jupytext.readthedocs.io/en/latest/developing.html for how to install it locally. Thanks for reporting this!

@liar666
Copy link
Author

liar666 commented May 31, 2021

Do you have any idea where this "env": null and "metadata": null in your kernelspec come from?

I've tried to use EIN (Emacs Ipython Notebook package) on this source file, that might be the source of this problem :{
https://github.com/millejoh/emacs-ipython-notebook

Thanks for quick response and the patch I'll try it ASAP!!!
EDIT: Works perfectly on my notebooks! Thanks again !!!

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

No branches or pull requests

2 participants