官术网_书友最值得收藏!

  • Python Data Science Essentials
  • Alberto Boschetti Luca Massaron
  • 221字
  • 2021-08-13 15:19:36

Installing packages directly from Jupyter Notebooks

Jupyter magic commands are really efficient in accomplishing different tasks, but you may sometimes find it difficult to achieve installing new packages during a Jupyter session (and it will happen often since you are using different environments based on conda or env). As Jake VanderPlas explained in his blog post Installing Python Packages from a Jupyter Notebook (https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/), it is a matter of fact that Jupyter kernels are different from the shell you started from, that is, you may be upgrading a wrong environment when you issue magic commands such as !pip install numpy or !conda install --yes numpy.

Unless you are using the default Python kernel that's active on the shell on the notebook, you actually won't succeed because your Jupyter Notebook is pointing to a different kernel than the one operated by pip and conda at a shell level.

The correct approach for installing, let's say, NumPy, using pip under a Jupyter Notebook is by creating a cell like this:

In: import sys
!"{sys.executable}" -m pip install numpy

Instead, if you want to use conda, this is the cell you have to create:

In: import sys
!conda install --yes --prefix "{sys.prefix}" numpy

Just replace numpy with any package you would like to install and then run, and the installation is guaranteed to succeed.

主站蜘蛛池模板: 遂昌县| 灵宝市| 乌兰浩特市| 通化县| 辽宁省| 南郑县| 铜梁县| 三穗县| 涟源市| 弋阳县| 连云港市| 云和县| 响水县| 托克托县| 保定市| 贵州省| 万盛区| 灵石县| 永平县| 蒙山县| 枞阳县| 福鼎市| 通州区| 卫辉市| 泾川县| 潢川县| 乐都县| 芦溪县| 山东省| 叙永县| 凯里市| 探索| 宜城市| 钦州市| 咸阳市| 临沧市| 通化市| 确山县| 兴文县| 潼关县| 德庆县|