0

So I am trying to switch from using Jupyter notebook in the browser to using IPython shell in terminal as I prefer it. I am on Ubuntu 22.0.4 OS and when I activate IPython, I can write code and a script but cannot open one or save one.

When I try to run a script that I have created using a Jupyter notebook by using line magic %run I get the following errors:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[6], line 1
----> 1 get_ipython().run_line_magic('run', 'script1.ipynb')

File ~/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2369, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2367     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2368 with self.builtin_trap:
-> 2369     result = fn(*args, **kwargs)
   2370 return result

File ~/.local/lib/python3.10/site-packages/IPython/core/magics/execution.py:717, in ExecutionMagics.run(self, parameter_s, runner, file_finder)
    715     with preserve_keys(self.shell.user_ns, '__file__'):
    716         self.shell.user_ns['__file__'] = filename
--> 717         self.shell.safe_execfile_ipy(filename, raise_exceptions=True)
    718     return
    720 # Control the response to exit() calls made by the script being run

File ~/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2872, in InteractiveShell.safe_execfile_ipy(self, fname, shell_futures, raise_exceptions)
   2870 with prepended_to_syspath(dname):
   2871     try:
-> 2872         for cell in get_cells():
   2873             result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
   2874             if raise_exceptions:

File ~/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2860, in InteractiveShell.safe_execfile_ipy.<locals>.get_cells()
   2858 """generator for sequence of code blocks to run"""
   2859 if fname.suffix == ".ipynb":
-> 2860     from nbformat import read
   2861     nb = read(fname, as_version=4)
   2862     if not nb.cells:

File ~/.local/lib/python3.10/site-packages/nbformat/__init__.py:11
      6 # Copyright (c) IPython Development Team.
      7 # Distributed under the terms of the Modified BSD License.
      9 from traitlets.log import get_logger
---> 11 from . import v1, v2, v3, v4
     12 from ._version import __version__, version_info
     13 from .sentinel import Sentinel

File ~/.local/lib/python3.10/site-packages/nbformat/v4/__init__.py:40
     37 writes_json = writes
     38 to_notebook_json = to_notebook
---> 40 from .convert import downgrade, upgrade

File ~/.local/lib/python3.10/site-packages/nbformat/v4/convert.py:13
      9 from traitlets.log import get_logger
     11 from nbformat import v3
---> 13 from .. import validator
     14 from .nbbase import NotebookNode, nbformat, nbformat_minor, random_cell_id
     17 def _warn_if_invalid(nb, version):

File ~/.local/lib/python3.10/site-packages/nbformat/validator.py:16
     14 from ._imports import import_item
     15 from .corpus.words import generate_corpus_id
---> 16 from .json_compat import ValidationError, _validator_for_name, get_current_validator
     17 from .reader import get_version
     18 from .warnings import DuplicateCellId, MissingIDFieldWarning

File ~/.local/lib/python3.10/site-packages/nbformat/json_compat.py:11
      8 import os
     10 import fastjsonschema
---> 11 import jsonschema
     12 from fastjsonschema import JsonSchemaException as _JsonSchemaException
     13 from jsonschema import Draft4Validator as _JsonSchemaValidator

File ~/.local/lib/python3.10/site-packages/jsonschema/__init__.py:13
      1 """
      2 An implementation of JSON Schema for Python
      3 
   (...)
      9 for you.
     10 """
     11 import warnings
---> 13 from jsonschema._format import FormatChecker
     14 from jsonschema._types import TypeChecker
     15 from jsonschema.exceptions import (
     16     ErrorTree,
     17     FormatError,
   (...)
     20     ValidationError,
     21 )

File ~/.local/lib/python3.10/site-packages/jsonschema/_format.py:11
      8 import typing
      9 import warnings
---> 11 from jsonschema.exceptions import FormatError
     13 _FormatCheckCallable = typing.Callable[[object], bool]
     14 _F = typing.TypeVar("_F", bound=_FormatCheckCallable)

File ~/.local/lib/python3.10/site-packages/jsonschema/exceptions.py:12
      9 import heapq
     10 import itertools
---> 12 import attr
     14 from jsonschema import _utils
     16 WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])

ModuleNotFoundError: No module named 'attr'

I have no issue running the script in a Jupyter notebook. Any help would be appreciated.

  • 1
    Does this answer your question? [ImportError: No module named attr](https://stackoverflow.com/questions/52729841/importerror-no-module-named-attr) – Pranav Hosangadi Mar 03 '23 at 21:35
  • I looked at that thread and used pip install attr. I ended up getting rid of the last error where it said "No module named 'attr'" but I still have the other errors and I got a new error saying "module 'attr' has no attribute 's'". – Jackanap3s Mar 03 '23 at 21:44
  • Why would you do that? None of the answers tell you to install `attr` -- they tell you to _un_ install `attr` and install/upgrade `attrs`. Also, the link in the top answer to that question takes you to a question that asks about your new error message – Pranav Hosangadi Mar 03 '23 at 21:46
  • `run` is normally used with a `.py` file, a plain python script. I don't know if it's supposed to work with a `ipynb`. I believe there's a jupyter utility that can convert the notebook to script. (I use `ipython` alot, but only dabble in notebooks). – hpaulj Mar 03 '23 at 21:48
  • @PranavHosangadi Yes you're right. I have now done pip install attrs and it works. – Jackanap3s Mar 03 '23 at 21:53
  • How would I open a file and edit a file? I know I can run, save with %run and %save. And is it possible to make a ipynb file using IPython? Whenever I open one it comes in a Json format. – Jackanap3s Mar 03 '23 at 21:55
  • Normally `nbconvert` is run from the shell: `jupyter nbconvert --to python notebook.ipynb`. You can of course run shell commands from within `ipython. As for the more general question of editing a file, `ipython` has an `%edit` magic. A default editor for that is `nano`. Or just use what ever editor you already know and love. – hpaulj Mar 03 '23 at 22:01
  • Thanks that makes sense now. I do not have that command installed I think but I'll look into it. – Jackanap3s Mar 03 '23 at 22:07

0 Answers0