I'm trying to run the anaconda distribution of python libraries in a Jupyter Notebook, but when I run the notebook I keep getting ImportErrors because the python path is set to the default distribution from Mac OS X 10.11
When I print out the sys.path and sys.executable, they differ when running python vs running jupyter notebook. For example,
from pprint import pprint as p
import sys
p(sys.path)
After doing this in python I get the correct output:
['',
'/Users/glennraskovich/anaconda2/lib/python27.zip',
'/Users/glennraskovich/anaconda2/lib/python2.7',
'/Users/glennraskovich/anaconda2/lib/python2.7/plat-darwin',
'/Users/glennraskovich/anaconda2/lib/python2.7/plat-mac',
'/Users/glennraskovich/anaconda2/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/glennraskovich/anaconda2/lib/python2.7/lib-tk',
'/Users/glennraskovich/anaconda2/lib/python2.7/lib-old',
'/Users/glennraskovich/anaconda2/lib/python2.7/lib-dynload',
'/Users/glennraskovich/anaconda2/lib/python2.7/site-packages',
'/Users/glennraskovich/anaconda2/lib/python2.7/site-packages/aeosa']
But when running this in jupyter notebook I get:
['',
'/usr/local/lib/python2.7/site-packages/dask-0.11.0-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/networkx-1.11-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/Pillow-3.3.1-py2.7-macosx-10.11-x86_64.egg',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions',
'/Users/glennraskovich/.ipython']
For the sys.executable,
p(sys.executable)
In python, correct output:
/Users/glennraskovich/anaconda2/bin/python
But in jupyter notebook, sys.executable is not set to the anaconda version
/usr/local/opt/python/bin/python2.7
I've tried setting PATH in my .bashrc and .bash_profile, and using the commands which python, which jupyter and such show anaconda paths but jupyter notebook is not using the anaconda paths. What could be the issue here?
I figured out the solution, since the kernel was set to use the default mac os x's python I fixed it by using the commands
python2 -m pip install ipykernel
python2 -m ipykernel install --user
For me, I installed Jupyter after creating an environment, but then was trying to run a module installed from the base env. I found by "jupyter kernelspec list" (https://github.com/jupyter/notebook/issues/2563), my kernel.json at C:\Users\username\Anaconda37\share\jupyter\kernels\python3\kernel.json was pointing to the python.exe in my working env. Changed the path and solved it.
This was an exhaustive description of python path setting.
I had this problem when I used Anaconda Navigator and the command line. I typed 'source activate ' into the console and then used Anaconda Navigator to open Jupyter. In Anaconda Navigator, however, I wasn't in the right environment which caused the problem. This is because Anaconda Navigator comes with its own activation for virtual environments (when you click on them). So you either need to activate the virtual environment from the console and then start Jupyter from the console or you need to activate the virtual environment in Anaconda Navigator and start Jupyter from the Navigator. Both ways work but not in combination. :-)
My experience:
I had a conda virtual environment in Windows, and I had linked it as a kernel in Jupyter Notebook. However, the sys.executable was pointing at the global Python installation executable.
Performing the steps in #Glenn's answer didn't help. To solve it:
Find the executable python.exe for your environment. Mine was inside C:\Users\youruser\miniconda3\envs\my_conda_environment\python.exe.
Find the kernel.json configuration file for your environment inside C:\Users\youruser\AppData\Roaming\jupyter\kernels\yourkernel\kernel.json
Modifiy the kernel.json configuration path, replacing the existing executable path by the one from step 1.
Restart jupyter and it's done!
Related
I have been using Spyder installed with with Anaconda distribution which uses Python 2.7 as default. Currently I need to set up a development virtual environment with Python 3.4.
Top two suggestions after research online are:
to set up virtual environment first and to point change the preferences of Spyder , e.g here;
to install all Spyder dependencies, like PyQt4, in the virtual environment itself, e. g. here ;
Both recommendations are cumbersome and do not look like smart options for development.
Is there a solution that would allow to run Spyder with required Python version automatically after activating the required virtual environment?
Here is a quick way to do it in 2021 using the Anaconda Navigator. This is the most reliable way to do it, unless you want to create environments programmatically which I don't think is the case for most users:
Open Anaconda Navigator.
Click on Environments > Create and give a name to your environment. Be sure to change Python/R Kernel version if needed.
Go "Home" and click on "Install" under the Spyder box.
Click "Launch/Run"
There are still a few minor bugs when setting up your environment, most of them should be solved by restarting the Navigator.
If you find a bug, please help us posting it in the Anaconda Issues bug-tracker too! If you run into trouble creating the environment or if the environment was not correctly created you can double check what got installed: Clicking the "Environments" opens a management window showing installed packages. Search and select Spyder-related packages and then click on "Apply" to install them.
There is an option to create virtual environments in Anaconda with required Python version.
conda create -n myenv python=3.4
To activate it :
source activate myenv # (in linux, you can use . as a shortcut for "source")
activate myenv # (in windows - note that you should be in your c:\anaconda2 directory)
UPDATE. I have tested it with Ubuntu 18.04. Now you have to install spyder additionally for the new environment with this command (after the activation of the environment with the command above):
conda install spyder
(I have also tested the installation with pip, but for Python 3.4 or older versions, it breaks with the library dependencies error that requires manual installation.)
And now to run Spyder with Python 3.4 just type:
spyder
EDIT from a reader:
For a normal opening, use "Anaconda Prompt" > activate myenv > spyder (then the "Anaconda Prompt" must stay open, you cannot use it for other commands, and a force-close will shut down Spyder). This is of course faster than the long load of "Anaconda Navigator" > switch environment > launch Spyder (#adelriosantiago's answer).
What worked for me :
run spyder from the environment (after source activate)
go to Tools --> preferences --> python Interpreter and select the python file from the env you want to link to spyder
ex : /home/you/anaconda3/envs/your_env/bin/python
Worked on ubuntu 16, spyder3, python3.6.
Additional to tomaskazemekas's answer: you should install spyder in that virtual environment by:
conda install -n myenv spyder
(on Windows, for Linux or MacOS, you can search for similar commands)
To do without reinstalling spyder in all environments follow official reference here.
In summary (tested with conda):
Spyder should be installed in the base environment
From the system prompt:
Create an new environment. Note that depending on how you create it (conda, virtualenv) the environment folder will be located at different place on your system)
Activate the environment (e.g., conda activate [yourEnvName])
Install spyder-kernels inside the environment (e.g., conda install spyder-kernels)
Find and copy the path for the python executable inside the environment. Finding this path can be done using from the prompt this command python -c "import sys; print(sys.executable)"
Deactivate the environment (i.e., return to base conda deactivate)
run spyder (spyder3)
Finally in spyder Tool menu go to
Preferences > Python Interpreter > Use the following interpreter and paste the environment python executable path
Restart the ipython console
PS: in spyder you should see at the bottom something like this
Voila
I just had the same problem trying to get Spyder to run in Virtual Environment.
The solution is simple:
Activate your virtual environment.
Then pip install Spyder and its dependencies (PyQt5) in your virtual environment.
Then launch Spyder3 from your virtual environment CLI.
It works fine for me now.
The above answers are correct but I calling spyder within my virtualenv would still use my PATH to look up the version of spyder in my default anaconda env. I found this answer which gave the following workaround:
source activate my_env # activate your target env with spyder installed
conda info -e # look up the directory of your conda env
find /path/to/my/env -name spyder # search for the spyder executable in your env
/path/to/my/env/then/to/spyder # run that executable directly
I chose this over modifying PATH or adding a link to the executable at a higher priority in PATH since I felt this was less likely to break other programs. However, I did add an alias to the executable in ~/.bash_aliases.
From Spyder official page on Github:
The naive approach
To use Spyder with another environment, the simplest way is to just
install it directly into the environment from which you'd like to use
the packages in, and run it from there. This works with all Spyder
versions and should require no extra configuration once the IDE is
installed; however, it results in multiple installations to manage and
isn't as flexible or configurable as the alternative. Therefore, when
dealing with multiple environments, we recommend the modular
approach.
The modular approach
Starting with Spyder 3.3.1, you can install the modular
spyder-kernels package into any Python environment (conda
environment, virtualenv/venv, system Python, WinPython, etc) in
which you wish to work, and then change the Python interpreter used by
Spyder on its IPython consoles to point to the Python executable of
that environment.
This takes a small amount of preparation and configuration, but is
much "lighter" and quicker than a full Spyder installation into that
environment, avoids dependency conflicts, and opens up new workflow
possibilities.
To achieve this, follow these steps:
1- Activate the environment (e.g. myenv) in which you'd like to work (e.g. with conda activate myenv for conda, source myenv/bin/activate or workon myenv for virtualenv/venv, etc)
2- Install the spyder-kernels package there, with the command:
3- conda install spyder-kernels if using conda/Anaconda,
4- pip install spyder-kernels if using pip/virtualenv.
5- After installing via either method, run the following command inside the same environment:
python -c "import sys; print(sys.executable)"
and copy the path returned by that command (it should end in
python, pythonw, python.exe or pythonw.exe, depending on your
operating system).
6- Deactivate that environment, activate the one in which Spyder is installed (if you've installed it in its own environment) and start
Spyder as you normally would.
7- After Spyder has started, navigate to Preferences > Python Interpreter > Use the following interpreter and paste the path from
Step 3 into the text box.
8- Start a new IPython console. All packages installed in your myenv environment should be available there. If conda is used, the
name of the current environment and its Python version should be
displayed in Spyder's status bar, and hovering over it should display
the path of the selected interpreter.
On Windows:
You can create a shortcut executing
Anaconda3\pythonw.exe Anaconda3\cwp.py Anaconda3\envs\<your_env> Anaconda3\envs\<your env>\pythonw.exe Anaconda3\envs\<your_env>\Scripts\spyder-script.py
However, if you started spyder from your venv inside Anaconda shell, it creates this shortcut for you automatically in the Windows menu. The steps:
install spyder in your venv using the methods mentioned in the other answers here.
(in anaconda:) activate testenv; though in my case, this step was not needed.
Look up the windows menu "recently added" or just search for "spyder" in the windows menu, find spyder (testenv) and
[add that to taskbar] and / or
[look up the file source location] and copy that to your desktop, e.g. from C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit), where the spyder links for any of my environments can be found.
Now you can directly start spyder from a shortcut without the need to open anaconda prompt.
For me below worked:
Open Anaconda, setup new environment, then select the env and click on play icon as shown below:
Then click on Open Terminal, and type "spyder" in terminal, it will open the spyder with selected env.
Note: For me directly opening Sypder from Home page was always opening with base env.
I follow one of the advice above and indeed it works. In summary while you download Anaconda on Ubuntu using the advice given above can help you to 'create' environments. The default when you download Spyder in my case is: (base) smith#ubuntu ~$. After you create the environment, i.e. fenics and activate it with $ conda activate fenics the prompt change to (fenics) smith#ubuntu ~$. Then you launch Spyder from this prompt, i.e $ spyder and your system open the Spyder IDE, and you can write fenics code on it. Remember every time you open a terminal your system open the default prompt. You have to activate your environment where your package is and the prompt change to it i.e. (fenics).
Apologies first-hand if I've done some silly mistake in the below raised issue. I have been stuck on this since quite some time, trying to successfully install multiple Python versions (via separate virtual environments) and run Jupyter notebook with all three versions in Change kernel switch.
AIM:
Setup Anaconda with Python 3.5.6 as default and create two virtual environments with Python 2.7.13 and Python 3.7.3 version and to be able to switch between these three Python versions on a Jupyter Notebook on Windows.
Process Followed: What I did (and ended in mess):
I first successfully installed Anaconda3 with Python 3.5.6 as default (installed in C:\ProgramData\Anaconda3) and set the PATH variables. Jupyter Notebook was up and running with an ipython kernel running from 'base' (or root) from
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
and kernel.json file was also mapped to the correct python version.
.
Then I created my first virtual environment (Python_2.7.13_ENV):
(base) conda create --p C:\ProgramData\Anaconda3\envs\Python_2.7.13_ENV python=2.7.13
and installed jupyter on it
(base) activate Python_2.7.13_ENV
(Python_2.7.13_ENV) conda install notebook ipykernel
(Python_2.7.13_ENV) python -m ipykernel install --p C:\Users\username\AppData\Roaming\jupyter\kernels\ --name Python_2.7.13_ENV --display-name "python_2.7.13"
I used the prefix notation as the default installation syntax was installing it for the root user and I wanted it to install it only for a specific user.
And this worked like a charm. The updated jupyter kernelspec read:
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
python_2.7.13 C:\Users\username\AppData\Roaming\jupyter\kernels\python_2.7.13
and kernel.json file was also mapped to the correct python version ("C:\\ProgramData\\Anaconda3\\envs\\Python_2.7.13\\python.exe")
This was also working fine. I could open a file in jupyter and succesfully switch between the two kernels.
.
Than I followed the same steps for creating my second virtual environment (Python_3.7.3_ENV):
Now, the updated kernelspec read:
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
python_2.7.13 C:\Users\username\AppData\Roaming\jupyter\kernels\python_2.7.13
python_3.7.3 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.7.3
and the kernel.json was also mapped to the correct python version.
Problem:
Both the virtual envs were created successfully.
Now when I run a jupyter notebook and try to switch to Python 2.7.13 kernel, it works fine, but shows a ImportError: DLL load failed (due to some import issue in zmq) on switching to Python_3.7.3 kernel.
However, when I first activate the Python_3.7.3_ENV virtual env and then load the jupyter notebook, I am able to switch between all three Python versions.
Can anybody provide a solution on how to toggle between all three versions without activating the virtual env beforehand if it's possible as I am able to do it with Py 2.7 & Py 3.5 versions.
PS. I have set the 'open with' default on right-click on a ipynb file to jupyter-notebook.exe.
I have similar setting: default python 3.7, env: python 3.6 and python 2.7.
set multiple versions of python/ipykernels running on jupyter notebook
Check you Anaconda version, if it is '>= 4.1.0', it is easier since after 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. If the Anaconda version is lower than 4.1.0 or just want to manually do that, you can reference here.
For python2.7 and adding the ipykernel to jupyter notebook, shown as 'Python (py27)'
conda create -n py27 python=2.7 ipykernel
conda activate py27
python -m ipykernel install --user --name=py27 --display-name "Python (py27)"
For python3.6 and adding the ipykernel to jupyter notebook, shown as 'Python (py36)'
conda create -n py36 python=3.6 ipykernel
conda activate py36
python -m ipykernel install --user --name=py36 --display-name "Python (py36)"
Then we can check if they works well not. You can deactivate from specific env and go back to the base env, and type jupyter notebook (or use graphic shortcut for jupyter notebook anaconda provides)and then you can create 'new' notebook, you can find apart from default Python 3, there also shows Python (py27) and Python (py36).
I was making mistake at the beginning to check which python was running:
Do not just use !python --version in jupyter notebook no matter which version kernel you are using.Because this is just like you running command in base env, it will always show the default env python version, which in my case is python 3.7.x.
What you can do to:
from platform import python_version
print(python_version())
## or this one, which ever you like.
import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
You will get right python version accordingly.
Show list of jupyter notebook kernels
jupyter kernelspec list (in base env, it will show all kernels name)
If you want to remove specific kernel, you can use:
jupyter kernelspec uninstall <kernel name>
Another way to confirm the python version is right, you can check the ipykernel json file, if the path to start the python is right or not.
To do this: use jupyter kernelspec list, so you can get to know the path of json file. (for example, in my case I can get the path for py27: C:\Users\username\AppData\Roaming\jupyter\kernels\py27). Then, you can go to the dir/path, and you can check what is shown in kernel.json
for the argv, it should show the path to access related python version. For example,
for py27, it will show like "C:\\Users\\username\\Anaconda3\\envs\\py27\\python.exe",
for py36, it will show like "C:\\Users\\username\\Anaconda3\\envs\\py36\\python.exe"
Last thing about setting PATH env variables: do not add Anaconda to the Windows PATH because this can interfere with other software. Instead, open Anaconda with the Start Menu and select Anaconda Prompt, or use Anaconda Navigator. More information can check the Anaconda official docs.
Hope this may be helpful if you meet similar issues.
PS: I was always using Unix like system, not so used to Windows env setting. That is what I met for setting different kernels for different anaconda envs and summarized it. Hope it helps.
After here and there, the only to make this work is having that virtual environment activated.
Following the above steps, I have installed Python 2.7.13, 3.5.6 and 3.7.3 version. My default python is Python 3.5.6 while the other two versions are installed in two virtual environments - Python_2.7.13_ENV and Python_3.7.3_ENV respectively.
How to use them ?
For Python 2.7 and 3.5, just use the Jupyter notebooks normally as you would. Since the default python is set to Python 3.5.6 there is no problem in switching between two versions using Change Kernel option in Jupyter Notebook Toolbar.
For Python 3.7 we first need to activate Python_3.7.3_ENV virtual environemnt and then we can switch between all three versions succesfully using Change Kernel option in Jupyter Notebook.
I have got two anaconda environments. The base one and my testing environment called testenv. This environment works without an issue for example with the PyCharm IDE.
When starting a jupyter server straight from the pictured directory by issuing jupyter notebook my scripts/notebooks complains about missing packages.
This tells me it is using base environment instead of my testenv environment. But as the screenshot show, its not the one thats activated.
So the question is: How can I set the virtual environment used by jupyter?
Used versions:
Windows 10 Pro x64
Jupyter Notebook 5.7.4
Python 3.7.1
Conda 4.5.12
[Edit] It turned out that jupyter was not even installed in my environment. I was assuming to be able to use the installation coming from base. After installing jupyter within my environment everything was working as expected.
This is usually foolproof for me. However, it does not allow switching virutal env on the fly. Conda docs.
Windows
$ activate testenv
$ jupyter notebook
Linux
$ source activate testenv
$ jupyter notebook
If you'd like to be able to switch between your envs even on the fly from within Jupyter, this should also do the trick.
How do I add python3 kernel to jupyter (IPython)
Importing rpy2.robjects fails in the jupyter notebook with LookupError: 'show' not found, however, import works in the commandline python interpreter with activated environment. How can I make it work in the notebook?
I have created the conda environment with rpy2 and ipykernel installed and have linked it to jupyter via python -m ipykernel install --user --name env --display-name "env" (within the activated environment). I looked up the path of the environment in kernel.json under the path provided by jupyter kernelspec list and found it to be the correct one of my environment. I do not get the error when I start jupyter notebook from within the activated environment and try to import rpy2.rojects. I would have thought that this should not make any difference. Furthermore, when starting the python interpreter via ./python within the ~home/user/anaconda3/envs/env, I do obtain the error. It seems some environment variables are set differently when starting the notebook with or without activated environment, but I do not have any clue which and how. I'll be happy about any hints to understand the issue.
It does make a difference whether an environment is activated. You've already pointed out one solution, start Jupyter in the activated environment, and the kernels will inherit the corresponding environment variables.
Anaconda also has a kernel provider that finds kernels in all conda environments and automatically activates the environment when the kernel starts.
Another way is that you write your own kernelspec that calls a script. In that script, you can activate the conda environment before starting the kernel.
I have just created a new env with python 3.5 using anaconda (called it python35). My root env points to python 2.7.11. I cant seem to launch ipython with this new env, here is what I did
1. in conda prompt, activate required env: activate python35
2. confirm the version: python --version
3. launch ipython: ipython notebook
4. open a notebook and do: import sys; print (sys.version)
Step #2, returns 3.5 but step #4 always gives me 2.7.11, its like ipython is picking up python version from root env. How do I fix this. Thanks for any help!
This question is related but I have already done what it suggests.
AFAIK, different environments in anaconda (and in venv as well) are activated by prepending env path to $PATH environment variable. It means, that if some file (eg, ipython) is not found in env path (the first entry of $PATH), the system searches for it in consequent entries of $PATH and finds it in root environment (that stays in $PATH). To fix the behavior, you need to install its own copy of ipython to anaconda env:
In command prompt, activate the environment: source activate python35 (or simply activate python35, depending on the OS)
While in environment, issue the command conda install ipython-notebook
An addition to Andrey Sobolev solution,you should switch to conda install ipython for higher python3.x version and log out the environment by Ctrl+D or conda deactivate then activate again
I could confirm the solution above (basically install notebook in active environment) in my case. Two updates from my side:
Since Anaconda 4.4 (?) ipython notebook is now jupyter notebook. So I had to install jupyter instead. I guess, deactivate and activate was required afterwards to get the path variables in notebook updated -> checked by python -c "import sys; print(sys.path)" or corresponding command in notebook.
If you aren't using a isolated anaconda environment, you may eventual calling the notebook package from your native OS-Python installation, instead the desired from Anaconda. Similarly, I had trouble with cmake or cxx, when I try to compile in Anaconda Environment - the diffent root folder might found in linux bash with e.g "which jupyter"