I am not able to use virtual environments in JupyterHub version 3.0.14. I have no shell access to the server, but I can use the JupyterHub terminal.
step 1
After reading some documentation, I did this:
installed ipykernel in the base environment with: pip install --user ipykernel in the JupyterHub terminal.
created a new environment with python -m venv test-env
created a new environment with python -m venv test-env2
run python -m ipykernel install --user --name=test-env
run python -m ipykernel install --user --name=test-env2
After the previous sub-steps, I find the new environments in JupyterHub and I can open a new notebook using the menu "new"->"name of the environment".
step 2
In JupyterHub, I select "new" -> "test-env". In the new notebook, I try and import a new module, for example, hiplot, receiving an error because it is not installed.
To install hiplot in the test-env environment, I open a JupyterHub terminal, activate the environment with source [...]/bin/activate, then pip install hiplot, with success. If I run python from the command line and import hiplot I receive no errors.
step 3
I create a new notebook in the test-env as in step 2 ("new"->"test-env" from the menu), but I still cannot use hiplot, that results not installed. If I try and import it, JupyterHub tells me it is not installed.
QUESTION: After steps 2 and 3, I assume that test-env is not referring to the same environment when accessed from the command line or JupyterHub. Am I correct?
step 4
Inside of a notebook running within the test-env in JupyterHub, I run: `
import sys
!{sys.executable} -m pip install hiplot
After the previous command, I can import hiplot in the current notebook.
BUT
step 5
I open a new JupyterHub notebook in the base or test-env2 environments, where I thought hiplot was not be available. But I was wrong: the module hiplot is available in both of them.
It seems that, in step 4, hiplot has been installed in the base environment and is thus available in all the JupyterHub environments.
I tried to repeat the same steps changing step 1: instead of installing ipykernel in the base environment, I installed it in the test-env, with no success.
QUESTIONS: Is it really possible to use environments in JupyterHub? If it is, what am I doing wrong? I kept reading help pages, but I keep finding instructions with the same commands I used...
QUESTION how can I share a set of installed modules between the system python and the Jupyter notebooks?
The best way to achive what you want is by using conda.
Have a look to this video LInkAnaconda for Python where he explains quite well some differences when using pip install or conda install for your vens
Related
I have python 3.10 installed on my PC but every time I open a new Jupyter Notebook and check the version it still says that I am using the previous version that I had. Is there a way to make it start using the latest version every time I open a new notebook?
You should try to work with virtual environments. They are very commonly used in Python. Then, on your Jupyter Notebook, you will be able to choose the particular environment (and Python version) you want to run your notebook.
First, create a virtual environment using Python 3.10:
pip install virtualenv
virtualenv nameofthevenv --python python3.10
source nameofthevenv/bin/activate
Then, inside the virtualenv:
(nameofthevenv) $ pip install jupyter ipykernel
(nameofthevenv) $ python -m ipykernel install --user --name nameofthevenv
(nameofthevenv) $ jupyter notebook
Finally, go to the page in your browser the last command opened, and choose the running kernel at the top right: Kernel >> Change Kernel >> <list of kernels>. You will see nameofthevenv. Select it and you're good to go: the python version will be Python 3.10.
I'm not able to find a package from my notebook when I have installed it via pip / conda in my terminal.
For example, I did pip install trading-calendars and conda install trading-calendars in terminal but from trading_calendars import get_calendar in Jupyter notebook throws a ModuleNotFoundError with the message No module named 'trading_calendars'.
Is it possible that Jupyter takes time to refresh? If this keeps happening despite restarting Jupyter notebook, what should I do?
Duplicates:
How to list imported modules?
Package for listing version of packages used in a Jupyter notebook
Possible solution:
sys.path different in Jupyter and Python - how to import own modules in Jupyter?
This can be pretty confusing with Jupyter. It's very important to realize that your Jupyter client can connect to different "kernels" which equates to various python environments you might have installed. That is, you can start the Jupyter server with one python environment, and be executing your notebook's cells from another.
You need to make sure that you have the libraries installed to the environment that your kernel is using.
You will need to generate a kernelspec for your environment if you haven't already.
You can create a kernelspec using ipykernel. Here's an example of me doing it with conda.
$ conda activate test
$ conda install ipykernel
$ python -m ipykernel install --user --name test \
--display-name "Python (test)"
You can view your kernelspecs with this command
{~/path/to/project} (master *$)$ jupyter kernelspec list
Available kernels:
django_extensions /Users/nicholasbrady/Library/Jupyter/kernels/django_extensions
python3 /Users/nicholasbrady/anaconda3/share/jupyter/kernels/python3
python2 /usr/local/share/jupyter/kernels/python2
I have installed the azureml package and can see it in . . .anaconda\lib\site-packages:
If I run import azureml.dataprep as dprep in a python script in Spyder (launched from Anaconda Navigator), it works. But, if I open one of my anaconda environments with jupyter notebook and try running the same line of code, I get an error about module not found for azureml:
I thought perhaps the problem was that the package needed to be installed for that specific environment, but azureml is not available as a package for install via the anaconda environments > install packages interface (there is an azure package but not an azureml package).
So, I followed instructions to use conda prompt to install a package into a specific environment. Instructions I followed:
(those are from this link)
And here is the result of following the instructions (it looked like it installed the package into the env):
But, I got the exact same error when trying to import the package in the environment started as a jupyter notebook. Then, I closed anaconda navigator completely just in case, but that also didn't change the result.
Any ideas about what I'm either doing wrong or how I can manually install this package into a specific anaconda environment?
You did the right thing to install the package into the environment. Btw, pip is automatically installed by conda into any environment that has Python, so installing it shouldn't have been necessary.
Are you sure that the environment that you installed into is the one in which your notebook kernel is running? Start the notebook and execute
!conda env list
That will give you a list of environments, and an asterisk * next to the one that is active.
You can also call pip directly from a notebook cell:
!pip install azureml
That will install into the conda environment in which the kernel is running.
I am currently trying to work basic python - jupyter projects.
I am stuck on following error during matplotlib:
screenshot on jupyter-error
ModuleNotFoundError: No module named 'matplotlib'
I tried to update, reinstall matplotlib aswell in conda and in pip but it still not working.
happy over every constructive feedback
In a Notebook's cell type and execute the code:
import sys
!{sys.executable} -m pip install --user matplotlib
and reload the kernel
(src: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/ )
open terminal and change the directory to Scripts folder where python installed. Then type the following command and hit enter
pip install matplotlib
Hope this will solve the issue.
I was facing the exact issue. It turns out that it was using the system Python version despite me having activated my virtual environment.
This is what eventually worked.
If you are using a virtual environment which has a name say myvenv, first activate it using command:
source activate myvenv
Then install module ipykernel using the command:
pip install ipykernel
Finally run (change myvenv in code below to the name of your environment):
ipykernel install --user --name myvenv --display-name "Python (myvenv)"
Now restart the notebook and it should pick up the Python version on your virtual environment.
While #Frederic's top-voted solution is based on JakeVDP's blog post from 2017, it completely neglects the %pip magic command mentioned in the blog post. Since 2017, that has landed in mainline IPython and the easiest way to access the correct pip instance connected to your current IPython kernel and environment from within a Jupyter notebook is to do
%pip install matplotlib
Take a look at the list of currently available magic commands at IPython's docs.
generally speaking you should try to work within python virtual environments. and once you do that, you then need to tell JupyterLab about it. for example:
# create a virtual environment
# use the exact python you want to work with in this step
python3.9 -m venv myvenv
# 'activate' (or 'enter') it
source myvenv/bin/activate
# install the exact stuff you want to use in that environment
pip install matplotlib
# now tell JupyterLabs about the environment
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"
# start it up
jupyter notebook mynotebook
# if you now look under 'Kernel->Change kernel', your 'myenv' should be there
# select it (restart kernel etc if needed) and you should be good
The issue with me was that jupyter was taking python3 for me, you can always check the version of python jupyter is running on by looking on the top right corner (attached screenshot).
When I was doing pip install it was installing the dependencies for python 2.7 which is installed on mac by default.
It got solved by doing:
> pip3 install matplotlib
Having the same issue, installing matplotlib before to create the virtualenv solved it for me. Then I created the virtual environment and installed matplotlib on it before to start jupyter notebook.
in jupter notebook type
print(sys.executable)
this gave me the following
/Users/myusername/opt/anaconda3/bin/python
open terminal, go into the folder
/Users/myusername/opt/anaconda3/bin/
type the following:
python3 -m pip install matplotlib
restart jupyter notebook (mine is vs code mac ox)
If module installed an you are still getting this error, you might need to run specific jupyter:
python -m jupyter notebook
and this is also works
sudo jupyter notebook --allow-root
My problem is that I am running multiple python environments with different dependencies on a Windows 10 box. The python themselves are working fine, however when I run Jupyter it does not use the correct python environment.
I have several python environments setup there were installed using
conda env create -n python=3.5
I installed Jupyter using pip (because the conda install seemed broken at the moment)
pip install jupyter
I am able to switch between them using activate in the windows cmd
>activate env1
/c/Anaconda3/envs/env1/python
>which python
>activate env2
>which python
/c/Anaconda3/envs/env2/python
When I run each python binary and check the sys.path it pertains to the correct install.
import sys
sys.path
['', 'C:\\Anaconda3\\envs\\env1', 'C:\\Anaconda3\\envs\\env1\\python35.zip', 'C:\\Anaconda3\\envs\\env1\\DLLs', 'C:\\Anaconda3\\envs\\env1\\lib', 'C:\\Anaconda3\\envs\\env1\\lib\\site-packages', 'C:\\Anaconda3\\envs\\env1\\lib\\site-packages\\cycler-0.10.0-py3.5.egg']
I do not have PYTHONPATH set and the only python entries in my path (other than whatever activate changes) are to the root Anaconda.
>echo %PATH%
... C:\Anaconda3\Library\bin; C:\Anaconda3\Scripts; C:\Anaconda3 ...
Things seem to work well if I am just using python.
However whenever I try to run jupyter it seems to be using env2 regardless. In my Jupyter notebook I run the import sys; sys.path and it always seems to point to env2.
I have verified that I am using the correct Jupyter, and I have attempted running Jupyter by calling python.
>activate env1
>which jupyter
/c/Anaconda3/envs/env1/Scripts/jupyter
>activate env2
>which jupyter
/c/Anaconda3/envs/env2/Scripts/jupyter
jupyter notebook # This shows env2 in sys.path
python -m jupyter notebook # This shows env2 in sys.path extended by the below entries
'c:\\anaconda3\\envs\\env2\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\username\\.ipython'
I have also tried manually installing the correct python in Jupyter and it does not change anything.
ipython kernel install --user
I have a bunch of dependencies installed in my env1 that my Jupyter notebooks require and I don't want to mess with my env2.
Does anyone have any idea why Jupyter is not using the correct python environment? Is there some python configuration I am not aware of? Is this some odd Windows specific issue?