从 virtualenv 调用 IPython
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20327621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling IPython from a virtualenv
提问by Mo Sander
I understand that IPython is not virtualenv-awareand that the most logical solution to this is to install ipython in each virtualenv seperately using
我知道 IPython 不支持virtualenv,对此最合乎逻辑的解决方案是在每个 virtualenv 中分别使用以下命令安装 ipython
pip install ipython
So far so good. One thing I noticed is that if the system-wide copy of IPython is called from within a virtualenv using $> ipythonbefore IPython is installed under this virtualenv, subsequent $> ipythoncommands will continue to bring up the system-wide ipython copy.
到现在为止还挺好。我注意到的一件事是,如果在将 IPython$> ipython安装在该 virtualenv 下之前,从 virtualenv 中调用 IPython 的系统范围副本,则后续$> ipython命令将继续启动系统范围的 ipython 副本。
On the other hand, if ipython is notcalled prior to installing it under a virtualenv $> ipythonwill bring up the newly installed copy.
另一方面,如果在 virtualenv 下安装ipython之前没有调用它,$> ipython则会调出新安装的副本。
What is the explanation for this?
对此有何解释?
It also makes me wonder if this behavior means I should expect some trouble down the way?
这也让我想知道这种行为是否意味着我应该期待一些麻烦?
采纳答案by SiddharthaRT
alias ipy="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"
alias ipy="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"
This is a great way of always being sure that the ipython instance always belongs to the virtualenv's python version.
这是始终确保 ipython 实例始终属于 virtualenv 的 python 版本的好方法。
This works only on ipython >2.0.
这仅适用于 ipython > 2.0。
回答by rgtk
You can force IPython to use a virtual environment if available by adding file below to ~/.ipython/profile_default/startups:
您可以通过将以下文件添加到以下文件来强制 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)
I recommend naming it 00-virtualenv.pyso changes will be made as early as possible.
我建议命名它,00-virtualenv.py以便尽早进行更改。
Note: Make sure ipython is installed in the new virtual environment to get this to work.
注意:确保 ipython 安装在新的虚拟环境中以使其正常工作。
回答by JDiMatteo
As others mentioned, recent versions of ipython are virtualenv aware, so you can use your virtualenv bin activatescript to run ipython using your virtualenv, e.g.
正如其他人所提到的,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.
回答by Fabio Scaccabarozzi
If you're trying to open a notebook, even ipython 5 won't help - ipython will disregard the virtualenv (at least on my machine/setup). You'll need to use rgtk's script, but please make sure to modify the optional filter part and the sys.path.insert as below:
如果您尝试打开笔记本,即使 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)
- ADD1: in the original script we get back a filter object, we would break sys.path and insert below would fail
- ADD2: see this questionand python documentation
回答by mjkrause
(Debian/Ubuntu) assuming some version (x) of Python3 is installed, then:
(Debian/Ubuntu) 假设安装了 Python3 的某个版本 (x),那么:
$ sudo apt-get install -y ipython
$ virtualenv --python=python3.x .venv
$ source .venv/bin/activate
$ pip3 install ipython
$ ipython3
will launch ipython running your version of Python3.
将启动运行您的 Python3 版本的 ipython。
回答by jcozar87
The answer given by @SiddharthaRT is good! Following this approach, it is simpler for me just:
@SiddharthaRT 给出的答案很好!按照这种方法,对我来说更简单:
python -m IPython
This will use the module IPython through the python bin, ensuring that it refers to the bin from the virtual env.
这将通过 python bin 使用模块 IPython,确保它引用虚拟环境中的 bin。
回答by TheDataGuy
Activate your virtual environment by using source ~/.virtualenvs/my_venv/bin/activateor by running workon my_venv(Depending on how you've installed the my_venv virtual environment)
Install ipython
使用source ~/.virtualenvs/my_venv/bin/activate或通过在my_venv上运行work来激活您的虚拟环境(取决于您如何安装 my_venv 虚拟环境)
安装 ipython
pip install ipython
pip 安装 ipython
- Now run ipython from my_venv.
- 现在从 my_venv 运行 ipython。
If it still loads the system's ipython,then run
如果它仍然加载系统的 ipython,则运行
hash -r
哈希 -r
回答by Skinner927
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
pyenvwhere your global Python version might be3.7and your local virtualenv Python is3.6therefore using the globalipythonwill fail. - Works outside of virtual environments (though not particularly useful as it always targets
python).
- 您不需要在当前 virtualenv 中安装 iPython,只需要与您的 virtualenv 的 Python 版本 (
3.6 != 3.7)匹配的全局 Python 。 - 适用于
pyenv您的全局 Python 版本可能所在的用户,因此3.7您的本地 virtualenv Python3.6使用全局ipython将失败。 - 在虚拟环境之外工作(虽然不是特别有用,因为它总是针对
python)。
Throw this in your ~/.bashrcor ~/.zshrcor what have you:
在抛出这样~/.bashrc或~/.zshrc或你有什么:
# 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 sourceor open a new terminal and run ipy.
然后source或打开一个新终端并运行ipy.

