Python jupiter notebook virtualenv

Using Jupyter Notebook in Virtual Environments for Python Data Science Projects

Learn how to install a kernelspec to access your Python data science virtual environments within Jupyter Notebook

In the Creating Virtual Environments for Python Data Science Projects, I explained how to install Pyenv and Virtualenv to manage your Python versions and virtual environments on mac OS Big Sur.

With that scaffolding in place, the next step will be to create a project directory, activate a new environment, and install some popular data science packages, like Pandas, Matplotlib, Seaborn, and Jupyter. Then, we will install a kernelspec so that these libraries will be available to use in Jupyter Notebook.

I. Create and activate a virtual environment with Pyenv and Virtualenv

First, we need to install the desired version of Python. Even though Python is already installed on your computer in multiple locations, Pyenv needs its own copy of the version your project will use. Unless you have a reason to use an older version, the newest stable release is a good place to start.

In the future, if you would like to use Python 3.9.1 for another environment, there is no need to reinstall it. You will only need to install each version of Python one time to use it.

Now that Pyenv has a copy of the Python version that you wish to use, you can create a virtual environment and assign it to that version.

% pyenv virtualenv 3.9.1 project_env
% pyenv virtualenv [python version] [environment name]

Next, your project will need a directory. You can create one by entering:

Then, assign the virtual environment as the local environment for that directory. Now the environment will open whenever you enter your project directory:

Источник

How To Use Virtual Environment And Jupyter Notebook

In this short tutorial, we’ll talk about how to use a Python Virtual Environment (venv) and the Jupyter Notebook. We’ll go through what they are, why, when, and how to use them.

Python Virtual Environment

Most of the time when we use Python, we’ll need to use some third-party libraries, like pandas, plotly, xlwings, etc. These don’t come with the standard Python installation.

Depending on the projects we work on, sometimes our task requires a specific version of a library, but we can keep only 1 version of a library at any given time. For example, if project A requires pandas version 1.0.1, and project B requires pandas version 1.2.0, there’s no way to install both on a computer.

Читайте также:  HTML Colors by Name

Python Virtual Environment (venv) solves this problem by creating a self-contained copy of Python plus all the libraries. When we create multiple virtual environments, each instance is self-isolated and doesn’t interfere with other environments, so we can have different versions of a library on our computer at the same time.

Create A Python Virtual Environment (venv)

We are going to use a Python module called venv , which is part of Python standard library and comes with the Python installation. Unlike other libraries, we don’t import venv inside a Python interpreter. Instead, we use it in a Command Prompt / Powershell / Terminal.

Before creating a virtual environment, we need to first decide where to place it, therefore we navigate to the desired folder location first. I’m going to create a new virtual environment in the venv_tut folder:

Once it’s created, we should see a folder called “tut_venv” appear in the current directory. Go into the subfolder by using the cd command

In the Scripts (or bin) folder, we should see a file named “activate”. Simply in the command prompt type activate to activate the virtual environment. To confirm that the virtual environment is activated, in the command prompt window, we should see (tut_venv) appear in front of the current input line.

Test The Virtual Environment

Let’s install pandas in this virtual environment and test if it works. Save the following line into a Python file.

Note if we attempt to run this code within the IDLE, it might not work, because the current IDLE is not in the virtual environment that we just installed pandas for. Depending on your machine, this current “environment” might not have pandas. To use the correct venv to run our code, we need to execute the code from the console where the venv has is activated. To do that, just type:

This time, the code will run within the correct virtual environment. Now if we need to install another version of pandas, we just need to create a new virtual environment and install it there.

When To Use A Virtual Environment

A virtual environment is particularly useful when you need to constantly switch between different versions of a library. Another instance is when your application requires multiple libraries, it’s known that having too many libraries installed in one environment might cause potential conflicts between libraries.

Although some believe that it’s a good practice to create a new virtual environment for every Python project we work on (because that way the libraries of every project are isolated from the system and each other). I would argue that a separate virtual environment is not necessary unless for larger projects. For example, most of the projects I work on require pandas. Therefore I just have pandas installed system-wide without needing to create a virtual environment every time I start a new project.

Jupyter Notebook

Jupyter Notebook is a web-based IDE (interactive development environment) for many programming languages, including Python. In fact, the three core languages that Jupyter supports are Julia, Python, and R. As its name suggests it’s a “notebook”. It means that it can contain both computer code and human-readable content such as text, pictures, etc – just like a physical notebook.

Читайте также:  Как создать интерфейс php

The Jupyter Notebook runs in a web browser, and it’s also interactive. The interactivity is much better than the vanilla Python IDLE in my opinion.

Install Jupyter Notebook

If you already have Python installed on your computer, we can use pip to install Jupyter Notebook.

Once the installation finishes, in a console, type jupyter notebook to open it. You will see that it executes in the console and automatically opens up a browser for the notebook. DO NOT CLOSE THE CONSOLE! The console is the back-end engine, and the browser is merely an interface. If you close the console then Jupyter Notebook will shut down.

Create A Virtual Environment For Jupyter Notebook

Using a virtual environment for Jupyter Notebook is a little different from using it for the vanilla IDLE. In Jupyter Notebook, there’s a thing called IPython Kernel, which is essentially the computational engine that executes the Python code in the back-end. Once we create a virtual environment, we can link it with the kernel so we don’t have to manually activate the venv every time we need it.

