add existing anaconda to jupyter notebook mac - ipython

I've installed anaconda2 and anaconda3 from official website
and now have 2 folders:
user/anaconda with anaconda2
user/anaconda3/anaconda with anacaonda3
the default path for python is set for anaconda 2:
MacBook-Air:anaconda zelenov$ which python
/Users/zelenov/anaconda/bin/python
but when I run jupyter notebook I can create only python 3 notebook
how can I add my python2 environment?
I tried this, but it didn't help:
python2 -m pip install ipykernel
python2 -m ipykernel install --user
Thanks in advance for any help!

Choose one Anaconda distribution, not both. I suggest uninstalling one of those distributions, e.g. Python 2. Then create an environment within your Python 3 distribution:
> conda create --name py27 python=2.7
Once the environment is created, you need to enter it, install ipykernel, then exit the environment once completed.
> source activate py27
> conda install ipykernel
> source deactivate
Note source can be omitted for Windows users. As of Anaconda 4.2, installing ipykernel is sufficient for creating a jupyter kernel automatically. Start jupyter:
> jupyter notebook
You should see a dropdown menu in the dashboard with the name of the py27 kernel installed. For more information on installing Python kernels, see Jupyter documentation.

Related

Not able to install feature -Engine Module

I am trying to install feature-engine module on anaconda
this is the error i am getting
Package is not available from current channels
repo.anaconda win 64 , noarch etc.
Can you please help me with the problem?
Thanks,
RD
to install from anaconda:
conda install -c conda-forge feature_engine
I believe that feature-engine is not available through anaconda channels for installation with conda install. I was able to install it via pip. Here is how I did it (in Windows):
open a CMD and run conda activate <<VIRTUALENV>>. This is the environment you create for your project. If you have not created one, then use base, the default one.
cd to the location of your pip installation within that activated conda Virtual environment (mine was within my user folder in \AppData\Local\Continuum\anaconda3\envs\<<VIRTUALENV>>\Scripts).
in there, run pip install feature-engine
you should now be able to see it listed under pip freeze or pip list, but not under conda list.
Finally, go to your code location and run the code. remember to activate that same <> each time you open a new CMD to run it.
Hope it helps.
If you are using Jupyter Notebooks, it might be the case that your Jupyter Notebook is not actually running the kernel in your (activated!) Anaconda environment (via this answer), but the generic Python3 kernel that only can import packages from your global Anaconda environment.
You can check for this by importing a package that is installed in your global environment (e.g., pandas), while running a notebook:
import pandas
pandas.__file__
If you see something likes this (on Windows), you are indeed running the wrong kernel (as you would expect the packages to be loaded from the activated environments):
'C:\\Users\\<user>\\Anaconda3\\lib\\site-packages\\pandas\\__init__.py'
Therefore, in your Anaconda Prompt, you have to create a new kernel within ipykernel (assuming cenv is your environment of interest):
$ conda activate cenv # . ./cenv/bin/activate in case of virtualenv
(cenv)$ conda install ipykernel
(cenv)$ ipython kernel install --user --name=<any_name_for_kernel>
(cenv)$ jupyter notebook
Now, in the restarted Jupyter Notebook you can change the kernel via the menu: Kernel > Change kernel > <any_name_for_kernel>
Importing the same package, like pandas, should show the following file path:
'C:\\Users\\<user>\\Anaconda3\\envs\\<cenv>\\lib\\site-packages\\pandas\\__init__.py'
and you should be able to import any package installed in that Anaconda environment.

Install conda package from Google Datalab

I'm looking to use the ospc taxcalc package in a Google Datalab notebook. This package must be installed via conda.
Datalab doesn't have conda by default, so this method (from https://stackoverflow.com/a/33176085/1840471) fails:
%%bash
conda install -c ospc taxcalc
Installing via pip also doesn't work:
%%bash
pip install conda
conda install -c ospc taxcalc
ERROR: The install method you used for conda--probably either pip install conda or easy_install conda--is not compatible with using conda as an application. If your intention is to install conda as a standalone application, currently supported install methods include the Anaconda installer and the miniconda installer. You can download the miniconda installer from https://conda.io/miniconda.html.
Following that URL, I tried this:
%%bash
wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh
bash Miniconda2-latest-Linux-x86_64.sh
wget works, but the bash command to install just kept in "Running..." state for seeming perpetuity.
This seems to be due to the conda installer prompting for several Enter keystrokes to review the license, and then for a yes indicating acceptance of the license terms. So conda's silent mode installation looked promising:
%%bash
bash Miniconda2-latest-Linux-x86_64.sh -u -b -p $HOME/miniconda
This produced the following warning:
WARNING: You currently have a PYTHONPATH environment variable set. This may cause unexpected behavior when running the Python interpreter in Miniconda2. For best results, please verify that your PYTHONPATH only points to directories of packages that are compatible with the Python interpreter in Miniconda2: /content/miniconda
And doesn't make available the conda command:
%%bash
conda install -c ospc taxcalc
bash: line 1: conda: command not found
There is a pending github issue tracking this work - https://github.com/googledatalab/datalab/issues/1376
I believe we will need to install conda and use that for python, pip and all other python packages, and in the interim it may not be possible to mix the two python environments. However someone with more experience with conda might know otherwise.
As of the 2018-02-21 release, Datalab supports Conda and kernels are each in their own Conda environment.

