我使用Jupyter笔记本在浏览器中进行Python编程,我已经安装了Anaconda (Python 3.5)。但我很确定Jupyter是用本地python解释器运行我的python命令,而不是用anaconda。我怎么能改变它,并使用蟒蛇作为解释器?
import sys
sys.executable
我会给你翻译的。您可以在创建新笔记本时选择所需的解释器。确保anaconda解释器的路径被添加到您的路径中(很可能在bashrc/bash_profile的某个地方)。
例如,我曾经在我的.bash_profile中有以下一行,我手动添加:
export PATH="$HOME/anaconda3/bin:$PATH"
编辑:正如在评论中提到的,这不是将anaconda添加到路径的正确方法。引用Anaconda的文档,这应该在安装后执行,使用conda init:
我应该把Anaconda添加到macOS或Linux PATH中吗? 我们不建议手动将Anaconda添加到PATH。在 安装时,你会被问到“你希望安装程序。 通过conda init初始化Anaconda3 ?”我们建议“是”。如果 您输入“no”,那么conda将不会修改您的shell脚本。 为了初始化安装过程完成后,首先 执行命令source <path to conda>/bin/activate,然后执行命令conda init
假设您使用了错误的后端系统,您可以通过创建新内核或编辑现有内核来更改后端内核。Json在你的jupyter数据路径jupyter——paths的内核文件夹。你可以有多个内核(R, Python2, Python3 (+virtualenvs), Haskell),例如,你可以创建一个Anaconda特定的内核:
$ <anaconda-path>/bin/python3 -m ipykernel install --user --name anaconda --display-name "Anaconda"
应该创建一个新的内核:
/<jupyter-data-dir>kernels/anaconda/kernel.json
{
"argv": [ "<anaconda-path>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "Anaconda",
"language": "python"
}
您需要确保在anaconda发行版中安装了ipykernel包。
这样你就可以在内核之间切换,使用不同的内核拥有不同的笔记本。
import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
-输出当我在CONDA venv外部运行JupyterNotebook
/home/dhankar/anaconda2/bin/python
2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
如下所示,当我在命令——创建的CONDA Venv中运行相同的JupyterNoteBook
conda create -n py35 python=3.5 ## Here - py35 , is name of my VENV
在我的Jupyter笔记本打印:-
/home/dhankar/anaconda2/envs/py35/bin/python
3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
另外,如果你已经用不同版本的Python创建了各种各样的VENV,你可以通过在JupyterNotebook菜单中选择Kernel >> CHANGE Kernel切换到所需的内核…… JupyterNotebookScreencapture
还可以在现有的CONDA虚拟环境中安装ipykernel
http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments
来源——https://github.com/jupyter/notebook/issues/1524
$ /path/to/python -m ipykernel install --help
usage: ipython-kernel-install [-h] [--user] [--name NAME]
[--display-name DISPLAY_NAME]
[--profile PROFILE] [--prefix PREFIX]
[--sys-prefix]
安装IPython内核规范。
optional arguments: -h, --help show this help message and exit --user Install for the current user instead of system-wide --name NAME Specify a name for the kernelspec. This is needed to have multiple IPython kernels at the same time. --display-name DISPLAY_NAME Specify the display name for the kernelspec. This is helpful when you have multiple IPython kernels. --profile PROFILE Specify an IPython profile to load. This can be used to create custom versions of the kernel. --prefix PREFIX Specify an install prefix for the kernelspec. This is needed to install into a non-default location, such as a conda/virtual-env. --sys-prefix Install to Python's sys.prefix. Shorthand for --prefix='/Users/bussonniermatthias/anaconda'. For use in conda/virtual-envs.
from platform import python_version
print(python_version())
这将为您提供运行脚本的python的确切版本。如输出:
3.6.5
创建Jupyter笔记本的虚拟环境
最小的Python安装
sudo apt install python3.7 python3.7-venv python3.7-minimal python3.7-distutils python3.7-dev python3.7-gdbm python3-gdbm-dbg python3-pip
然后您就可以创建和使用环境了
/usr/bin/python3.7 -m venv test
cd test
source test/bin/activate
pip install jupyter matplotlib seaborn numpy pandas scipy
# install other packages you need with pip/apt
jupyter notebook
deactivate
你可以给木星做内核
ipython3 kernel install --user --name=test
你可以使用
!python -V
or
!python --version
Python 3.6.5:: Anaconda, Inc.
您可以将Conda环境添加到jupyter笔记本
步骤1:创建Conda环境。
conda create --name firstEnv
步骤2:使用控制台中显示的命令激活环境。
conda activate firstEnv
conda install -c conda-forge <package-name>
E.g.
conda install -c conda-forge tensorflow
步骤3:在jupyter笔记本上设置这个conda环境
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=firstEnv
第四步:检查你的Jupyter笔记本,看看第一stenv
你可以参考这篇文章
https://medium.com/@nrk25693/how-to-add-your-conda-environment-to-your-jupyter-notebook-in-just-4-steps-abeab8b8d084