Sep-13-2023, 12:35 PM
Hello Forum,
I think I am clear on what a virtual environment is: it is a "folder" that contains specific Python libraries/modules and a specific Python interpreter (we can choose the version).
In essence, does this all mean that, even if we create a new virtual environment and launch jupyter notebook from it, the kernel that is used, by default, is the kernel (Python interpreter) associated with the (base) environment! This is a problem in the notebook. To avoid that, we need to create a new jupyter kernel and select that kernel in the notebook to be able to use the modules/libraries contained inside the activated environment (ven1). This sounds redundant since (ven1) already has its own Python interpreter but are creating a new one using the library ipykernel
Thank you!
I think I am clear on what a virtual environment is: it is a "folder" that contains specific Python libraries/modules and a specific Python interpreter (we can choose the version).
- Every environment has its own Python interpreter. The purpose of virtual environments is to keep projects and their dependencies separate so there are no conflicts.
- Virtual environments, once created, are generally stored all together in a specific and regular folder on our computer. The folder can be anywhere. And the Python scripts that use a specific and activated environment should not be saved inside the environment itself (even if they can be) but in some other folder.
- I am using Anaconda and the Anaconda Navigator to create virtual environments at the Anaconda terminal: I start in the (base) virtual environment and then use conda to create a new environment, (ven1):
(base) C:\Users\Tom\python_env\ conda create --name ven1 # to create a new environment called ven1
(base) C:\Users\Tom\python_env\ conda activate ven1 # to activate the environment ven1
(ven1) C:\Users\Tom\python_env\ jupyter notebook # this launches jupyter notebook from (ven1). The notebook should use the Python interpreter associated with environment (ven1)? But it doesn't!
- When I launch jupyter notebook from (ven1), the jupyter kernel for the notebook is the SAME as the kernel that would be used if I launched the Jupyter notebook from the (base) environment. Why? I expected the kernel to be automatically different since (base) and (ven1) have different Python interpreters.
- A jupyter kernel is a different name for Python interpreter, correct? So when I launch Jupyter from different environments, shouldn't we there be different kernels since we are using a Jupyter notebook from two different virtual environments having different Python interpreters?
- To avoid the problem, we have to run this code to install a new kernel in (ven1)...but doesn't (ven1) already have its own kernel which is different from the kernel of (base)?
(ven1) C:\Users\Brett\python_env\ conda install ipykernel # we install the package ipykernel inside the ven1 environment
(ven1) C:\Users\Brett\python_env\ ipython kernel install --user --name=ven1kernel # creates a new kernel
(ven1) (C:\Users\Brett\python_env\ jupyter kernelspec list #lists all jupyter kernels
In essence, does this all mean that, even if we create a new virtual environment and launch jupyter notebook from it, the kernel that is used, by default, is the kernel (Python interpreter) associated with the (base) environment! This is a problem in the notebook. To avoid that, we need to create a new jupyter kernel and select that kernel in the notebook to be able to use the modules/libraries contained inside the activated environment (ven1). This sounds redundant since (ven1) already has its own Python interpreter but are creating a new one using the library ipykernel
Thank you!