check which conda env jupyter console is using - python

I have a conda environment and I would like to run a jupyter console within that environment. I do the usual source activate myenv and then jupyter console. The source activate myenv works since which python points to the right path. However, it doesn't seem like the jupyter console is picking up the right env. I have done this:
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
I have nb_conda and nb_conda_kernels installed. What's going on with my setup? I have Ipython 6.4.0, Python 3.6.5. Also, coming from an R background, I find it weird that I'm spending so much time on the basic setup where in R things just work. Is there something I'm missing or doing wrong? How can i check which env Ipython is running in?

sys.executable does indeed give you that info. This is how one should actually start the correct kernel (provided by How to start an ipython shell(not notebook) within a conda or virtualenv):
source activate myenv
python -m ipykernel install --user --name myenv --display-name "myenv"
jupyter console --kernel myenv
To get a list of the kernels that can be used:
jupyter kernelspec list

import sys
print(sys.executable)
Your conda environment is just a unique interpreter executable with its own PATH, etc. You could then regex on the string of its location to get the environment name.

Related

Anaconda environment with a python specific [duplicate]

I have miniconda3 installed and since I would like to have an environment with python version 3.3.0, I create it via
conda create -n "myenv" python=3.3.0
However when I activate the environment via
conda activate myenv
python has version 2.7.15 and path
/usr/bin/python
and ipython has python version 3.6.8 and path
/home/myname/.local/bin/ipython
I can access the correct python with python3 which is at
/home/myname/miniconda3/envs/myenv/bin/python3
however, ipython3 has python version 3.6.8 again.
conda install python=3.3.0
left the situation unchanged.
A solution would be to open IPython via
python3 -m IPython
however, while this works fine for python here I get the error message
/home/myname/miniconda3/envs/myenv/bin/python3: No module named IPython
Is it possible to access with the commands python and ipython both python version 3.3.0 in that specific environment, i.e. not by setting an alias in the .bashrc?
EDIT:
Turns out that this problem does not occur if you select version 3.3 instead of 3.3.0 together with #ilmarinen's answer
conda create -n "myenv" python=3.3 ipython
everything works fine and python as well as ipython result to version python 3.3.5.
You need to install ipython as well into your given environment
conda create -n "myenv" python=3.3.0 ipython
The conda environments are prepended to your PATH variable, so when you are trying to run the executable "ipython", Linux will not find "ipython" in your activated environment (since it doesn't exist there), but it will continue searching for it, and eventually find it wherever you have it installed.
To create an environment named py33 with python 3.3.0, using the channel conda-forge and a list of packages:
conda create -y --name py33 python==3.3.0
conda install -f -y -q --name py33 -c conda-forge --file requirements.txt
conda activate py33
...
conda deactivate
Alternatively you can use
conda env create -f environment.yml
for using an environment.yml file instead of requirements.txt:
name: py33
channels:
- conda-forge
dependencies:
- python==3.3.0
- ipython
Use this command to remove the environment:
conda env remove -n py33
I had similar issue. And I could't find many useful discussions.
The problem for me was I have alias pointing python to miniconda python hardcoded in my shell config file when I execute conda init zsh. Somehow the init process copies the alias and always reload that, thus overwrites the "correct" version.
After conda create -n py27 python=2.7 (my system default is 3.6), the version was correctly installed at miniconda3/envs/py27/bin/python. But the activated evironment python was not pointing to it, as indicated by which python,even if I deleted updated my shell config.
In the end it was solved by 'reverse' conda init (remove the generated conda function in .zshrc), remove alias, and re-init.
I guess other shell is using the same mechanism.

Accessing package in python virtual environment

I have a virtual environment in which I have installed 'geopandas'. When I do a pip list it lists this package as well. But when I try to call it in my jupyter notebook via 'import geopandas' I get a ModuleNotFoundError. Please help - I'm using windows machine
You can verify that your notebook is running in the correct virtual environment by doing:
import sys
sys.version
Here's how to run a Jupyter notebook in a virtualenv.
You should check if your notebook is using the correct kernel (the correct virtualenv). If you are still in the kernel using your standard environment and geopandas is not installed, it is possible you get this error.
So check if you are working in the correct kernel:
kernel check
You can install a kernel in jupyter notebook by activating the venv and then installing it:
source activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
More info about this install here.

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 add python 3.6 kernel alongside 3.5 on jupyter

I am trying to test some code on Python 3.6 but my system default python version is python 3.5 I have installed python 3.6 and have tried to follow the jupyter documentation to install a new python kernel
python3.6 -m pip install ipykernel
python3.6 -m ipykernel install --user
But it didn't work since it continues to show a single kernel in the menu: Python3
Has anyone managed to have both 3.5 and 3.6 in the same jupyter installation?
one option is to create a kernel that you can use in jupyter notebook.
you can do this inside the virtual environment:
Open up your terminal and enter the following line by line
virtualenv -p python3.6 py_36_env
source py_36_env/bin/activate
pip install ipykernel
python -m ipykernel install --user --name=py_36_env
jupyter notebook
Then in jupyter notebook you can select the 3.6 environment (py_36_env) from the 'New' drop down menu shown above or from the 'Kernel' drop down menu within a given jupyter notebook.
Steps to install Python 3.6 in Windows
Open command prompt
conda install ipykernel
conda create -n Python3.6Test python=3.6
Activate Python3.6Test
pip install ipykernel
python -m ipykernel install --name Python3.6Test
Here you go with 3.6
C:\ProgramData\jupyter\kernels\Python3.6Test
Now open Jupitor Notebook and see, you will get the Python3.6Test option
Following worked for me:
Commands are executed in Jupyter Notebook (OS: Ubuntu 16.04 LTS)
Upgrade pip:
!pip install --upgrade pip
Install virtual environment:
!pip install virtualenv
Select version of Python you want to use in new environment:
I wanted to create an environment with Python version 3.6 Naming it as Python_3_6:
!virtualenv -p python3.6 Python_3_6
After execution, this will create a folder with the same name in the current working directory (i.e. the location where Jupyter notebook is present)
Create a new option with the name of the created environment
And finally, run the following command:
!python -m ipykernel install --user --name=Python_3_6
This will create a new option with the name Python_3_6 in the menu from where we create a new notebook.
NOTE: One can run above commands from the terminal as well just don't use '!' before the commands.
This is an old question, but like some commenters here I wanted to change the default jupyter completely without using a venv. It seems jupyter keeps this in a json file called kernel.json:
{
"display_name": "Python 3",
"language": "python",
"argv": [
"/path/to/some/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
]
}
There may be several paths containing this file which are searched in order. To find out what they are on your machine, run:
ipython kernel install --prefix tmp
ipython will throw a warning that the config file is being created in tmp and might not be found, but it will also output the paths it uses to find kernel.json, which you can then edit manually.
You should create a virtualenv for each Python version you are using. Create one for Python 3.5:
virtualenv -p /usr/bin/python3.5 py35
source py35/bin/activate
pip install jupyter
jupyter # Jupyter running Python 3.5
deactivate # Leave virtualenv
And create one for Python 3.6:
virtualenv -p /usr/bin/python3.6 py36
source py36/bin/activate
pip install jupyter
jupyter # Jupyter running Python 3.6
upper solution does not work in my case!!
I want to execute code in python3.8 but unfortunately I previous install pytohn3.6
I create, activate virtualenv of python3.8 but during lib installation all package installed in virtualenv of python3.6
Solution is that I deleted python3.6 env & Setup a new way of Jupyter kernel. After that mine code execute happily & required lib install in virtualenv of python3.8
For Windows users:
To add python 3.x ipykernel to jupyter notebook with name "Python 3.x", shown as 'Python 3.x', open your terminal (such as Anaconda Prompt), in base env and enter the following line by line.
For Python 3.10 ipykernel:
conda create -n py310 python=3.10 ipykernel
conda activate py310
python -m ipykernel install --user --name=py310 --display-name "Python 3.10"
For Python 3.7.7 ipykernel:
conda create -n py377 python=3.7.7 ipykernel
conda activate py377
python -m ipykernel install --user --name=py377 --display-name "Python 3.7"
Then close the specific env and go back to the base env, and type in jupyter notebook. Once it opens you'll see the following when you click the "New" drop down menu in the top right corner:
Then to check if the correct Python is installed, open a new notebook and run:
from platform import python_version
print(python_version())
For Python 3.10:
For Python 3.7.7
To remove a specific kernel, you can run the following in your terminal in the base env:
jupyter kernelspec uninstall <kernel name>

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