关于在魔术单元中使用bash命令的CalledProcessError

时间:2019-05-11 14:47:55

标签: python jupyter-notebook

我在Jupiter笔记本中使用了下面的代码。

%%bash -s "$store_path" "$store_file"
! echo a = $1, b = $2
! cat $1 | awk '{ print length, $0 }' | sort -n -s | cut -d ' ' -f 2- | awk '!a[$0]++' > $2

以前,该代码可以正常工作,经过很长一段时间,我尝试使用此代码。它显示了以下问题,对此我一无所知。

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-30-1e2db86b01e6> in <module>
----> 1 get_ipython().run_cell_magic('bash', '-s "$store_path" "$store_file"', "! echo a = $1, b = $2\n! cat $1 | awk '{ print length, $0 }' | sort -n -s | cut -d ' ' -f 2- | awk '!a[$0]++' > $2\n")

~/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2350             with self.builtin_trap:
   2351                 args = (magic_arg_s, cell)
-> 2352                 result = fn(*args, **kwargs)
   2353             return result
   2354 

~/.local/lib/python3.6/site-packages/IPython/core/magics/script.py in named_script_magic(line, cell)
    140             else:
    141                 line = script
--> 142             return self.shebang(line, cell)
    143 
    144         # write a basic docstring:

</home/ambijat/.local/lib/python3.6/site-packages/decorator.py:decorator-gen-110> in shebang(self, line, cell)

~/.local/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/.local/lib/python3.6/site-packages/IPython/core/magics/script.py in shebang(self, line, cell)
    243             sys.stderr.flush()
    244         if args.raise_error and p.returncode!=0:
--> 245             raise CalledProcessError(p.returncode, cell, output=out, stderr=err)
    246 
    247     def _run_script(self, p, cell, to_close):

CalledProcessError: Command 'b"! echo a = $1, b = $2\n! cat $1 | awk '{ print length, $0 }' | sort -n -s | cut -d ' ' -f 2- | awk '!a[$0]++' > $2\n"' returned non-zero exit status 1.

我试图通过使用一行作为import subprocess来进行操作,但是没有成功。我尝试过的另一件事是使用%而不是%%,但这也不起作用。 需要帮助。

1 个答案:

答案 0 :(得分:0)

一旦'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class, 'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class, 位于顶部,则在连续的行中不需要%%。另外,从关于旧代码的内存中,也应避免使用!在魔术单元的顶部插入注释,因为它还会产生一些错误。