When you're working in a Jupyter Notebook (like Google Colab or other notebook environments), installing Python packages is often necessary. You can install packages directly within the notebook using the Python pip
package manager, which is typically done through shell commands.
In Jupyter and Colab, there are two ways to use pip
to install packages:
%pip
(Magic Command)!pip
(Shell Command)%pip
(Magic Command)%
(for single-line commands) or %%
(for multi-line commands).%pip
is a magic command specifically designed to install packages within the current notebook kernel (the Python environment in which the notebook is running).When you run %pip install <package>
, Jupyter/Colab ensures that the installed package is available in the current Python environment that the notebook is running. It's integrated into the notebook's system, so it knows how to properly handle the installation and environment.
Syntax:
%pip install <package_name>
Example: below command will install the numpy
package into the environment running the notebook.
%pip install numpy
%pip
?!pip
(Shell Command)!
symbol in Jupyter or Colab is used to run shell commands (commands that would normally be run in the terminal or command prompt). When you prefix pip
with !
, it tells the system to run pip
as a shell command.!pip install
is a more general way of invoking pip
from within Python code in a notebook.When you use !pip install <package>
, it's the same as running pip install
from the command line or terminal. The system runs pip
to install the package into your environment. However, when running inside a notebook, the package is installed system-wide, not necessarily in the notebook’s kernel environment.
Syntax:
!pip install <package_name>
Example: Below command will install the pandas
package into the system environment, and it should be available for use in the notebook if the system environment matches the kernel.
!pip install pandas
!pip
?!pip
works everywhere, not just in notebooks. It can be used in any Python script or environment that allows shell commands.pip
as if you were in the terminal, so it works with any command-line options and behavior that pip
supports.Let's see short comparision:
Feature | %pip | !pip |
---|---|---|
Context | Designed specifically for Jupyter/Colab | General shell command, works everywhere |
Environment | Installs directly in the notebook kernel | Installs in the system Python environment |
Integration | Integrated with Jupyter’s kernel | May not integrate well with Jupyter kernel |
Magic Command | Yes (specifically for notebooks) | No (standard shell command) |
%pip
or !pip
?%pip
if you are working in a Jupyter Notebook or Google Colab and want to install packages that are directly available in the notebook environment.
!pip
when you are:
pip
.%pip
is a Jupyter-specific magic command designed to handle package installations within the notebook's kernel, ensuring the environment remains consistent with the notebook’s execution.!pip
runs shell commands within a notebook or script and behaves like using pip
from the command line. In Jupyter or Colab notebooks, %pip
is generally preferred for installing packages because it integrates better with the notebook’s environment. However, both commands will work, and they serve different purposes depending on the environment you're working in. Hope this can help you understanding, please have a nice day!
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him