LoginSignup
1
0

Virtualenv上のipythonが起動できないので対処

Last updated at Posted at 2022-03-17

環境
OS - Ubuntu 20.04.4 LTS
virtualenv - 20.13.2
python3 - 3.8.10
ipython - 8.1.0
(2024/2/8追記)見ての通り古い情報です。ご注意ください。

ipythonが起動できなかった

ある日Virtualenv上のipythonを起動しようと思い、ipythonと打ったらこんな事に。(ipythonは何かのパッケージと一緒に導入された&初起動っぽかったです)

~~~~~ 中略 ~~~~~

AttributeError: 'PosixPath' object has no attribute 'readlink'

If you suspect this is an IPython 8.1.0 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True

エラー文で検索して出てきたGitHubのissueを見てみると、site-packagesのあたりが変になっている様子。元ページはややこしいうえに英語なので、本記事に日本語でまとめてみました。

解決法

まず、Virtualenvを導入したディレクトリに移動します。
そして起動し、site-packagesの絶対パスを出力。

cd なんたらかんたら
source venv/bin/activate  
python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'

site-packagesまでの相対パスを把握します。
自分の場合はvenv/lib/python3.8/site-packagesでした。

次のコマンドの最後に相対パス/IPython/core/interactiveshell.pyを追記して実行。

sed -i 's/p.readlink()/p._from_parts(os.readlink(p,))/g' 

自分の場合は以下のようになりました。

sed -i 's/p.readlink()/p._from_parts(os.readlink(p,))/g' venv/lib/python3.8/site-packages/IPython/core/interactiveshell.py

そして、ipython --versionと実行すると8.1.0が出力。
治りました!

参考 - https://github.com/ipython/ipython/issues/13554

そもそも(追記)

2022/3/18時点で、ipython-8.1.0以降は不安定っぽい?です。
from IPython.lib import passwdを実行すると、ImportError: cannot import name 'passwd' from 'IPython.lib'と出ました。

GitHub上のIPythonのリポジトリを見ると、passwdが入っている筈のIPython/lib/security.pyも無い様子。

最終的にipythonを入れ直しました。
pipdeptreeで確認したところバージョンが指定される依存関係も無かったので、ipython7以上8未満に。

pip3 uninstall -y ipython
pip3 install 'ipython>=7,<8'

ipython-7.32.0が入りました。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0