Al_*_*l_P 4 python virtualenv ipython pipenv python-poetry
我使用 IPython 和%bookmarkmagic 命令,但限制是我需要退出 IPython shell 才能激活虚拟环境,然后重新启动 IPython。
有没有办法内置到 IPython、第三方包或已知的 hack 中,让我在不退出 IPython shell 的情况下激活环境?
# within ipython shell
exit
# bash shell
cd path/of/my/project
poetry shell # or pipenv shell
ipython
Run Code Online (Sandbox Code Playgroud)
%cd -b demographics
# activate venv
import package # from that env
Run Code Online (Sandbox Code Playgroud)
这是一个很好的问题,但不幸的是,答案可能不是你所希望的。如果我正确理解你的问题 - 这是不可能的。因为虚拟 shell 激活本质上是运行子进程,为另一个 Python 解释器设置 PATH,然后为此设置 PYTHONPATH 环境变量。在任何操作系统中,子进程都不能修改其父进程。例如,您无法更改已运行的 Python 解释器,因为替换内存中正在运行的进程非常复杂,而且在实践中从未这样做过。
如果这是一个工作流程问题,并且您肯定想在 IPython 中执行此操作,我建议您编写一个单行复制粘贴命令,该命令将执行以下操作:
(source venv/bin/activate && ipython)对于他的工作流程问题可能有更好的解决方案。也许有一个类似于 的工作流程%%bookmark,但是针对整个 Python 环境和流程,例如创建 shell 脚本。
| 归档时间: |
|
| 查看次数: |
1490 次 |
| 最近记录: |