I'm trying to use conda to set up one of my projects. I installed openCV by conda install -c conda-forge opencv
. When I run conda list
, I can see openCV in the list. Running python -i
and then import cv2
works, but when I open up Jupyter Notebook and navigate to that folder (I have to do it this way because running jupyter notebook
in the directory also pulls up an error), and open up a notebook which imports cv2, I get an error. Why is this happening, and how would I solve it? Any kind of help will be greatly appreciated.

- 143
- 1
- 2
- 12
-
Does this answer your question? [Conda environments not showing up in Jupyter Notebook](https://stackoverflow.com/questions/39604271/conda-environments-not-showing-up-in-jupyter-notebook) – merv Jun 24 '20 at 20:22
4 Answers
So as I said before, I wasn't able to start Jupyter Notebook from the command line, I had to start it from the start menu and navigate to my folder. Because of that, my notebook wasn't working in the conda environment that I created. I fixed that by running python -m ipykernal install --user --name <env_name> --display-name "<display_name>"
. I had to conda install ipykernel
. It works now. Thanks for the other answers.

- 143
- 1
- 2
- 12
Usually that indicates that the notebook is running with a different Python or in a different environment from Python in the command prompt. Check sys.executable to see which Python it's running in, and sys.path to see where it's looking for imports

- 76
- 2
-
I added the output of `sys.executable` and `path` to my question when I ran those from the command line using `python -i` after activating the environment and when I ran those from the notebook. I get different outputs. Might that be what's wrong? – Kadhir Jun 11 '20 at 17:28
Everybody says that pip installing from notebook is not the best practice, but maybe for a fast try it would do the thing:
# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} packagename
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install packagename
I used it from Install python packages on Jupyter Notebook and it worked for me.

- 473
- 4
- 13
Step 1: Activate environment before running
conda activate <environment-name>
Step 2: Install ipykernel
conda install -c anaconda ipykernel
Step 3: Add the conda environment to ipykernel
ipython kernel install --name <environment-name> --user
Step 4: Install your package
conda install -c conda-forge opencv
References:
How to add your Conda environment to your jupyter notebook in just 4 steps

- 2,028
- 1
- 20
- 18