Sitemap

Setting up Python Environment and Kernel

5 min readNov 13, 2025

--

Python is a very versatile language which is used in many purposes. While you start coding on python you need to set up your system first. If you are working in a .py(python) file then you need to set up an environment specific to your project. Similarly if you are working in Jupyter notebook( Specially for data scientist) you need to connect your Kernel to a environment. This articale is dedicated to all this seting up and concepts. I will proceed by asking different questions. I am considering myself as a beginer and I am asking step by step question and trying to clarifying that. In this way all the concepts will be clear.

What is a python environment?

A python environment is a self-contained workspace that includes:

  1. A specific version of python
  2. A set of installed packages/libraries(like numpy ,pandas , matplotlib)
  3. Its own path and settings

You can think of it as a “sandbox” where your code runs — isolated from the rest of your system.

What is Kernel in jupyter notebbok?

A kernel is a program that runs and interprets your code, keeps track of variable states, and returns results to the notebook interface.

How it works:

You write code in a cell (for example, Python code).When you run the cell, the code is sent to the kernel.The kernel executes it and sends the output (text, plots, errors, etc.) back to be displayed below the cell.

Example:
In a standard Jupyter Notebook, the default kernel is usually Python (like ipykernel).
But you can install and use kernels for other languages too — like R, Julia, or JavaScript — if you have the right setup.

Why we don’t need any kernel to run .py file but we need it to run .ipynb file ?

For .py file:

The Python interpreter itself (like python.exe or /usr/bin/python) runs your file.It loads the code, executes it from top to bottom, and exits when it’s done.There’s no need for a separate kernel process — the interpreter is the runtime environment.So, a .py file runs directly through the Python runtime, not through an external engine.

For .ipynb file

A Jupyter Notebook isn’t just a plain text file — it’s a JSON document that stores:

  • Code cells
  • Markdown cells
  • Outputs (like plots, images, etc.)
  • Metadata (like kernel info)

To make it interactive (so you can run one cell at a time, keep variables in memory, and see live output), Jupyter needs a persistent process that stays alive in the background.

That’s what the kernel does:

  • It runs your code piece by piece, not all at once.
  • It keeps the state of variables, imports, and functions between cells.
  • It communicates with the notebook interface (the browser window) through a protocol (called Jupyter messaging protocol).

Without a kernel, the notebook would just be a static document — you couldn’t execute code, only view it.

Does environment required for both .py and .ipynb file ?

yes, both .py files and .ipynb notebooks require an environment, but how they use it differs slightly.

.py files and environments

When you run a .py file, like:

python myscript.py

The Python interpreter from your environment executes it.
So:

If you’re in a virtual environment (venv, conda, poetry, etc.), your script uses the packages from that environment.If you’re using the system Python, it uses the system packages.

So yes — a .py file always runs inside some environment, even if it’s just the default system one.

.ipynb files and environments

A Jupyter Notebook also runs inside an environment — but through a kernel that’s tied to it.Each kernel corresponds to an environment.
For example:

  • A Python 3.10 (env_ml) kernel might come from your Conda environment named env_ml.
  • A Python 3.11 kernel might be your system default Python.

So, when you open a notebook, Jupyter lets you choose which environment’s kernel to use.Therefore, .ipynb files also require an environment — the one where their kernel is installed.

How to create and activate an environment and how to connect it to a Kernel?

Using venv (built-in with Python)

To create an environment execute the following command:

    python -m venv myenv

After using this command an environment file will be created in your command directory with name “myenv”

Get Ronitbiswas’s stories in your inbox

Join Medium for free to get updates from this writer.

Now to activate the directory pass the following command:

For windows

 myenv\Scripts\activate 

Sometimes powershell/terminal shows error to execute the command “myenv\Scripts\activate “ as PowerShell’s execution policy doesn’t allow running script files (like activate.ps1), which is what your virtual environment uses for activation.In that case run the following command

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

What this does:

-Scope Process → applies only to the current PowerShell session (temporary, no system changes)

-ExecutionPolicy Bypass → allows running scripts just for this session

For Mac/Linux:

source myenv/bin/activate

Using Conda (recommended for data science / Jupyter):

conda create --name myenv python=3.11

Explanation:

conda create → creates a new Conda environment

— name myenv → sets the environment name

python=3.11 → installs a specific Python version (optional)

To activate it:

conda activate myenv

How to connect the environment to Kernel?

To add environment to jupyter notebook pass the following command

pip install ipykernel

python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"

This makes your environment appear in the Jupyter “Kernel” menu, so you can choose it inside notebooks.

Additional:

If you don’t have anaconda and you just have python installed in the system then also you can open jupyter notebook. You have to just install notebook module in the environment using the following command

pip install notebook

then after installing notebook you just have to pass the following command in the terminal

python -m notebook

note: when we run any .py file in terminal we don’t use -m but in case of running any module we use -m before the name of the module

Python is installed in my laptop but I am not beging able to run and command in command prompt or terminal. It giving the error that “python in not recognized”. what to do?

this is a very common problem in windows. Just find out the folder where the python.exe file is saved. Inside that folder you will get a script folder . now copy the following two path

  • path of the folder where the python.exe file is installed. for example C:\Programme File\python3.12.7
  • path of the script file. for example C:\Programme File\python3.12.7

Now press window key in your system. search edit environment variable . then edit environment variable. In editing tab put the furst path in 1st line and second tab in 2nd line . then press ok 3 times .

Now restart your terminal/command promt and you can see the following command are working

python --version
where python

--

--