Skip to main content

ipython 选择正确的python版本

环境了使用pyenv安装了多个版本的python, 使用pip intall ipython后, 启动的ipython版本总是不对, 导致已经安装的pip依赖无法使用.

两个解决方法, 一个是使用python -m IPython启动ipython, 另一个是修改ipython的启动脚本, 设置启动的python位置.

  • 环境里安装多个python
 pyenv versions
system
* 3.11.4 (set by /home/ubuntu/.pyenv/version)
  • 默认启动的iypthon, 总是使用systme版本 3.10.6
ipython
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.
  • 直接使用python -m IPython启动, 可以使用当前生效的python版本
ubuntu@ocx:/data/projects/ChatGLM2-6B$ python -m IPython
Python 3.11.4 (main, Jul 22 2023, 12:30:53) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.

  • 查看python和ipython的启动命令, 然后可以进行修改
ubuntu@ocx:/data/projects/ChatGLM2-6B$ which python
/home/ubuntu/.pyenv/shims/python
ubuntu@ocx:/data/projects/ChatGLM2-6B$ which ipython
/home/ubuntu/.local/bin/ipython
  • ipython的启动命令, hash bang指定里启动的python为#!/usr/bin/python3
ubuntu@ocx:/data/projects/ChatGLM2-6B$ cat /home/ubuntu/.local/bin/ipython
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from IPython import start_ipython
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())

  • 修改ipython启动脚本的hash bang为#!/home/ubuntu/.pyenv/shims/python, ipython顺利正常启动

ubuntu@ocx:/data/projects/ChatGLM2-6B$ cat /home/ubuntu/.local/bin/ipython
#!/home/ubuntu/.pyenv/shims/python
# -*- coding: utf-8 -*-
import re
import sys
from IPython import start_ipython
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())
ubuntu@ocx:/data/projects/ChatGLM2-6B$
ubuntu@ocx:/data/projects/ChatGLM2-6B$ ipython
Python 3.11.4 (main, Jul 22 2023, 12:30:53) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.

  • 参考

ipython reads wrong python version

https://stackoverflow.com/questions/9386048/ipython-reads-wrong-python-version

created at 2023-07-22