Using a Conda Environment in Jupyter via Miniconda - python-3.x

Has anyone found a way to successfully set up an environment for a jupyter notebook without having the full version of Anaconda installed on your system? I am using Miniconda and have been unable to use a conda environment with the jupyter notebook platform. Note that with Miniconda source activate myenv will not work.
I also tried using ipykernel which did successfully get an environment to appear in my notebook (via the command: python -m ipykernel install --user --name myenv). However, this environment is still made as a copy of my base environment. Commands similar to conda create -n test_env python=3.7 don't seem to be compatible with ipykernel unless I am missing something.

You don't need to run ipython from the environment you are adding.
Since you've got an environment with jupyter, lets call that Jenv.
go to your miniconda install location
Scripts/activate Jenv
conda create -n SomeOther_vEnv python=3.6.8 whateverPackagesYouWant
python -m ipykernel install --sys-prefix --name SomeOther_vEnv --display-name "foo"
Now jupyter when started from your Jenv should see "foo". You can't start jupyter from foo, but you can use it in a jupyter server started from Jenv. (unless whateverPackagesYouWant included jupyter of course)

Related

Cannot connect to rust kernel from a jupyter server in a conda env

I have been trying to install a Rust kernel for a Jupyter server inside a dedicated conda environment but I get errors.
Slighty adjusting steps from:
https://depth-first.com/articles/2020/09/21/interactive-rust-in-a-repl-and-jupyter-notebook-with-evcxr/
conda create -n rusttest
conda activate rusttest
conda install -c conda-forge rust jupyterlab
conda install -c anaconda cmake -y
cargo install evcxr_jupyter
Add $HOME/.cargo/bin to my PATH variable (export PATH)
evcxr_jupyter --install (here I already see the kernel is installed outside the env)
jupyter lab
The kernel is visible on the dashboard however when I try to start a notebook with it the connection fails and I get:
Error: Failed to find sysroot for Cargo.toml file /tmp/.tmpbZ0Pkw/Cargo.toml. Is rust-src installed?
I have tried manually:
jupyter kernelspec install {MY_PATH_DURING_PKG_INSTALLATION}/Jupyter/kernels/rust --sys-prefix
And I get:
[InstallKernelSpec] Installed kernelspec rust in {MY_PATH}/miniconda3/envs/rusttest/share/jupyter/kernels/rust
Which seems OK (inside the correct conda env) but the error persists.
Is there any way to add a working kernel just to this one jupyter server inside that env?
(I want Rust to be gone when I start jupyter lab from another conda env)
EDIT
After digging into this I think the first thing is to get rust-src installed inside that conda environment... I don't know how...
Solved:
I needed to download https://static.rust-lang.org/dist/2022-05-19/rust-src-1.61.0.tar.gz manually and extract it under the env dir such that XXX/miniconda3/envs/rusttest/lib/rustlib/src/rust exists.

Jupyter lab cannot connect to a kernel of a pipenv environment

I have python 3.8.5 installed with conda (miniconda). Jupyter lab is installed in the 'base' environment. I created a project and its virtual environment (fibonacci-6zMY4jHo) with 'pipenv'. To add the fibonacci-6zMY4jHo as a kernel into Jupyter lab, I did the following:
activated fibonacci-6zMY4jHo:
pipenv shell
installed ipykernel:
pipenv install ipykernel
Added fibonacci-6zMY4jHo to jupyter lab:
ipython kernel install --user --name=fibonacci-6zMY4jHo
Next:I run jupyter lab from the base environmnet. The list of kernels shows fibonacci-6zMY4jHo, but when I select it, jupter is not able to connect to it.
Any idea what is wrong?

How to create new Anaconda Environments within JupyterHub and bind a Environment to a new Jupyter- Notebook?

