【问题标题】:Easy install python modules for Jupyter notebooks?为 Jupyter 笔记本轻松安装 python 模块?
【发布时间】:2021-02-05 18:55:26
【问题描述】:

最近我一直在学习一些关于 Python 的知识。 Jupyter notebook 在理论上似乎是个好主意,但实际上我在 Jupyter 环境中设置 python 模块时遇到了一些困难。

例如,我发现了这个整洁的geographical data article,我想继续。我下载了一个非常不错的github project 以跟随。

启动 Jupyter,一切正常,直到我运行第一个代码块:

import pandas as pd
import re
from datetime import datetime
from dateutil.parser import parse
import missingno as msno
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt
import json

这会立即产生错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-422e89229f53> in <module>
----> 1 import pandas as pd
      2 import re
      3 from datetime import datetime
      4 from dateutil.parser import parse
      5 import missingno as msno

ModuleNotFoundError: No module named 'pandas'

有很多模块,我想我可以进入命令行单独添加它们。这似乎效率不高。是否有其他“Jupyter”或“Conda”方法可以做到这一点?

【问题讨论】:

  • 理想情况下,项目将提供 requirements.txt 文件。但在这种情况下,我会使用conda install 来获取所需的库
  • 另外,您可以将任意数量的库传递给单个conda install 命令
  • @PaulH 我为后续跟进提出了另一个问题。 stackoverflow.com/questions/64489517/…

标签: python jupyter-notebook


【解决方案1】:

只需在 Jupyter 中运行:

!pip install <the name of the module>, <the name of the module>, <the name of the module>...

这将安装您选择的模块。 例如:

!pip install pandas, re, ...

【讨论】:

    【解决方案2】:

    在 Jupyter 笔记本中,您可以在行首使用 ! 执行命令行方法,例如

    ! conda install pandas --yes

    关于内部工作的更多细节在这个问题中讨论: Running interactive command line code from Jupyter notebook

    感谢@PaulH 指出需要添加--yes 标志。

    【讨论】:

    猜你喜欢
    • 2017-10-27
    • 2020-11-22
    • 2018-07-19
    • 2018-07-28
    • 1970-01-01
    • 2019-04-10
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多