Problem using jupyter notebook in virtual enviroment - python

I am following a guide on how to use a jupyter notebook in a virtual environment and am receiving unexpected errors that aren't addressed other stack overflow threads.
The following error occurs when trying to install the ipykernel to the virtual environment.
[Errno 13] Permission denied: '/usr/local/share'
This is the command I am entering (names edited out)
(sportsipy_env) (base) xxx#xxxs-MacBook- Environments % python -m ipykernel install --name=sportsipy_env
Both jupyter notebook and ipykernel were pip installed without issue. I am doing this task from the terminal in VScode if that matters.

Related

Conda error when using Jupyter notebook (DLL load failed while importing _device: The file cannot be accessed by the system.)

I have a project that requires using python =3.8.5, jupyter_client =6.1.12 and pywin32=300. I have python 3.10 installed on my computer. I decided to set up conda environment with Jupyter Notebook in miniconda. First,
I created a conda environment
conda create -n testenv python=3.8.5
then i activated the env
conda activate testenv
once in the testenv environment, i installed packages using pip
pip install jupyter jupyter_client==6.1.12 pywin32==300
last i type the command
jupyter notebook
However I received the following error message
File "C:\Users..\Miniconda3\envs\testenv\lib\site-packages\zmq\backend\cython_init_.py", line 6, in
from . import (
ImportError: DLL load failed while importing _device: The file cannot be accessed by the system.
Does anyone know what might be the issue? Thank you!

Trouble in jupyter notebook running by line (f10) with VS Code

Problem:
When I try running code by line (f10) with VS Code, "ipykernel setup required for this feature" message pops up.
What I've tried:
Using Anaconda Prompt: "conda install ipykernel", "conda install -c conda-forge ipykernel"
-When I tried this, the prompt said "conda-forge:: ipykernel-6.4.1-py38h595 --> pkgs/main:: ipykernel-6.2.0-py38haa95532_1"
Doesn't it mean I already install ipykernel v.6.20 or higher?
After I tried this I checked Jupyter --Version it said ipykernel 5.3.2
Other specs are qualified. (VS Code, Jupyter, Anaconda versions etc,)
Using Terminal (in VS Code): "pip install ipykernel"
Reinstalling all of add-ins such as Python for VS, Python, Anaconda, Jupyterlab, VS Code
Updating ExecutionPolicy: CurrentUser-RemoteSigned, LocalMachins-AllSigned, others Undefined.
+) Created virtual-env named envi which is conda env. In VS Code I selected this interpreter and also reloaded all windows.
As I understand, Conda install Ipkkernel > Reload is what pop-up msg required.
This is what I'm going through now.. I just got interested in Python and now I cannot doing anything..
I had the same problem. It seems there was a problem with the version of ipython or ipykernel since I managed to fix it by running the following commands in conda environment:
pip install --upgrade ipython
pip install --upgrade ipykernel
Don't know which of the two was the problem but I was able to use the jupyter notebook line-by-line feature after this.

WSL2 jupyter notebook cannot use kernel install in conda environment

I have anaconda with jupyter notebook installed in my WSL2
however, when I'm start the jupyter notebook, It not use my conda environment but using from window
In the "Change kernel" menu only Python3 is available
(env) user#DESKTOP:/mnt/c/Users/user$ ipython kernelspec list
Available kernels:
python3 /home/user/.local/share/jupyter/kernels/python3
pytorch /home/user/.local/share/jupyter/kernels/pytorch
rlml /home/user/.local/share/jupyter/kernels/rlml
I already tried
python -m ipykernel install --user --name env --display-name "name"
but still doesn't work.
Is there anyway to force jupyter notebook to use kernel in WSL ?
I also tried to use from VS code itself but I face another problem.
When try to change jupyter kernel to my conda environment it prompt dialog
but after click install, it can't find
-bash: C:/Users/user/anaconda3/envs/env/python.exe: No such file or directory
I have checked the directory and it have python.exe in there.

Can't install python packages for a conda environment

I could use some clarification regarding anaconda envs and the installed packages. I have just began using environments other than the base to keep my installations clean.
I just deleted my anaconda3 folder, installed it from scratch, made a new environment and tried to run a jupyter-notebook.
When reading excel via pandas I get the error Missing optional dependency 'xlrd'
prompting me to install xlrd via conda or pip.
So I open the terminal, activate the env I’m working in and install it using conda. I close the JN from the terminal, reopen it and run the same code. The problem persists.
I repeat the procedure, this time indicating the name of the env at the end. Problem persists.
I checked which python in the terminal, it is indeed the anaconda3/envs/newenv/bin/python
I do the same (in the new env) using pip. I use the command .../anaconda3/envs/newenv/bin/python -m pip install xlrd. I get the message Requirement already satisfied: xlrd in ./anaconda3/envs/newenv/lib/python3.7/site-packages (1.2.0).
I even tried installing the package inside the notebook using !conda install xlrd, still I get the same error.
Finally, I open Jupyter via the base env, and the package works there perfectly.
I have no idea why I can’t install this under the newenv, which was the point of having local envs after all.
Thanks heaps for your help!
You need to install the kernel in Jupyter to be able to use it.
jupyter kernelspec list
That command will give you the list of kernels you have. I am assuming it only shows you Python3
You will now need to install a kernel. Remember to do this while inside your virtual environment
python3 -m pip install ipykernel
python3 -m ipykernel install --user --name <your-new-kernel-name>
You should now see this in Jupyter notebook. Select the kernel in Jupyter and you should be good to go.
Another thing you may want to try is to install Jupyter while inside your virtual environment. While inside your virtual environment, you could do:
python3 -m pip install jupyter lab
and then while still inside your virtual environment, run jupyter after checking which jupyter. It should solve your problem as well.

