I am using a jupyter notebook, and inside this running jupyter notebook I want to use a virtualenv.
I was able to create and source(?) a virtualenv by using these commands
!virtualenv venv
!. venv/bin/activate
but it looks like python is still being used from the standard location
!which python
/opt/conda/bin/python
Is there any way to create and switch virtual environments from inside a running jupyter notebook?
After activating the venv
pip install ipykernel
ipython kernel install --user --name=venv
jupyter notebook
In jupyter Notebook you would be able to choose between venv and python x.
Related
I have installed jupyter notebook with pip through python and anaconda multiple times but I cannot find it in my start menu or any other location in my computer.
I have also tried using pip download and then pip install. It is successfull i cmd but I still cannot find it in my gui.
I think the best way to go about using Jupyter notebooks is to first create a conda environment that has jupyter installed. You can do this with the following line of code,
conda create -n my_env python jupyter
Optionally, you can even specify a python version as follows,
conda create -n my_env python=3.7 jupyter
Now, activate your environment,
conda activate my_env
Finally, launch Jupyter notebooks,
jupyter notebook
If you already have an environment created, just activate it, install jupyter and launch Jupyter notebooks,
conda activate my_env
pip install jupyter
jupyter notebook
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.
I would like to use it with Jupyter Lab, a virtual environment created using virtualenv in the Terminal of my computer.
When I want to use the environment in the Terminal, I run workon ami, ami is the name of the environment and it works fine. I have installed some modules such as matplotlib, which I can use inside this environment.
I have tried to use this environment in Jupyter Lab. I have run
python -m ipykernel install --user --name=ami
The name ami can be selected as a kernel in the Jupyter Lab session, but the matplotlib module is not installed. It seems the ami environment created is not the same as that used in the Terminal.
I have tried to install Jupyter Lab in the ami environment, but it does not have matplotlib installed.
I would like to use the environment in Jupyter Lab without installing the Python modules that I already installed.
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
I would like to execute a long running Python script from within a Jupyter notebook so that I can hack on the data structures generated mid-run.
The script has many dependencies and command line arguments and is executed with a specific virtualenv. Is it possible to interactively run a Python script inside a notebook from a specified virtualenv (different to that of the Jupyter installation)?
Here's what worked for me (non conda python):
(MacOS, brew version of python. if you are working with system python, you may (will) need prepend each command with sudo)
First activate virtualenv. If starting afresh then, e.g., you could use virtualenvwrapper:
$ pip install virtualenvwrapper
$ mkvirtualenv -p python2 py2env
$ workon py2env
# This will activate virtualenv
(py2env)$
# Then install jupyter within the active virtualenv
(py2env)$ pip install jupyter
# jupyter comes with ipykernel, but somehow you manage to get an error due to ipykernel, then for reference ipykernel package can be installed using:
(py2env)$ pip install ipykernel
Next, set up the kernel
(py2env)$ python -m ipykernel install --user --name py2env --display-name "Python2 (py2env)"
then start jupyter notebook (the venv need not be activated for this step)
(py2env)$ jupyter notebook
# or
#$ jupyter notebook
In the jupyter notebook dropdown menu: Kernel >> Change Kernel >> <list of kernels> you should see Python2 (py2env) kernel.
This also makes it easy to identify python version of kernel, and maintain either side by side.
Here is the link to detailed docs:
http://ipython.readthedocs.io/en/stable/install/kernel_install.html
A bit more simple solution to get notebook kernels available in other notebooks.
I'm using Linux + virtualenv + virtualenvwrapper. If you are using different setup, change some commands to the appropriate ones, but you should get the idea.
mkvirtualenv jupyter2
workon jupyter2
(jupyter2) pip install jupyter
(jupyter2) ipython kernel install --name "jupyter2_Python_2" --user
last command creates ~/.local/share/jupyter/kernels/jupyter2\ python\ 2/ directory
same stuff for 3
mkvirtualenv -p /usr/bin/python3 jupyter3
// this uses python3 as default python in virtualenv
workon jupyter3
(jupyter3) pip install jupyter
(jupyter3) ipython kernel install --name "jupyter3_Python_3" --user
When done you should see both kernels, no matter what env are you using to start jupyter.
You can delete links to kernels directly in ~/.local/share/jupyter/kernels/.
To specify location provide options to ipython kernel install (--help) or just copy directories from ~/.local/share/jupyter/kernels/ to ~/envs/jupyter3/share/jupyter if you want to run multiple kerenels from one notebook only.
I found this link to be very useful:
https://ocefpaf.github.io/python4oceanographers/blog/2014/09/01/ipython_kernel/
Make sure that you pip install jupyter into your virtualenv. In case the link goes away later, here's the gist:
You need to create a new kernel. You specify your kernel with a JSON file. Your kernels are usually located at ~/.ipython/kernels. Create a directory with the name of your virtualenv and create your kernel.json file in it. For instance, one of my paths looks like ~./ipython/kernels/datamanip/kernel.json
Here's what my kernel.json file looks like:
{
"display_name": "Data Manipulation (Python2)",
"language": "python",
"codemirror_mode": {
"version": 3,
"name":"ipython"
},
"argv": [
"/Users/ed/.virtualenvs/datamanip/bin/python",
"-c",
"from IPython.kernel.zmq.kernelapp import main; main()",
"-f",
"{connection_file}"
]
}
I am not certain exactly what the codemirror_mode object is doing, but it doesn't seem to do any harm.
It is really simple, based on the documentation
You can use a virtualenv for your IPython notebook. Follow the following steps, actually no need for step one, just make sure you activated your virtualenv via source ~/path-to-your-virtualenv/
Install the ipython kernel module into your virtualenv
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install ipykernel
(The most important step) 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 to 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.
#singer's solution didn't work for me. Here's what worked:
. /path/to/virtualenv/.venv/bin/activate
python -m ipykernel install --user --name .venv --display-name .venv
Reference: Kernels for different environments (official docs)
the nb_canda is useful:
conda install nb_conda
so,you can create and select your own python kernel with conda virtual environment,and manage the packages in venv
Screenshots
List item
conda environment manager Conda tab in jupyter notebook allows you to manage your environments right from within your notebook.
Change Kernel
You can also select which kernel to run a notebook in by using the Change kernel option in Kernel menu