development

virtualenv에서 IPython 호출

big-blog 2020. 10. 24. 11:08
반응형

virtualenv에서 IPython 호출


나는 IPython이 virtualenv를 인식 하지 못하며 이에 대한 가장 논리적 인 해결책은 각 virtualenv에 ipython을 별도로 설치 하는 것임을 이해합니다.

pip install ipython

여태까지는 그런대로 잘됐다. 내가 알아 차린 한 가지는 IPython $> ipython이이 virtualenv 아래에 설치되기 전에 사용하는 virtualenv 내에서 IPython의 시스템 전체 복사본이 호출 되면 후속 $> ipython명령이 시스템 전체 ipython 복사본을 계속 불러옵니다.

반면에, ipython을 virtualenv 아래에 설치하기 전에 호출 하지 않으면$> ipython 새로 설치된 복사본을 가져옵니다.

이것에 대한 설명은 무엇입니까?

또한이 행동이 어떤 문제가 발생할 것으로 예상해야하는지 궁금합니다.


alias ipy="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"

이것은 ipython 인스턴스가 항상 virtualenv의 python 버전에 속하는지 항상 확인하는 좋은 방법입니다.

이것은 ipython 2.0 이상에서만 작동합니다.

출처


가능한 경우 아래 파일을 다음에 추가하여 IPython이 가상 환경을 사용하도록 할 수 있습니다 ~/.ipython/profile_default/startups.

import os
import sys

if 'VIRTUAL_ENV' in os.environ:
    py_version = sys.version_info[:2] # formatted as X.Y 
    py_infix = os.path.join('lib', ('python%d.%d' % py_version))
    virtual_site = os.path.join(os.environ.get('VIRTUAL_ENV'), py_infix, 'site-packages')
    dist_site = os.path.join('/usr', py_infix, 'dist-packages')

    # OPTIONAL: exclude debian-based system distributions sites
    sys.path = filter(lambda p: not p.startswith(dist_site), sys.path)

    # add virtualenv site
    sys.path.insert(0, virtual_site)

00-virtualenv.py가능한 한 빨리 변경되도록 이름을 지정하는 것이 좋습니다 .

참고 :이 작업을 수행하려면 ipython이 새 가상 환경에 설치되어 있는지 확인하십시오.


@SiddharthaRT의 대답은 좋습니다! 이 접근 방식을 따르면 나에게 더 간단합니다.

python -m IPython

이것은 파이썬 bin을 통해 모듈 IPython을 사용하여 가상 환경의 bin을 참조하는지 확인합니다.


다른 사람들이 언급했듯이 최신 버전의 ipython은 virtualenv를 인식하므로 virtualenv bin activate 스크립트를 사용하여 virtualenv를 사용하여 ipython을 실행할 수 있습니다.

$ source venv/bin/activate
(venv) $ ipython
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.

노트북을 열려고한다면 ipython 5도 도움이되지 않습니다. ipython은 (적어도 내 컴퓨터 / 설정에서) virtualenv를 무시합니다. rgtk의 스크립트를 사용해야하지만 선택적 필터 부분과 sys.path.insert를 아래와 같이 수정해야합니다.

import os
import sys

if 'VIRTUAL_ENV' in os.environ:
    py_version = sys.version_info[:2] # formatted as X.Y 
    py_infix = os.path.join('lib', ('python%d.%d' % py_version))
    virtual_site = os.path.join(os.environ.get('VIRTUAL_ENV'), py_infix, 'site-packages')
    dist_site = os.path.join('/usr', py_infix, 'dist-packages')

    # OPTIONAL: exclude debian-based system distributions sites
    # ADD1: sys.path must be a list
    sys.path = list(filter(lambda p: not p.startswith(dist_site), sys.path))

    # add virtualenv site
    # ADD2: insert(0 is wrong and breaks conformance of sys.path
    sys.path.insert(1, virtual_site)

(Debian / Ubuntu) Python3의 일부 버전 (x)이 설치되어 있다고 가정하면 다음을 수행합니다.

$ sudo apt-get install -y ipython
$ virtualenv --python=python3.x .venv
$ source .venv/bin/activate
$ pip3 install ipython
$ ipython3

Python3 버전을 실행하는 ipython을 시작합니다.


  1. 소스 ~ / .virtualenvs / my_venv / bin / activate를 사용 하거나 workon my_venv 를 실행 하여 가상 환경을 활성화합니다 (my_venv 가상 환경을 설치 한 방법에 따라 다름).

  2. ipython 설치

pip install ipython

  1. Now run ipython from my_venv.

If it still loads the system's ipython,then run

hash -r


I'll chime in years later in hopes someone finds this useful.

This solution solves a few problems:

  • You don't need iPython installed in the current virtualenv, only for the global Python that matches your virtualenv's Python version (3.6 != 3.7).
  • Works for users of pyenv where your global Python version might be 3.7 and your local virtualenv Python is 3.6 therefore using the global ipython will fail.
  • Works outside of virtual environments (though not particularly useful as it always targets python).

Throw this in your ~/.bashrc or ~/.zshrc or what have you:

# This is a roundabout way to start ipython from inside a virtualenv without it being installed
# in that virtualenv. The only caveot is that the "global" python must have ipython installed.
# What this function does that's different than simply calling the global ipython is it ensures to
# call the ipython that is installed for the same major.minor python version as in the virtualenv.
# This is most useful if you use pyenv for example as global python3 could be 3.7 and local
# virtualenv python3 is 3.6.
function ipy {
  local PY_BIN
  local IPYTHON
  local PYV
  # This quick way will work if ipython is in the virtualenv
  PY_BIN="$(python -c 'import sys; print(sys.executable)')"
  IPYTHON="$(dirname "$PY_BIN")/ipython"
  if [[ -x "$IPYTHON" ]]; then
    "$IPYTHON"
  else
    # Ask the current python what version it is
    PYV="$(python -c 'import sys; print(".".join(str(i) for i in sys.version_info[:2]))')"
    echo "Looking for iPython for Python $PYV"
    # In a new shell (where pyenv should load if equipped) try to find that version
    PY_BIN="$($SHELL -i -c "python$PYV -c 'import sys; print(sys.executable)'")"
    "$(dirname "$PY_BIN")/ipython"
  fi
}

Then source or open a new terminal and run ipy.

참고URL : https://stackoverflow.com/questions/20327621/calling-ipython-from-a-virtualenv

반응형