Creating and Using Virtual Environment on Jupyter Notebook with Python | by Cornellius Yudha Wijaya | Towards Data Science

As a Data Scientist, we would like to experiment with many different packages and codes to create that amazing analysis and superb machine learning model. The problem with this kind of experiment is that many packages dependencies overlap one another, and sometimes it disturbs our working environment. In this case, we need what we called Virtual Environment.
A virtual environment, just as the name implies, is an environment that is virtually created. It is an empty place isolated from our local source where we could install and play around with code and packages.
It is a best practice to use Virtual Environment where we are testing a new package or creating a new pipeline.
While it is a best practice, sometimes people wonder how we implement the newly created virtual environment we create to be used in the Jupyter Notebook. In this case, I want to show you how to create and use the virtual environment in Jupyter Notebook.
Creating a Virtual Environment using Python is a simple task, although, in this article, I assume you already installed Python 3. As a starter, we need to install the following package first via pip.
pip install virtualvenv
In your CLI or Command Prompt, we would type the following code to create the virtual environment.
virtualvenv myenv
where you could replace myenv with any virtual environment name you want.
Creating the virtual environment is not enough; we also need to activate this virtual environment to use it. To do that, try to type the following code.
.\myenv\Scripts\activate
With that, we already have our virtual environment set. You might want to check your default virtual environment python version and what packages automatically installed.
#check the python versionpython --version#check the packagespip list
When you think you do not want to be inside the virtual environment anymore, you could type deactivate.
Many people installing Anaconda to simplify packages management. With Anaconda, we could create a virtual environment as well. The steps are similar to the one we did previously with Python, but we only rely on the conda command.
To create a virtual environment, we only need to run the following code.
conda create -n myenv python=3.6
Where myenv is the name of the virtual environment and python= is the version of python you want inside the virtual environment.
To use the virtual environment, we need to activate that virtual environment. We can do that by running the following code.
conda activate myenv
With this, the virtual environment is activated, and you could install any packages dependencies you want inside this virtual environment.
If you have finished the task, you might want to get out of the virtual environment by running the following code.
conda deactivate
If you want to check all the virtual environment you already created in the Anaconda, you can check it using conda env list and if you want to remove any environment, you could run the code conda env remove -n myenv.
When you have created a virtual environment, you would realize that the virtual environment is separate from your Jupyter Notebook. We need to set up a few things before we could have our virtual environment in the Jupyter Notebook.
First, activate your virtual environment and run this code.
pip install --user ipykernel
We need to manually add the kernel if we want to have the virtual environment in the Jupyter Notebook. That is why we need to add it by running this code.
python -m ipykernel install --user --name=myenv
With this, we have set up our virtual environment kernel and ready to be used in the Jupyter Notebook.
You could check in your Jupyter Lab interface or when you create a new Jupyter Notebook.
The notebook you created based on the virtual environment would have all the packages you already installed in this environment.
If you have finished with the virtual environment and did not need it anymore, you could remove it using this code.
jupyter kernelspec uninstall myenv
I have shown you to create a new virtual environment using Python or Conda. You only need to create a virtual environment, activate it, and add the kernel.
I hope it helps!
Visit me on my LinkedIn or Twitter
If you are not subscribed as a Medium Member, please consider subscribing through my referral.