Install Python package using Jupyter Notebook

Posted in

Install Python package using Jupyter Notebook
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    Jupyter Notebook is an open-source web-application that can be used to write, execute, store, and share code in a single document file. Although using Jupyter notebook we can write and execute code for 40 different programming languages, but Pythoninsta highly uses it for Python Data Science and ML projects. There are two ways of Installing Jupyter notebook for Python, we recommend you to read this article if you want to know how to install Jupyter notebook with coda and pip command.

    In the Jupyter Notebook, we have the terminology called "cell" where we can write code, markup language, raw code, and Heading for our notebook document. The type of the cell can be changed according to the requirement. By default, when we create a Jupyter Notebook, it uses the installed Python as a kernel to execute the Python code, this means it can only use the packages that we have installed for our Python environment.

    Now let's say you wish to use a new Python package that is not installed for your Python, so what will you do?. You simply go to your terminal or command prompt and install it using the Python pip command, which is one of the obvious options you have. Sometimes, even after installing the package using the terminal and pip command, it does not guarantee that you can import it instantly.

    You might have to close your Jupyter Notebook and restart it, which can be a tedious task. So now the question is- can we install Python packages using Jupyter Notebook. Yes, there are two techniques to install Python Package Using Jupyter Notebook.

    How to Install Python Library/Package using Jupyter Notebook?

    1. Install Python package in Jupyter Notebook using "! pip install."
    2. Install Python package in Jupyter Notebook using " Python sys library. "

    1. How to Install Python package in Jupyter Notebook using "! pip install."

    We can use the Jupyter Notebook cells as a command line or terminal shell by using the ! symbol as a prefix. Similar to the pip install <package_name> command we use in our terminal or command prompt we can use the same command in our Jupyter Notebook cell to install the Python package from the Notebook itself. To execute a shell command in the Jupyter Notebook we use the ! sing before the command. Now let's install the Python numpy library by using Jupyter Notebook.

    ! pip install numpy

    For some operating systems where there are multiple users exist on the system, the above command will not work and ask you for the user permission in that case you can use the following command.

    ! pip install numpy --user

    Many Pythoneer also uses conda distribution for installing Jupyter Notebook and  Python packages, in that case, the above command won't be of any help. If you are using Jupyter Notebook with anaconda and want to install python packages using Jupyter Notebook then you need to write the following command on your Notebook cell

    !conda install --yes numpy

    Similar to the pip install command we use the conda install command to install Python packages for the conda environment. The --yes command will make all the permission to yes even if the operating system has multiple users.

    2. Install Python package in Jupyter Notebook using "Python sys library."

    The approach of installing Python Package using sys library in Jupyter Notebook is similar to the above ! pip install command . Many Pythonista use multiple Python versions on the same system, for instance, you might have Python3.8 and 3.9 running side by side on the same system. And using Python3.9 for your latest Jupyter Notebook protect and 3.8 for an old project. So in that case, you do not install one dependency package of a project into another using PIP command in Jupyter notebook, it would be a great practice to use Python sys library, which will return the current versions of pip on which your Jupyter Notebook is running.

    Syntax:

    import sys 
    !{sys.executable} -m pip install <python_package>

    How to Install numpy with sys library in Jupyter Notebook

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

    If  you are using Conda distribution to run jupyter notebook ten use the following command

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

    Conclusion

    In this tutorial, you learned how to install Python Packages directly from the Jupyter Notebook cells. By putting the ! sign in the cell we can use the Jupyter Notebook cell as a terminal shell.  Like the command shell, you can also use the different terminal commands with ! sign, for instance, you can check the current directly from the Jupyter Notebook using ! cd command.

    People are also reading:

    Leave a Comment on this Post

    0 Comments