I installed JupyterHub using Docker container.
When I log in the web interface, I see the following environment options:
After a while of loading, I see the Launcher tab as follows:
Now, I would like to create some new Anaconda Environments (MyDevEnvPy3.7, MyTestEnvPy3.7, MyDevEnvPy3.8, MyTestEnvPy3.8), and start a new JupyterNotebook that uses exactly one of these Anaconda Environments mentioned above.
The idea is, that I'd be able to configure each Anaconda Environment with different packages and python versions (i.e. 3.7 or 3.8).
Especially in the case, if I need to upgrade some Python packages, i.e. in my MyDevEnvPy3.7, I would like to upgrade the Python packages first in MyTestEnvPy3.7, to see if my code is still running. Then I would like to do like this using MyDevEnvPy3.7.
How can I do this?
Or is it even possible?
I've only worked with the Anaconda Navigator So far. Anaconda Navigator was my starting point, where I configured my Environments in the "Environment" tab first, and then switched to the "Home" tab, selected the Anaconda Environment to be used and launched the Jupyter Notebook (using this Environment). But now in JupyterHub, I don't have a clue, what's the best practice to start a Jupyter Notebook using a specific Anaconda Environment ...
This is pretty much a standard functionality provided by Jupyter/IPython, which technically is called 'adding new kernel'. Please follow these steps (in a shell terminal):
Say you have a conda env named MyTestEnvPy3.8. Activate this env.
Install ipykernel in the conda env you want to add to Jupyter:
$ conda install ipykernel
Install the kernel
$ python -m ipykernel install --user --name test --display-name "MyTestEnvPy3.8"
You can always change the name (how this kernel appears in your jupyter directory) and display-name (how it appears in the JupyterLab's 'Select Kernel' window).
Check if you installed the kernel successfully:
$ jupyter kernelspec list
And now you can find the kernel you installed in JupyterLab's 'Select Kernel' window. To interface you called 'launcher' is the start window of JupyterLab. JupyterHub hosts multiple Jupyter server, once you launch the JupyterLab from it, you are in the world of JupyterLab!
There's no such thing as 'bind a kernel with the notebook', notebook is just a file format, when you open it in JupyterNotebook or JupyterLab interface, you can switch between the kernels anytime.
I finally managed to create differenct Anaconda Environments:
Open "Terminal" in Launcher tab
Paste in the following commands:
# Creates a new Anaconda Environment called "MyDevEnvPy3.7" using Python Version 3.7
# in silent mode (parameter "-y")
conda create -n MyDevEnvPy3.7 python=3.7 -y
# Activates the new created Anaconda Environment "MyDevEnvPy3.7"
conda activate MyDevEnvPy3.7
# Installs "ipykernel" in active Anaconda Environment "MyDevEnvPy3.7" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyDevEnvPy3.7" in Anaconda Environment "MyDevEnvPy3.7"
python -m ipykernel install --user --name MyDevEnvPy3.7 --display-name="MyDevEnvPy3.7"
# Deactivates the new created Anaconda Environment "MyDevEnvPy3.7"
conda deactivate
# Creates a new Anaconda Environment called "MyTestEnvPy3.7" using Python Version 3.7
# in silent mode (parameter "-y")
conda create -n MyTestEnvPy3.7 python=3.7 -y
# Activates the new created Anaconda Environment "MyTestEnvPy3.7"
conda activate MyTestEnvPy3.7
# Installs "ipykernel" in active Anaconda Environment "MyTestEnvPy3.7" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyTestEnvPy3.7" in Anaconda Environment "MyTestEnvPy3.7"
python -m ipykernel install --user --name MyTestEnvPy3.7 --display-name="MyTestEnvPy3.7"
# Installs "numpy" in active Anaconda Environment "MyTestEnvPy3.7" in silent mode
conda install numpy -y
# Deactivates the new created Anaconda Environment "MyTestEnvPy3.7"
conda deactivate
# Creates a new Anaconda Environment called "MyDevEnvPy3.8" using Python Version 3.8
# in silent mode (parameter "-y")
conda create -n MyDevEnvPy3.8 python=3.8 -y
# Activates the new created Anaconda Environment "MyDevEnvPy3.8"
conda activate MyDevEnvPy3.8
# Installs "ipykernel" in active Anaconda Environment "MyDevEnvPy3.8" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyDevEnvPy3.8" in Anaconda Environment "MyDevEnvPy3.8"
python -m ipykernel install --user --name MyDevEnvPy3.8 --display-name="MyDevEnvPy3.8"
# Deactivates the new created Anaconda Environment "MyDevEnvPy3.8"
conda deactivate
# Creates a new Anaconda Environment called "MyTestEnvPy3.8" using Python Version 3.8
# in silent mode (parameter "-y")
conda create -n MyTestEnvPy3.8 python=3.8 -y
# Activates the new created Anaconda Environment "MyTestEnvPy3.8"
conda activate MyTestEnvPy3.8
# Installs "ipykernel" in active Anaconda Environment "MyTestEnvPy3.8" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyTestEnvPy3.8" in Anaconda Environment "MyTestEnvPy3.8"
python -m ipykernel install --user --name MyTestEnvPy3.8 --display-name="MyTestEnvPy3.8"
# Installs "numpy" in active Anaconda Environment "MyTestEnvPy3.8" in silent mode
conda install numpy -y
# Deactivates the new created Anaconda Environment "MyTestEnvPy3.8"
conda deactivate
Select "File" --> "New Launcher"
Now, you should see the 4 new Anaconda Environments:
By selecting the desired Anaconda Environment in the Notebook section, a new Jupyter Notebook will be launched, and is bound to the selected Anaconda Environment.
Let's check this:
As you can see on the upper right side, the current selected Anaconda Environment is MyDevEnvPy3.7. We expect the Python version 3.7, and that numpy can't be imported, as it wasn't installed in this Anaconda Environment before.
As you can see on the upper right side, the current selected Anaconda Environment is MyTestEnvPy3.7. We expect the Python version 3.7, and that numpy can be imported, as it was installed in this Anaconda Environment before.
As you can see on the upper right side, the current selected Anaconda Environment is MyDevEnvPy3.8. We expect the Python version 3.8, and that numpy can't be imported, as it wasn't installed in this Anaconda Environment before.
As you can see on the upper right side, the current selected Anaconda Environment is MyTestEnvPy3.8. We expect the Python version 3.8, and that numpy can be imported, as it was installed in this Anaconda Environment before.

Execute an existing jupyter notebook on an anaconda Virtual environment

I have an existing Jupyter notebook and I need to execute it an anaconda environment. I add the conda environment to jupyter notebook by executing the following commands:
conda install ipykernel
python -m ipykernel install --user --name myenv
Now, I can create a new jupyter notebook under the conda myenv.
The problem is how to execute an existing jupyter notebook with myenv? I can do it by copying the content to a new one but is there any other method?
We have just change the kernel under kernel->change kernel

How to change kernel when Jupyter notebook shows only one Python

I need to change the kernel to point it to miniconda version of Python, but Jupyter Notebook shows only one "Python 3" under Kernel-> Change Kernel.
Any idea how to get Jupyter notebook to show the additional one installed?
You can have a look at this and install the required kernel
https://ipython.readthedocs.io/en/latest/install/kernel_install.html
If you want to manually configure (add) Python 2.7 environment, try this:
conda create -n py27 python=2.7
conda activate py27
conda install notebook ipykernel
ipython kernel install --user

Resources