alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[883] Install Python package using Jupyter Notebook

In the installation of packages in Jupyter Notebook, I encountered an issue. I installed packages using Command Prompt (CMD), but when I try to import them in Jupyter Notebook, it prompted the error that "No module named XXX". This blog is trying to solve this issue.

ref: Install Python package using Jupyter Notebook

ref: 解决python Jupyter不能导入外部包问题


Installing Python Library in Jupyter

Using ! pip install

To install Python libraries, we use pip command on the command line console of the Operating System. The OS has a set of paths to executable programs in its so-called environment variables through which it identifies directly what exactly the pip means. This is the reason that whenever pip command can directly be run on the console.

In Jupyter, the console commands can be executed by the ‘!’ sign before the command within the cell. For example, If the following code is written in the Jupyter cell, it will execute as a command in CMD.

! echo GeeksforGeeks

Similarly we can install any package via jupyter in the same way, and it will run it directly in the OS shell.

Syntax:

! pip install [package_name]

But using this method is not recommended because of the OS behavior. This command executed on the current version in the $PATH variable of the OS. So in the case of multiple Python versions, this might not install the same package in the jupyter’s Python version. In the simplest case it may work.

Using sys library

To solve the above-mentioned problem, it is recommended to use sys library in Python which will return the path of the current version’s pip on which the jupyter is running. sys.executable will return the path of the Python.exe of the version on which the current Jupyter instance is

Syntax:

import sys
!{sys.executable} -m pip install [package_name]

By the above code, the package will be installed in the same Python version on which the jupyter notebook is running.

posted on   McDelfino  阅读(13)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-09-22 【480】Keras实操
点击右上角即可分享
微信分享提示