Jupyter notebook, ImportError: No module named pylab - python

I am a ubuntu user, and I have install python 2.7.9 on my computer.
In order to use jupyter notebook, I down load anaconda2.
After my installation, I find after entering the command 'jupyter notebook', my browser will open jupyter notebook, but I can't find new python2, so I went to the https://ipython.readthedocs.org/en/latest/install/kernel_install.html
Use the following commands:
conda create -n ipykernel_py2 python=2 ipykernel
source activate ipykernel_py2
python -m ipykernel install --user
Now I do can new python2 on the jupyter notebook, but when I enter the command 'from pylab import *'
it will show the error as ImportError: No module named pylab
but if I open a terminal and enter 'python' and then enter 'from pylab import *', it works all ok.
So, what is the problem? I am new to python and jupyter notebook.
Thank you very much.

I find the problem.
In terminal:
ipython
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/home/hust/anaconda2/bin',
'/home/hust/anaconda2/lib/python27.zip',
'/home/hust/anaconda2/lib/python2.7',
'/home/hust/anaconda2/lib/python2.7/plat-linux2',
'/home/hust/anaconda2/lib/python2.7/lib-tk',
'/home/hust/anaconda2/lib/python2.7/lib-old',
'/home/hust/anaconda2/lib/python2.7/lib-dynload',
'/home/hust/.local/lib/python2.7/site-packages',
'/home/hust/anaconda2/lib/python2.7/site-packages',
'/home/hust/anaconda2/lib/python2.7/site-packages/Sphinx-1.3.5-py2.7.egg',
'/home/hust/anaconda2/lib/python2.7/site-packages/setuptools-20.3-py2.7.egg',
'/home/hust/.local/lib/python2.7/site-packages/IPython/extensions',
'/home/hust/.ipython']
But in jupyter notebook, the sys.path is:
['',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python27.zip',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/plat-linux2',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/lib-tk',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/lib-old',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/lib-dynload',
'/home/hust/.local/lib/python2.7/site-packages',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/site-packages/setuptools-20.7.0-py2.7.egg',
'/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/site-packages',
'/home/hust/.local/lib/python2.7/site-packages/IPython/extensions',
'/home/hust/.ipython']
Then I remember I have used the following codes to create a python2 kernel.
conda create -n ipykernel_py2 python=2 ipykernel
source activate ipykernel_py2
python -m ipykernel install --user
In the website's description, if you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this. But actually I am running Jupyter on Python 2, the reason why I could not new a python2 script in jupyter notebook should be something else.
Finally, I realize that it is better to master all the pythons' environment via pyenv. I should firstly install pyenv and then I only need use these commands, pyenv install anaconda-2.4.0 , pyenv global anaconda-2.4.0 and jupyter notebook.

Related

ModuleNotFoundError in jupyter notebook but module import succeeded in ipython console in the same virtual environnement

I made a virtual env with:
$ python3 -m venv /home/jeanpat/Devel/icevision
Different modules were installed (pycocotools ...) with pip.
From a virtual environment activated as follow:
source /home/jeanpat/Devel/icevision/bin/activate
The modules can be imported from an ipython console with no error. When the modules are imported from a jupyter notebook run from the same env (icevision), the immport fails. from a notebook cell,I can run :
!pip freeze|grep pycocotools
and get the modules list:
pycocotools==2.0.4
I have:
(icevision) jeanpat:~/ $ jupyter --paths [11:06:36]
config:
/home/jeanpat/.jupyter
/home/jeanpat/Devel/icevision/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/home/jeanpat/.local/share/jupyter
/home/jeanpat/Devel/icevision/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/home/jeanpat/.local/share/jupyter/runtime
The problem is similar to, i try this:
$ ipython kernel install --name "icevision" --user
but it didn't resolve the problem.
Thanks for your suggestions.
I had to use the command to solve the issue in the answer by Peter S. Magnusson:
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"

Can't import the installed package in Python3 environment of jupyter notebook

I have already installed the Anaconda software based on Python 2.7, and the Python 3.4 kernel has also been configured.
Using pip3 install xxx, I can install some packages for Python 3 environment. In ipython3, they can be imported well. However, in Python 3 kernel of jupyter notebook, those packages can't be successfully imported.
# Python 3 environment inside the jupyter notebook
import numpy as np
> No module named 'numpy'
My kernel path:
Available kernels:
ir /Users/HYF/Library/Jupyter/kernels/ir
javascript /Users/HYF/Library/Jupyter/kernels/javascript
python2 /Users/HYF/anaconda/share/jupyter/kernels/python2
python3 /usr/local/share/jupyter/kernels/python3
The Python3 environment in jupyter kernel shows like this:
import sys
sys.executable
>'/Users/HYF/anaconda/envs/py35/bin/python'
I thought the problem is that the python3 package path is not loaded in jupyter notebook. How to fix this issue?
Try by installing directly within Jupyter using the following command in a Jupyter cell:
import sys
!{sys.executable} -m pip install your_package_name
You need to activate your python3 environment prior to installing packages:
On the command line: source activate python3_environmane_name (or the name of your python3 environment.
Then you either conda install package_name, or, if not available via conda, pip install package_name, or pip3 install package_name.
Using pip3 in the python2 environment will not magically install anything in another env.
Note: to return to the default env, on command line: source deactivate

Running Jupyter notebook in a virtualenv: installed sklearn module not available

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

discrepancy in numbers of installed modules between ipython and ipython notebook

Hi I have a curious (and maybe trivial who knows?) problem.
For the requirements of a course on coursera I have created a python 2.7 environment in my anaconda distribution and installed DATOs' GraphLab Create modules.
While the simple "import graphlab" functions properly from the python command line or the ipython console it fails in a jupyter notebook.
checking installed modules with pip.get_installed_distributions() yields 148 modules in ipython console and only 42 in the jupyter notebook!
So, what is at work here? :-)
Thanks for any hint,
Alain
As you said you have created an environment with Python 2.7, you thus need to register this environment with the notebook.
Activate the environement:
$ source activate myenv
Then install the kernelspec for this environment:
(myenv)$ python -m ipykernel install --name myenv
You will need to install ipykernel in this env if it is not installed.
Now when starting a notebook, you can create a new notebook using this env, and/or change the currently used env, using the Kernel menu.

How do I specify which python and which modules are being used in my jupyter notebook?

When I do
import sys
sys.executable
I get '/usr/local/opt/python/bin/python2.7' in my ordinary python shell and '/usr/bin/python' in IPython or my jupyter notebook. I would like to force my jupyter notebook to use this same python that the shell is using. I have installed many modules and would like to be able to use the same ones in jupyter than I am using already in the shell. How can I do this?
The simplest way is to install IPython and Jupyter with the Python you want them to use. You can do this using pip:
path/to/python -m pip install jupyter
You could alternatively set up the IPython kernel to run with your desired Python without reinstalling the notebook. See the docs on installing kernels. This is more complicated than just installing everything again, though.
I had the same problem when using jupyter from a virtualenv.
In my case I had two kernels named python3. Doing a jupyter kernelspec list it reported only one kernel named python3 that pointed to an incorrect binary. I removed it using jupyter kernelpec remove python3 and magically appeared the correct one pointing to my activated virtualenv.
If you need to reinstall the kernel then, from the bin directory of the virtualenv you can do,
./python -m pip install ipykernel
sudo ./python -m ipykernel install

Categories