gpt4 book ai didi

python-3.x - 无法在jupyter中导入python包

转载 作者:行者123 更新时间:2023-12-05 08:53:19 28 4
gpt4 key购买 nike

我有以下问题。我通过 conda install rpy2 在我的 virtualenv 中安装了 rpy2。现在,如果我运行,我可以在终端中导入这个包

>>> python
>>> import rpy2

但是,如果我尝试在 jupyter notebook 中导入 rpy2,则会出现错误 ModuleNotFoundError: No module named 'rpy2'。如果我运行以下代码

import sys
print(sys.prefix)

我得到 /anaconda3。另外,如果我通过终端执行相同的操作:python->import sys -> print(sys.prefix) 我会得到 /anaconda3/envs/work_python36。如果我在终端中运行 which python,我将获得:/anaconda3/envs/work_python36/bin/python。我尝试使用导出更改 PYTHONPATH,但没有成功。如果有人知道如何解决这个问题,请告诉我。

我还打印了以下 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

最佳答案

确保在 Jupyter 中创建新笔记本时使用完全相同的虚拟环境。

要在 Jupyter 中启用虚拟环境作为内核,请执行以下命令 -

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

这将导致创建一个新内核 -

enter image description here

现在,该包也将在带有 projectname 内核的 Jupyter notebook 中可用。

关于python-3.x - 无法在jupyter中导入python包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129100/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com