Jupyter and conda: error when cloning environment - python

I have created multiple conda environments in order to test compatibility of installed packages. I use conda create -n test02 --clone test01 to create environment test02 as a clone of test01. I activate test02, install new packages and start instance of python from which I import new packages with no problem. The problem arises when I launch jupyter notebook or qtconsole and try to import newly installed package and get import error: no module named 'xxx'. I do a sys.executable and see that jupyter is running python from the old environment (test01) I cloned the new one from. Why is this happening? Can I change it in config file somewhere and where might I find this file?
edit: more info
jupyter --paths for both environments share path entry for config and data in the same environment directory
(test01) PS C:\Users\Aka> jupyter --paths
config:
C:\Users\Aka\.jupyter
c:\users\aka\miniconda3\envs\test01\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\test01\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
(test01) PS C:\Users\Aka> conda activate test02
(test02) PS C:\Users\Aka> jupyter --paths
config:
C:\Users\Aka\.jupyter
c:\users\aka\miniconda3\envs\test01\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\test01\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
edit2: I forgot to mention that I installed Jupyter using pip.
to recreate the problem I did:
conda create -n env1
conda activate env1
pip install jupyter
jupyter --paths
config:
C:\Users\Aka.jupyter
c:\users\aka\miniconda3\envs\env1\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\env1\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
conda create --clone env1 -n env2
conda activate env2
jupyter --paths
config:
C:\Users\Aka.jupyter
c:\users\aka\miniconda3\envs\env1\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\env1\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
If I were to install jupyter with conda into a brand new environment and then clone the environment, jupyter behaves as expected. Note: I installed jupyter with pip because I am using python 3.5 because of other packages I need and installing jupyter with conda in my environments kept breaking it. I reached out to conda-forge for help but they said "sorry, we don't support python 3.5".

Jupyter only needs to be install in a single location - either a Conda env or at the system-level.
Jupyter installed in Conda env
To use other envs as kernels, one needs to install nb_conda_kernels in the env with Jupyter, and ipykernel in any env you wish to use as a kernel. Always launch jupyter notebook from the env with Jupyter and the others will be available automatically.
Jupyter outside Conda
If Jupyter is installed at a system level, one must manually register the Conda envs one wishes to use as kernels:
conda activate my_env
conda install ipykernel
python -m ipykernel install --user --name my_env_name
Then launch Jupyter from anywhere.

Related

Python New Env created by Anaconda Prompt not showing in Jupyter Notebook kernel list

I created a new python env using Anaconda Prompt by using the following steps:
conda create --name py3-TF2.0 python=3
conda activate py3-TF2.0
conda install tensorflow
pip install --upgrade tensorflow
pip install ipykernel
then i restarted anaconda and opened jupyter notebook.
The problem is when i open a new notebook and go to kernel to choose the new env i could not find the env i created (p3-TF2.0)
however, when I use conda info --envs, i find the new env created.
what is missing here?
thanks in advance
After activating the environment, try running:
python -m ipykernel install --user --name py3-TF2.0 --display-name "py3-TF2.0"
If this still isn't pointing to the correct environment (i.e. you can't import packages you know you installed in that environment), remove the py3-TF2.0 kernel that's broken with jupyter kernelspec remove py3-TF2.0.
Then run <your_path_to>/anaconda3/envs/py3-TF2.0/bin/python -m ipykernel install --user --name py3-TF2.0 --display-name "py3_TF2.0".
Additional help on Jupyter Kernels.

Conda install packages from jupyter notebook

I have used !{sys.executable} -m pip install to install packages i want to use in my jupyter notebooks.
Now i also want to install some packaged by conda and use in the same notebooks.
This is the command i run and the error message
They ask us to create a separate conda environment. I am confused about this.
Do we need to create a new conda enviroment everytime we need new packages?
What is the correct way to install and access conda packages on jupyter python notebook?
You shouldn't in principle install from the jupyter notebook but from the terminal/cmd.
You can create an enviroment >conda create --name myenv
And activate it: >conda activate myenv
now everything you install will be restricted to myenv. for example conda install numpy
To use the packages in myenv in jupyter simply open jupyter with your enviroment active:
>conda activate myenv
>jupyter notebook
Note that you have to install jupyter in your enviroment too: conda install jupyter

windows anaconda environment not working in jupyter notebook

i'm created a conda environment
conda create -n tfgpu tensorflow-gpu
conda activate tfgpu
but now after running
(tfgpu) > jupyter notebook
I can't import tensoflow as tf, notebook doesn't load this environment!
Why it's happen?
My problem is solved after:
https://stackoverflow.com/a/44786736/12370909
conda create -n tfgpu tensorflow-gpu
conda activate tfgpu
python -m ipykernel install --user --name tfgpu --display-name "Python (tfgpu)"
jupyter notebook
Then you could be able to create a new Notebook using your environment by selecting "Python (tfgpu)" from the drop-down list.

Jupyter Notebook's terminal command not using correct conda environment

I have 2 conda environments installed:
- env1: base environment where jupyter-notebook is installed and started from
- env2: project environment with ipykernel installed
I manually added kernelspecs for the 2 environments following this guide.
Everything works fine. sys.executable in 2 kernels show separate, correct paths. But for terminal commands (i.e. !which python), no matter which kernel I'm running in the environment defaults to env1.
Is there any way to have the notebook automatically change this to the kernel's environment?
P.S. I already tried installing nb_conda, nb_conda_kernels
install nb_conda and nb_conda_kernels into your base.
conda install nb_conda nb_conda_kernels -n env1
This should give you the ability to change kernel in jupyter, and use the env2 kernel.
I would install jupyter notebook in the base env (not env1, not env2)
Then install nb_conda_kernels in the base
in env1 and env2, install ipykernel
in env1 and env2, run this:
python -m ipykernel install --user --name env1 --display-name "env1 env"
Check this out for more info:
New Conda environment with latest Python Version for Jupyter Notebook

How to start jupyter in an environment created by conda?

I use conda created an environment called testEnv and activated it, after that I use the command jupyter notebook to call the jupyter editor. It works, but the problem is that, I can only create file in the root environment. How can I create file in testEnv environment?
Here are the steps what I have done:
$ conda create -n testEnv python=3.5 # create environmet
$ source activate testEnv # activate the environmet
(testEnv)$ jupyter notebook # start the jupyter notebook
Here are the result, which shows I can only create file with in "root" but not in "testEnv" (There is only Root, but no testEnv):
In the Tab Conda, I can see the testEnv, but how can I switch to it?
You have two options. You can install the Jupyter Notebook into each environment, and run the Notebook from that environment:
conda create -n testEnv python=3.5 notebook
source activate testEnv
jupyter notebook
or you need to install the IPython kernel from testEnv into the environment from which you want to run Jupyter Notebook. Instructions are here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments To summarize:
conda create -n testEnv python=3.5
source activate testEnv
python -m ipykernel install --user --name testEnv --display-name "Python (testEnv)"
source deactivate
jupyter notebook
The answer is that you probably shouldn't do this. Python virtualenvs and Conda environments are intended to determine the resources available to the Python system, which are completely independent of your working directory.
You can use the same environment to work on multiple projects, as long as they have the same dependencies. The minute you start tweaking the environment you begin messing with something that is normally automatically maintained.
So perhaps the real question you should ask yourself is "why do I think it's a good idea to store my notebooks inside the environment used to execute them."

Categories

Resources