jupyter executables not accessible after pip install on ubuntu

I have installed jupyter - but to double check ran the install again:
pip install jupyter
..
Requirement already satisfied (use --upgrade to upgrade): jupyter in ./.local/lib/python2.7/site-packages
However there is no jupyter (or jupyter-notebook etc.) on the $PATH or anywhere under /usr
find /usr -name jupyter
Where is it installed? I am on ubuntu 16.0.4
Maybe I'm wrong but is your
PYTHONPATH
correct?
Because it should be installed there :
/usr/local/lib/python2.7/dist-packages
or
/usr/lib/python2.7/dist-packages
And if really you struggle with system based python and Jupyter, I think you should install Anaconda instead.
It's the most used, production ready, compartimented python environment.
And it has jupyter built-in.
To finish my argumentation, it's the recommanded way to install Jupyter.

After pip install jupyter within a virtualenv, the Python 3 kernel is hardcoded for that virtualenv's python. How to fix?

I switch to any other virtualenv and do a "pip install jupyter", when I run the notebook the Python 3 kernel is hardcoded to point to the python of the initial virtualenv that jupyter was installed in.
Is there a way to fix this?

In which conda environment is Jupyter executing?

I have jupyter/anaconda/python3.5.
How can I know which conda environment is my jupyter notebook running on?
How can I launch jupyter from a new conda environment?
As mentioned in the comments, conda support for jupyter notebooks is needed to switch kernels. Seems like this support is now available through conda itself (rather than relying on pip).
http://docs.continuum.io/anaconda/user-guide/tasks/use-jupyter-notebook-extensions/
conda install nb_conda
which brings three other handy extensions in addition to Notebook Conda Kernels.
Question 1: Find the current notebook's conda environment
Open the notebook in Jupyter Notebooks and look in the upper right corner of the screen.
It should say, for example, "Python [env_name]" if the language is Python and it's using an environment called env_name.
Question 2: Start Jupyter Notebook from within a different conda environment
Activate a conda environment in your terminal using source activate <environment name> before you run jupyter notebook. This sets the default environment for Jupyter Notebooks. Otherwise, the [Root] environment is the default.
You can also create new environments from within Jupyter Notebook (home screen, Conda tab, and then click the plus sign).
And you can create a notebook in any environment you want. Select the "Files" tab on the home screen and click the "New" dropdown menu, and in that menu select a Python environment from the list.
which environment is jupyter executing:
import sys
print(sys.executable)
create kernel for jupyter notebook
source activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
source activate other-env
python -m ipykernel install --user --name other-env --display-name "Python (other-env)"
http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernel-install
If the above ans doesn't work then try running conda install ipykernel in new env and then run jupyter notebook from any env, you will be able to see or switch between those kernels.
to show which conda env a notebook is using just type in a cell:
!conda info
if you have grep, a more direct way:
!conda info | grep 'active env'
You can also switch environments in Anaconda Navigator, install Jupiter and run it.
Because none of the answers above worked for me, I write here the solution that finally solved my problem on Ubuntu. My problem was:
I did the following steps:
Activate my environment: conda activate MyEnv
Start jupyter notebook:jupyter notebook
Although MyEnv was active in the terminal and had an asterix when writing conda env list, but jupyter notebook was started with the base environment.
Installing nb_conda and ipykernel didn't solve the problem for me either. Additionally, the conda tab wasn't appearing in jupyter notebook and also clicking on the kernels or going to the menu Kernel->Change Kernel didn't show the kernel MyEnv.
Solution was: install the jupyter_environment_kernel in MyEnv environment:
pip install environment_kernels
After that when starting jupyter notebook, it is started with the right environment. You can also switch between environments without stopping the kernel, by going to the menu Kernel->Change Kernel and selecting the desired kernel.
Question 1: How can I know which conda environment is my jupyter notebook running on?
Launch your Anaconda Prompt and run the command conda env list to list all the available conda environments.
You can clearly see that I've two different conda environments installed on my PC, with my currently active environment being root(Python 2.7), indicated by the asterisk(*) symbol ahead of the path.
Question 2: How can I launch jupyter from a new conda environment?
Now, to launch the desired conda environment, simply run activate <environment name>. In this case, activate py36
For more info, check out this link and this previous Stack Overflow question..
The following commands will add the env in the jupyter notebook directly.
conda create --name test_env
conda activate test_env
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=test_env
Now It should say, "Python [test_env]" if the language is Python and it's using an environment called test_env.
To check on which environment your notebook is running type the following commands in the notebook shell
import sys
print(sys.executable)
To launch the notebook in a new environment deactivate that environment first. Create a conda environment and then install the ipykernel. Activate that environment. Install jupyter on that environment.
conda create --name {envname}
conda install ipykernel --name {envname}
python -m ipykernel install --prefix=C:/anaconda/envs/{envname} --name {envname}
activate envname
pip install jupyter
In your case path "C:/anaconda/envs/{envname}" could be different, check accordingly.
After following all steps, launch notebook and do step 1
run the following in shell.
sys.executable
This should show: Anaconda/envs/envname
On Ubuntu 20.04, none of the suggestions above worked.
I.e. I activated an existing environment. I discovered (using sys.executable and sys.path) that my jupyter notebook kernel was running the DEFAULT Anaconda python, and NOT the python I had installed in my activated environment. The consequence of this was that my notebook was unable to import packages that I had installed into this particular Anaconda environment.
Following instructions above (and a slew of other URLs), I installed ipykernel, nb_conda, and nb_conda_kernels, and ran: python -m ipykernel --user --name myenv.
Using the Kernels|Change Kernel... menu in my Jupyter notebook, I selected myenv, the one I had specified in my python -m ipykernel command.
However, sys.executable showed that this did not "stick".
I tried shutting down and restarting, but nothing resulted in my getting the environment I had selected.
Finally, I simply edited file kernel.json in folder:
~/.local/share/jupyter/kernels/myenv
Sure enough, despite my having performed all the steps suggested above, the first argument in this JSON file was still showing the default python location:
$Anaconda/bin/python (where $Anaconda is the location where I installed anaconda)
I edited file kernel.json with a text editor so that this was changed to:
$Anaconda/envs/myenv/bin/python
Hopefully, my use of myenv is understood to mean that you should replace this with the name of YOUR environment.
Having edited this file, my Jupyter notebooks started working properly - namely, they used the python specified for my activated environment, and I was able to import packages that were installed in this environment, but not the base Anaconda environment.
Clearly, something is messed up in how the set of packages ipykernel, nb_conda, and nb_conda_kernels are configuring Anaconda environments for jupyter.
I have tried every method mentioned above and nothing worked, except installing jupyter in the new environment.
to activate the new environment
conda activate new_env
replace 'new_env' with your environment name.
next install jupyter
'pip install jupyter'
you can also install jupyter by going to anaconda navigator and selecting the right environment, and installing jupyter notebook from Home tab
Adding to the above answers, you can also use
!which python
Type this in a cell and this will show the path of the environment. I'm not sure of the reason, but in my installation, there is no segregation of environments in the notebook, but on activating the environment and launching jupyter notebook, the path used is the python installed in the environment.
For windows 10,
Go into Anaconda Launcher
In the 'Applications on' dropdown menu, select the required conda environment.
Install Jupyter notebook in the Anaconda Launcher
Launch Jupyter notebook from the Anaconda Launcher
The Conda tab is visible in the Jupyter notebook where you can see your active conda env.
For checking on Which Python your Jupyter Notebook is running try executig this code.
from platform import python_version
print(python_version())
In order to run jupyter notebook from your environment
activate MYenv
and install jupyter notebook using command
pip install jupyter notebook
then just
jupyter notebook
What solved the issue for me was that I had to run the following command:
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
The issue was that I opened a jupyter notebook made with/for a kernel for another conda python environment. That was visible from the output on the terminal; it is handy op run jupyter notebook from a terminal on the conda environment so that you can easily see what error messages are shown. Then it became clear that the notebook tried to run python from another environment.
Inspecting the folder/files:
C:\Users\<username>\AppData\Roaming\jupyter\kernels\<env name>\kernel.json
"argv": [
"D:\\Users\\<username..path>\\envs\\<env name>\\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python (env name)",
"language": "python",
"metadata": {
"debugger": true
}
}
So you can check and correct if necessary.
Second, when using anaconda/conda, make sure you have a healthy channel policy and execute the following commands when creating a new environment:
conda config --add channels conda-forge
conda config --set channel_priority strict
It solved this problem for me, I hope it helps you too.