How To Use Virtual Environment inside Jupyter lab
In This Article I will explain how to activate your virtual environment inside jupyter lab
In this article you will learn
Today I want to talk to you about a topic on how we can use the virtual environment feature in Jupiter Lab.
You may also have used Jupyter for programming. If you do not know about Jupiter, I can explain to you in simple language that it is a Python programming environment that runs on your browser and one of its main features is that you can write and execute your own code in separate cells.
And if you have worked with Jupyter, there are probably two things that have upset you??
first, the lack of beautiful themes?? ♂? and second, the difficulty of activating the virtual environment??
Today I want to have an introduction for you about jupyterlab and why it is better to use modern jupyter(jupyter lab) instead of classic jupyter(jupyter notebook).
First, let me tell you about what is a jupyterlab is and how it different from a normal jupyter.
jupyterlab is actually the next generation of jupyter notebook, which has much more useful and more capabilities than the classic Jupiter. In jupyterlab, we can open different tabs at the same time, we can have better control over the codes and use HTML, Mark down, etc... in our code. We can also easily give beautiful colors to our work environment, which I like very much :)
Well, if you want to install Jupyterlab, just use this command.
pip install jupyterlab
After installation, you can easily run it using the following command
jupyter-lab
But before running, we want to create a virtual environment and activate it inside Jupyterlab. There are several ways to install virtual environments for python. I use this method which is easily installed everywhere.
python -m venv virtualENV
Now we need to activate our environment in the terminal. It depends on whether you are on Windows or Linux and Mac, I am on Windows and I use this command.
#inside project directory
.\virtualENV\Scripts\activate
If you are on Mac or Linux use the following command
source env/bin/activate
That's great. Now we have a activated virtual environment.(you should see your virtual environment name before directory name in parenthesis)
领英推荐
now we need to install ipykernel in our virtualENV you can do this by adding ipykernel in requiements.txt file for install it separately.
pip install ipykernel
And If you want to install your own requirements files, you can do this now.
pip install -r requirements.txt
Great, everything has been prepared so far, you can see the result in the picture below.????
Now, before running Jupyterlab, we want to introduce our virtual environment to jupyterlab so that we can use it(Remember to use this command inside of virtual environment)
#python -m ipykernel install --user --name=[your virtual environment name ]
in my case:
python -m ipykernel install --user --name=virtualenv
it is finished?Now we enter jupyterlab ????
As you can see in the picture above, I used a module that I did not have installed on the system and I wanted to use it inside the virtual environment.
But now it is enough to choose our virtual environment in the kernel section and Boom??
As you can see, our new kernel is on the list and we can install and use our own modules inside it.????
Finally, if you want to use this beautiful theme, you can choose it under settings/Jupyter themes. Note that if you already have Jupyterlab and its only have 2 themes, you should install the latest version.??
Also for see list of all installed kernels we can use following command
jupyter kernelspec list
and for removing kernel from jupyterlab we can use
jupyter kernelspec uninstall virtualenv
I also suggest that you Do Not Use Ready-Made Themes that can find in GitHub and other sites, as you may experience various problems.??
Thank you for your attention and time for reading this article. This was my first article in English. I'm glad to know your comments.??????
Also, if you have any questions, be sure to ask in the comments, I will be happy to help you ??
MBA, PMP
4 个月Thank you for this, it was just what I was looking for ??
Kernel created this way sometimes fails to import packages installed in editable mode within venv (pip install -e). Or it works when kernel is freshly created, but few days later I need to remove and install kernel again to make it working... do you have an idea why?
Sophomore | Eager for AI in Computer Science & Neuroscience
1 年Very quality article, comes in handy when I need it most.
Statistical Analyst @ Revista32.mx | Statistical Analysis, Sampling Frames
1 年Thank you so much for such handy information. It saved me today.
quantum-computing
2 年great article thanks