gpt4 book ai didi

python - 使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本

转载 作者:IT老高 更新时间:2023-10-28 20:31:20 31 4
gpt4 key购买 nike

我想在 Jupyter 笔记本中执行一个长时间运行的 Python 脚本,这样我就可以破解运行中生成的数据结构。

该脚本有许多依赖项和命令行参数,并使用特定的 virtualenv 执行。是否可以从指定的 virtualenv(与 Jupyter 安装不同)在笔记本内交互式运行 Python 脚本?

最佳答案

这对我有用(非 conda python):(MacOS,python 的 brew 版本。如果您正在使用系统 python,您可能(将)需要在每个命令前加上 sudo)

  1. 首先激活virtualenv。如果重新开始,例如,您可以使用 virtualenvwrapper:

    $ pip install virtualenvwrapper
    $ mkvirtualenv -p python2 py2env
    $ workon py2env

    # This will activate virtualenv

    (py2env)$

    # Then install jupyter within the active virtualenv
    (py2env)$ pip install jupyter

    # jupyter comes with ipykernel, but somehow you manage to get an error due to ipykernel, then for reference ipykernel package can be installed using:
    (py2env)$ pip install ipykernel
  2. 接下来,设置内核

    (py2env)$ python -m ipykernel install --user --name py2env --display-name "Python2 (py2env)"
  3. 然后启动jupyter notebook(这一步不需要激活venv)

    (py2env)$ jupyter notebook
    # or
    #$ jupyter notebook

在 jupyter notebook 下拉菜单中:Kernel >> Change Kernel >> <list of kernels>你应该看到 Python2 (py2env)内核。

这也使得识别内核的python版本变得容易,并且可以同时维护。

这里是详细文档的链接: http://ipython.readthedocs.io/en/stable/install/kernel_install.html

关于python - 使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496350/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com