Running Jupyter notebook in a virtualenv: installed sklearn module not available

I have installed a created a virtualenv machinelearn and installed a few python modules (pandas, scipy and sklearn) in that environment.
When I run jupyter notebook, I can import pandas and scipy in my notebooks - however, when I try to import sklearn, I get the following error message:
import sklearn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-8fd979e02004> in <module>()
----> 1 import sklearn
ImportError: No module named 'sklearn'
I am able to import all modules, at the command line - so I know they have been successfully installed:
(machinelearn) me#yourbox:~/path/to/machinelearn$ python -c "import pandas, scipy, sklearn"
(machinelearn) me#yourbox:~/path/to/machinelearn$
How can I import sklearn in my jupyter notebook running in a virtualenv?
You probably have not installed jupyter / IPython in your virtualenv. Try the following:
python -c "import IPython"
and check that the jupyter command found in your $PATH is the one from the bin folder of your venv:
which jupyter
For windows users in a powershell console, you can use the following to check that the jupyter command in your $env:Path is the one from the Scripts folder of you venv:
get-command jupyter
Edit: if this is the problem, just run python -m pip install jupyter in your venv.
Edit 2: actually you might also need:
python -m ipykernel install --user --name=my-virtualenv-name
and then switch the kernel named "my-virtualenv-name" in the jupyter user interface.
Edit 3: maybe the --user flag in the last command is a bad idea:
python -m ipykernel install --name=my-virtualenv-name
Another approach to take is to have one global jupyter installation, but to point to different kernels to run as the backend.
That approach is outlined here in their docs:
http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
Copying below in case the link breaks:
You can use a virtualenv for your IPython notebook. Follow the following steps:
Install the ipython kernel module into your virtualenv
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install ipykernel
Now run the kernel "self-install" script:
python -m ipykernel install --user --name=my-virtualenv-name
Replacing the --name parameter as appropriate.
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:
create a virtual environment
mkvirtualenv --no-site-packages --python=/your/python/path your_env_name
Activate the virtual environment
workon your_env_name
Install Jupyter and other packages
pip install jupyter, numpy
Add a new kernel to your Jupyter config
ipython kernel install --user --name=your_env_name
Done. You may now use Jupyter notebook under the virtual environment.
jupyter-notebook
Disclaimer: the question has been answered but is hidden in one of the replies. I googled and took sometime to find the right answer. So I just summarize it so someone having the same issue can easily follow.
Assuming that jupyter is installed on your machine, not on the virtual environtment.
Using a virtual environment with Jupyter notebook
VENV_NAME = "YOUR VIRTUAL ENV NAME"
1) virtualenv VENV_NAME
2) source venv/bin/activate
3) Add this package if not present: pip3 install ipykernel
4) Then execute this command: ipython kernel install --user --name=VENV_NAME
5) Now open up the Jupyter Notebook and in change kernel select VENV_NAME
6) To install a new package perform pip3 install <PACKAGE NAME> in your terminal and repeat step 4.
Hope it helps!
Solution without adding a new kernel globally!!
create a new virtual environment by
python3 -m virtualenv envname
Activate your enviroment and install jupyter in it by
pip install jupyter
One thing you have to make sure before installing jupyter is that you don't have following packages already installed in it.
ipykernel
ipython
ipython-genutils
ipywidgets
jupyter
jupyter-client
jupyter-console
jupyter-core
If you've previously installed them then first uninstall them by pip uninstall.
Install your desired packages in activated virtualenv and launch jupyter in it and voila!
Creation of virtualenv with python3 -m venv command
I had the same problem as yours.
In my case I had created the virtualenv with the command
python3 -m venv ./my_virtual_env --system-site-packages
The problem was I could not install jupyter inside the virtual environment as it was already in the system-site-package (when you try to install it, it tells you "Requirement already satisfied").
To install jupyter, (and in a first instance pip, that does not get installed neither in your virtual environment with this command) but still have access to system-site-package you can run :
python3 -m venv ./my_virtual_env
Activate you virtual environment, run pip3 install jupyter (and pip3 install pip) and then turn on the option include-system-site-packages in the file ./my_virtual_env/pyvenv.cfg.
After deactivation and reactivation of you environment, you will have access to system site-packages.
Creation of virtualenv with virtualenv command
Given this answer you can prevent the access to system site-packages by creating a file ./my_virtual_env/lib/python3.4/no-global-site-packages.txt,
and get the access back by removing it.
You can still install jupyter inside your virtual-environment if you have created your virtual env using:
python -m venv --system-site-packages path/to/my-venv
Simply do this:
activate-your-env
pip install -I jupyter
And you are now ready to go
jupyter notebook

Categories