Alex Rozhnov
Alex Rozhnov

Reputation: 199

Cannot import python package in jupyter

I have the following problem. I installed rpy2 via conda install rpy2 in my virtualenv. Now, I can import this package in terminal if I run

>>> python
>>> import rpy2

However, if I try to import rpy2 in jupyter notebook I get an error ModuleNotFoundError: No module named 'rpy2'. If I run the following code

import sys
print(sys.prefix)

I get /anaconda3. Also if I do the same via terminal: python->import sys -> print(sys.prefix) I get /anaconda3/envs/work_python36. If I run which python in terminal I obtain: /anaconda3/envs/work_python36/bin/python. I tried to change PYTHONPATH using export, but it did not work. If anyone knows how to solve this problem, please, tell me.

I also printed the following conda info -a

active environment : work_python36
active env location : /anaconda3/envs/work_python36
        shell level : 1
   user config file : /Users/alex/.condarc
populated config files : 
      conda version : 4.5.12
conda-build version : 3.10.5
     python version : 3.6.5.final.0
   base environment : /anaconda3  (writable)
       channel URLs : https://repo.anaconda.com/pkgs/main/osx-64
                      https://repo.anaconda.com/pkgs/main/noarch
                      https://repo.anaconda.com/pkgs/free/osx-64
                      https://repo.anaconda.com/pkgs/free/noarch
                      https://repo.anaconda.com/pkgs/r/osx-64
                      https://repo.anaconda.com/pkgs/r/noarch
                      https://repo.anaconda.com/pkgs/pro/osx-64
                      https://repo.anaconda.com/pkgs/pro/noarch
      package cache : /anaconda3/pkgs
                      /Users/alex/.conda/pkgs
   envs directories : /anaconda3/envs
                      /Users/alex/.conda/envs
           platform : osx-64
         user-agent : conda/4.5.12 requests/2.19.1 CPython/3.6.5 Darwin/18.2.0 OSX/10.14.2
            UID:GID : 501:20
         netrc file : None
       offline mode : False

# conda environments:
#
base                     /anaconda3
work_python36         *  /anaconda3/envs/work_python36

sys.version: 3.6.5 |Anaconda, Inc.| (default, Apr 26 ...
sys.prefix: /anaconda3
sys.executable: /anaconda3/bin/python
conda location: /anaconda3/lib/python3.6/site-packages/conda
conda-build: /anaconda3/bin/conda-build
conda-convert: /anaconda3/bin/conda-convert
conda-develop: /anaconda3/bin/conda-develop
conda-env: /anaconda3/bin/conda-env
conda-index: /anaconda3/bin/conda-index
conda-inspect: /anaconda3/bin/conda-inspect
conda-metapackage: /anaconda3/bin/conda-metapackage
conda-render: /anaconda3/bin/conda-render
conda-server: /anaconda3/bin/conda-server
conda-skeleton: /anaconda3/bin/conda-skeleton
conda-verify: /anaconda3/bin/conda-verify
user site dirs: 

CIO_TEST: <not set>
CONDA_BACKUP_HOST: x86_64-apple-darwin13.4.0
CONDA_BUILD_SYSROOT: 
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk
CONDA_DEFAULT_ENV: work_python36
CONDA_EXE: /anaconda3/bin/conda
CONDA_PREFIX: /anaconda3/envs/work_python36
CONDA_PROMPT_MODIFIER: (work_python36) 
CONDA_PYTHON_EXE: /anaconda3/bin/python
CONDA_ROOT: /anaconda3
CONDA_SHLVL: 1
PATH: /anaconda3/bin:/anaconda3/envs/work_python36/bin:/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin
PYTHONPATH: :~/anaconda3/envs/work_python36
REQUESTS_CA_BUNDLE: <not set>
SSL_CERT_FILE: <not set>
WARNING: could not import _license.show_info
# try:
# $ conda install -n root _license

Upvotes: 2

Views: 8431

Answers (1)

sat
sat

Reputation: 623

Make sure you are using the exact same virtual environment when creating a new notebook in Jupyter.

To enable a virtual environment as a kernel in Jupyter, execute the following commands -

$ python -m venv projectname
$ source projectname/bin/activate
(venv) $ pip install ipykernel
(venv) $ ipython kernel install --user --name=projectname
pip install rpy2

This will result in a new kernel being created -

enter image description here

Now the package will also be available inside the Jupyter notebook with projectname kernel.

Upvotes: 9

Related Questions