bpythonをDocker上で使うときにターミナルをiTerm2にしていると時々出るエラーへの対処法

Sept. 15, 2018, 8:24 a.m. edited Sept. 15, 2018, 4:51 p.m.

#Docker  #Python  #Django 

$$ \def\bra#1{\mathinner{\left\langle{#1}\right|}} \def\ket#1{\mathinner{\left|{#1}\right\rangle}} \def\braket#1#2{\mathinner{\left\langle{#1}\middle|#2\right\rangle}} $$

ニッチな話だなぁ...DjangoをDockerコンテナ上で動かしているが,その際,PythonシェルとしてIPythonなどを導入していると便利である1.もっとも私はbpythonを好んで使っているので,こちらを導入している2

しかし,いつからか

# ./manage.py shell

Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/shell.py", line 99, in handle
    return getattr(self, shell)(options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/shell.py", line 40, in bpython
    bpython.embed()
  File "/usr/local/lib/python3.5/dist-packages/bpython/__init__.py", line 36, in embed
    return main(args, locals_, banner)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsies.py", line 190, in main
    exit_value = repl.mainloop()
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsies.py", line 117, in mainloop
    self.process_event_and_paint(None)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsies.py", line 99, in process_event_and_paint
    array, cursor_pos = self.paint()
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/repl.py", line 1306, in paint
    self.current_cursor_line)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/replpainter.py", line 47, in paint_current_line
    lines = display_linize(current_display_line, columns, True)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/replpainter.py", line 32, in display_linize
    if msg else ([''] if blank_line else []))
ValueError: range() arg 3 must not be zero

というようなエラーが出るようになってしまった.そこで,bpythonは動くか確かめたところ

# bpython
bpython version 0.16 on top of Python 3.5.2 /usr/bin/python3

Traceback (most recent call last):
  File "/usr/local/bin/bpython", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsies.py", line 190, in main
    exit_value = repl.mainloop()
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsies.py", line 117, in mainloop
    self.process_event_and_paint(None)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsies.py", line 99, in process_event_and_paint
    array, cursor_pos = self.paint()
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/repl.py", line 1306, in paint
    self.current_cursor_line)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/replpainter.py", line 47, in paint_current_line
    lines = display_linize(current_display_line, columns, True)
  File "/usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/replpainter.py", line 32, in display_linize
    if msg else ([''] if blank_line else []))
ValueError: range() arg 3 must not be zero

だめですねぇ.どうやら原因は /usr/local/lib/python3.5/dist-packages/bpython/curtsiesfrontend/replpainter.py:30,31 にある columns が 0 になっていることなので,つまりターミナルの幅等をうまく取得できなくなっているようだ.

今使っているターミナルはiTerm2で,最大化している.

そこで,

command+Enter -> command+Enter (ウィンドウ化してからまた最大化)

# ./manage.py shell
>>>
Welcome to bpython! Press <F1> for help.

Works well:) う,うごいたーー


  1. pip install ipython とするだけで自動的に ./manage.py shell で使われるシェルが IPython となるはずである 

  2. pip install bpython