Han Zhengzu
Han Zhengzu

Reputation: 3842

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?

Upvotes: 5

Views: 16578

Answers (2)

drGabriel
drGabriel

Reputation: 808

Try by installing directly within Jupyter using the following command in a Jupyter cell:

import sys
!{sys.executable} -m pip install your_package_name

Upvotes: 10

Reblochon Masque
Reblochon Masque

Reputation: 36652

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

Upvotes: 1

Related Questions