To register the venv with the kernel, we need to pip install another Python module ipykernel .

Once the installation is finished, type the following in the console:

And we’ll see the following message:

  1. Shutdown Jupyter Notebook
  2. Deactivate the current venv
  3. Re-open Jupyter Notebook
  4. Check in “Open”, we should see the venv name we that we just created ‘tut-venv’. Open a new file with this kernel
  5. Execute code to check

Remove Virtual Environment From Jupyter Notebook

To remove the venv, type jupyter kernelspec list in command prompt to confirm the venv name. We’ll see something like the following:

Let’s remove the “tut_venv” that we created for this tutorial. To delete, type jupyter kernelspec uninstall tut_venv , and we’ll see the following confirmation:

One comment

Awesome post, thanks! How do I manage different virtual environments? In VS Code there’s a way to have the virtual environment depending on what folder I’m working in . That way, I can work on several different projects with various virtual environment configurations

Источник

Create Virtual Environment using “virtualenv” and add it to Jupyter Notebook

Are you a Machine Learning Engineer and working with Python and Jupyter Notebook? In this article, you will see why Virtual Environment is needed, the difference between some existing tools and how to add a virtual environment to Jupyter Notebook.

Here is the article outline:

  • Why need Virtual Environment?
  • What is the difference between virtualenv , virtualenvwrapper , penv and venv ?
  • Create a Virtual Environment using virtualenv
  • Add Virtual Environment to Juypter Notebook

Why need Virtual Environment?

Like other programming language, Python has its own way of downloading, storing and resolving packages (or libraries). By default, every Python project on your machine will use the default Python site-packages directory (Which is associated with the main Python installation and is represented as base(root) environment). You can find that by:

>>> import site
>>> site.getsitepackages()
['/Users/admin/anaconda3/lib/python3.7/site-packages']

Imagine a scenario where you are working on two machine learning projects and one of them uses TensorFlow v1.5 and the other uses TensorFlow v2. This would be a real problem since Python can’t differentiate between versions in the site-packages directory. Both TensorFlow v1.5 and TensorFlow v2 would reside in the same directory with the same name. Since there is no differentiation between versions, both projects would be required to use the same version, which is unacceptable in this case. This is where virtual environments tools come into play.

Читайте также:  Xampp httpd conf php

The main purpose of Python virtual environments is to create an isolated environment for Python projects. Each project can have its own dependencies, regardless of what dependencies every other project has.

In addition, a virtual environment is also useful when you need to work on a shared system and do not have permission to install packages as you will be able to install them in the virtual…

Источник

How to install jupyter notebook on ubuntu 20.04/18.04 using python 3 and virtualenv

An open-source web application, Jupyter Notebook lets you create and share interactive code, visualisations, and more. It is an essential software used by data scientists. It is often used for working with data, statistical modelling, and machine learning.

If you are using windows the best way to setup Jupyter Notebook is by using Anaconda.

if you want to keep the install minimal, and is not likely to use other preinstalled packages from anaconda this article is for you. Using virtualenv package we can create a virtual environment and install the jupyter notebook easily.

By the end of this guide, you will be able to run Python 3 code using Jupyter Notebook running on your system.

Prerequisites

In order to complete this guide, you should have a Ubuntu system with non-root user with sudo privileges configured.

Step 1 — Set Up Python

Update the local apt package index and then download and install the packages:

Next, install pip and the Python header files, which are used by some of Jupyter’s dependencies:

sudo apt install python3-pip python3-dev 

Step 2 — Create a Python Virtual Environment for Jupyter

Upgrade pip and install the package by typing:

sudo -H pip3 install --upgrade pip sudo -H pip3 install virtualenv 

The -H flag ensures that the security policy sets the home environment variable to the home directory of the target user.

With virtualenv installed, we can start forming our environment. Create and move into a directory where we can keep our project files.

mkdir ~/ml_projects cd ~/ml_projects 

Within the project directory, we’ll create a Python virtual environment.

milindsoorya@predator:~/ml_projects$ virtualenv houseprices_env 

it will install a local version of Python and a local version of pip. We can use this to install and configure an isolated Python environment for Jupyter.

Before we install Jupyter, we need to activate the virtual environment. You can do that by typing:

source houseprices_env/bin/activate 

Step 3 — Install Jupyter

Once the virtual environment is activated, use pip instead of pip3 , even if you are using Python 3. The virtual environment’s copy of the tool is always named pip , regardless of the Python version.

(houseprices_env) milindsoorya@predator:~/ml_projects$ pip install jupyter 

Step 4 — Run Jupyter Notebook

You now have everything you need to run Jupyter Notebook! To run it, execute the following command:

(houseprices_env) milindsoorya@predator:~/ml_projects$ jupyter notebook 

Now the notebook should open automatically in your browser. If it is not opening you can manually copy the URL from the terminal it will usually have a port number of 8888.

You might also like:-

Learn about building products as a Data Scientist

Get a once-per-month email with my latest article and additional details about my launches, products, and experiments ✨

No spam, sales, or ads. Unsubscribe as your heart desires.

Источник

Оцените статью