集智翻译-Anaconda与Jupyter notebook 环境搭建与应用

来自集智百科
跳转到: 导航搜索

目录

Anaconda

(Translated by Li Ling 李凌 )

Welcome to this lesson on using Anaconda to manage packages and environments for use with Python. With Anaconda, it's simple to install the packages you'll often use in data science work. You'll also use it to create virtual environments that make working on multiple projects much less mind-twisting. Anaconda has simplified my workflow and solved a lot of issues I had dealing with packages and multiple Python versions.

Anaconda is actually a distribution of software that comes with conda, Python, and over 150 scientific packages and their dependencies. The application conda is a package and environment manager. Anaconda is a fairly large download (~500 MB) because it comes with the most common data science packages in Python. If you don't need all the packages or need to conserve bandwidth or storage space, there is also Miniconda, a smaller distribution that includes only conda and Python. You can still install any of the available packages with conda, it just doesn't come with them.

Conda is a program you'll be using exclusively from the command line, so if you aren't comfortable using it, check out this command prompt tutorial for Windows or our Linux Command Line Basics course for OSX/Linux.

You probably already have Python installed and wonder why you need this at all. Firstly, since Anaconda comes with a bunch of data science packages, you'll be all set to start working with data. Secondly, using conda to manage your packages and environments will reduce future issues dealing with the various libraries you'll be using.

欢迎打开这篇介绍Anaconda的文章,它会教你怎样用Anaconda来管理一些与Python配合使用的软件包与运行环境。通过Anaconda, 你能更轻松的安装那些经常在大数据处理中使用到的软件。你也可以通过它轻而易举的搭建可以同时进行多个项目的虚拟环境。Anaconda 简化了我们的工作流程,解决了很多Python版本与软件包安装时遇到的问题。

Anaconda其实就是一系列软件的集合发行版,包括Conda、Python以及150多个科学软件包。Conda就是一个软件包与运行环境的管理器。Anaconda大概会占用你500MB的空间,因为它包含了Python中涉及到大数据处理的最常用软件包。如果你不需要所有的软件包,或者想保留你的带宽或硬盘空间,你也可以下载Miniconda,一个只包含Conda和Python的发行版。你也可以用Conda安装其他任何可用的软件包,只是它们不是一起下载来的。

Conda是一个只能使用命令行来操作的程序,如果你还不习惯,可以先去学习一下针对Windows或Linux与OSX 进行命令行操作的相关教程。

你可能已经安装了Python,并对为什么要装这个Anaconda而感到好奇。

首先,因为Anaconda包含了一堆大数据分析所需要的软件包,你可以立即开始工作。另外,使用Conda来管理你的软件包和运行环境可以减少你以后使用多个数据库时遇到的各种问题。

Managing Packages

软件包的管理

(Translated by 苏格兰)

Installing numpy with conda

Package managers are used to install libraries and other software on your computer. You’re probably already familiar with pip, it’s the default package manager for Python libraries. Conda is similar to pip except that the available packages are focused around data science while pip is for general use. However, conda is not Python specific like pip is, it can also install non-Python packages. It is a package manager for any software stack. That being said, not all Python libraries are available from the Anaconda distribution and conda. You can (and will) still use pip alongside conda to install packages.

Conda installs precompiled packages. For example, the Anaconda distribution comes with Numpy, Scipy and Scikit-learn compiled with the MKL library, speeding up various math operations. The packages are maintained by contributors to the distribution which means they usually lag behind new releases. But because someone needed to build the packages for many systems, they tend to be more stable (and more convenient for you).


用Conda安装numpy

软件包管理器用于在计算机上安装库和其他软件。可能你已经熟悉pip了,它是Python库的默认包管理器。 Conda与pip类似,只不过Conda的可用包专注于数据科学,而pip应用广泛。然而,conda并不像pip那样为Python量身打造,它也可以安装非Python包。它是任何软件堆栈的包管理器。话虽如此,不是所有的Python库都可以从Anaconda发行版和conda获得。

你可以(并且今后仍然)使用pip和conda一起安装软件包。Conda安装预编译的软件包。例如,Anaconda发行版带有使用MKL库编译的Numpy,Scipy和Scikit-learn,它们加快了各种数学操作。这些软件包由供应商维护,这意味着它们通常落后于新版本。但是对于需要为许多系统构建软件包的客户来说,这些软件包往往更稳定(并且更加方便您使用)。

Environments

(Translated by 李晗)

Creating an environment with conda

Along with managing packages, Conda is also a virtual environment manager. It's similar to virtualenv and pyenv, other popular environment managers. Environments allow you to separate and isolate the packages you are using for different projects. Often you’ll be working with code that depends on different versions of some library. For example, you could have code that uses new features in Numpy, or code that uses old features that have been removed. It’s practically impossible to have two versions of Numpy installed at once. Instead, you should make an environment for each version of Numpy then work in the appropriate environment for the project. This issue also happens a lot when dealing with Python 2 and Python 3. You might be working with old code that doesn’t run in Python 3 and new code that doesn’t run in Python 2. Having both installed can lead to a lot of confusion and bugs. It’s much better to have separate environments. You can also export the list of packages in an environment to a file, then include that file with your code. This allows other people to easily load all the dependencies for your code. Pip has similar functionality with pip freeze > requirements.txt.

使用Conda创造一个环境

Conda除了可以管理包,也是一个虚拟环境管理工具。它与virutalenv和pyenv等其他流行的环境管理工具类似。不同的项目的环境允许你分离和独立出你在不同项目中使用的不同包。你通常会经常使用不同的代码工作,而这些代码依赖于不同版本的库。例如,你的代码可能使用了Numpy的新特性,而另一些代码使用了一些被废除的旧特性。但是你实际上你不可能一次同时安装2个不同版本的Numpy。取而代之,你可以针对每个版本Numpy设置一个环境,然后让你的项目在恰当的环境下工作。当你在Python 2和Python 3之间切换时,这种问题同样很常见。你使用的旧代码可能不能在Python 3下运行,而新的代码不能在Python 2下运行。但同时安装2个Python会导致许多混乱和问题。而使用分离的环境是一个更好的选择。

你同样可以把一个环境中包的列表输出到文件中,然后把这个文件和你的代码放在一起。这样其他人就可以简单地载入所有你的代码所依赖的内容。Pip也有类似的功能,可以通过pip freeze > requirements.txt将环境导出。

Where we go from here

(Translated by 陈浩然)

Next, I'll get into the details of using Anaconda. First I'll cover installing it, then using the package manager, and finally creating and managing environments.

Installing Anaconda

Anaconda is available for Windows, Mac OS X, and Linux. You can find the installers and installation instructions at https://www.continuum.io/downloads. If you already have Python installed on your computer, this won't break anything. Instead, the default Python used by your scripts and programs will be the one that comes with Anaconda. Choose the Python 3.5 version, you can install Python 2 versions later. Also, choose the 64-bit installer if you have a 64-bit operating system, otherwise go with the 32-bit installer. Go ahead and choose the appropriate version, then install it. Continue on afterwards! After installation, you’re automatically in the default conda environment with all packages installed which you can see below. You can check out your own install by entering conda list into your terminal.

下面,我将详细讲述如何使用Anaconda。首先,我将介绍如何安装,然后是如何使用软件包管理器,最后介绍创建和管理运行环境

安装Anaconda

Anaconda 可以在Windows, Mac OS X,Linux上面运行。可以在下面的链接找到安装文件和安装说明https://www.continuum.io/downloads。如果你已经在电脑上安装了Python, 此次安装不会破坏任何东西。相反的,您的脚本和程序使用的默认Python将是Anaconda自带的。选择Python 3.5版本,可以稍后安装Python 2版本。此外,如果您有64位操作系统,请选择64位安装程序,否则选择32位安装程序。继续选择适当的版本,然后安装它。然后继续。

安装后,您将自动在默认的conda环境中安装您可以在下面看到所有软件包。您可以通过在终端窗口输入conda list检查自己的安装

On Windows

(Translated by 李昉)

A bunch of applications are installed along with Anaconda:

Anaconda Navigator, a GUI for managing your environments and packages

Anaconda Prompt, a terminal where you can use the command line interface to manage your environments and packages

Spyder, an IDE geared toward scientific development

To avoid errors later, it's best to update all the packages in the default environment. Open the Anaconda Prompt application. In the prompt, run the following commands:

conda upgrade conda
conda upgrade --all

and answer yes when asked if you want to install the packages. The packages that come with the initial install tend to be out of date, so updating them now will prevent future errors from out of date software.

Note: In the previous step, running conda upgrade conda should not be necessary because --all includes the conda package itself, but some users have encountered errors without it.

In the rest of this lesson, I'll be asking you to use commands in your terminal. I highly suggest you start working with Anaconda this way, then later use the GUI if you'd like.

很多应用和Anaconda一起被安装

• Anaconda Navigator, 用于管理你的环境和包的图形界面

• Anaconda Prompt, 可以通过命令行管理你的环境和包的终端程序

• Spyder, 面向科学开发的集成开发环境(IDE)

为了避免错误, 最好是在默认环境更新所有的包。打开Anaconda Prompt程序,在提示行,运行下面的命令:

conda upgrade conda conda upgrade --all

当问你是否要安装包的时候,回答yes。 初始安装带的包往往陈旧了,所以应该把这些陈旧的包升级以避免错误发生。

注意:在前面的步骤中,运行conda upgrade conda本来不需要,因为 --all已经包含了conda包本身,但是一些用户不运行它会报错。

在最后,我将请你在终端程序使用命令。我强烈建议你用这种方式开始学习Anaconda,然后稍后如果你喜欢,再使用图形界面。

Managing environments

(Translated by 张倩)

As I mentioned before, conda can be used to create environments to isolate your projects. To create an environment, use conda create -n env_name list of packages in your terminal. Here -n env_namesets the name of your environment (-n for name) and list of packages is the list of packages you want installed in the environment. For example, to create an environment named my_env and install numpy in it, type conda create -n my_env numpy.

When creating an environment, you can specify which version of Python to install in the environment. This is useful when you're working with code in both Python 2.x and Python 3.x. To create an environment with a specific Python version, do something like conda create -n py3 python=3 or conda create -n py2 python=2. I actually have both of these environments on my personal computer. I use them as general environments not tied to any specific project, but rather for general work with each Python version easily accessible. These commands will install the most recent version of Python 3 and 2, respectively. To install a specific version, use conda create -n py python=3.3 for Python 3.3.

Entering an environment

Once you have an environment created, use source activate my_env to enter it on OSX/Linux. On Windows, use activate my_env. When you're in the environment, you'll see the environment name in the terminal prompt. Something like (my_env) ~ $. The environment has only a few packages installed by default, plus the ones you installed when creating it. You can check this out with conda list. Installing packages in the environment is the same as before: conda install package_name. Only this time, the specific packages you install will only be available when you're in the environment. To leave the environment, type source deactivate (on OSX/Linux). On Windows, use deactivate.

如同之前提到的,conda可以用来给你的项目创建独立环境。创建环境,可以在终端中使用:

conda create -n env_name [包的列表]

这里,-n env_name 是为你的环境(-n就表示环境)命名,[包的列表]是你要在环境中安装的包的列表。例如,你可以创建一个名字为my_env的环境,并且安装上了numpy包,那么你可以输入conda create -n my_env numpy

创建环境时,可以指定要在环境中安装的Python版本。 当你同时在Python 2.x和Python 3.x中使用代码时,这是非常有用的。 要创建具有特定Python版本的环境,请执行类似conda create -n py3 python = 3或conda create -n py2 python = 2这样的命令。 实际上,在我的个人计算机上,我同时有这两个环境。我使用它们作为一般环境,而不绑定到任何具体项目,而是希望很方便地访问每个Python版本。 这些命令将分别安装最新版本的Python 3和2。如果要安装特定版本,例如 Python 3.3,使用conda create -n py python=3.3。


进入环境

一旦创建了环境,你只要在OSX / Linux上输入source activate my_evn就能进入环境。 在Windows上,使用active my_env就能进入。 当你进入了环境时,你就能在终端提示符中看到环境名称。 类似(my_env)〜$。 环境默认情况下仅安装了几个软件包,以及在创建时安装的软件包。 你可以用conda list检查它。在环境中安装软件包与之前相同:conda install [包名称]。 只有这时,你安装的特定软件包才会在环境中可用。 要退出环境,请键入source deactivate(在OSX / Linux上)。 在Windows上,使用deactivate。

Saving and loading environments

(Translated by 王大鱼 )

A really useful feature is sharing environments so others can install all the packages used in your code, with the correct versions. You can save the packages to a YAML file with conda env export > environment.yaml. The first part conda env export writes out all the packages in the environment, including the Python version.

Exported environment printed to the terminal

Above you can see the name of the environment and all the dependencies (along with versions) are listed. The second part of the export command, > environment.yaml writes the exported text to a YAML file environment.yaml. This file can now be shared and others will be able to create the same environment you used for the project.To create an environment from an environment file use conda env create -f environment.yaml. This will create a new environment with the same name listed in environment.yaml.


Listing environments

If you forget what your environments are named (happens to me sometimes), use conda env list to list out all the environments you've created. You should see a list of environments, there will be an asterisk next to the environment you're currently in. The default environment, the environment used when you aren't in one, is called root.

Removing environments

If there are environments you don't use anymore, conda env remove -n env_name will remove the specified environment (here, named env_name).

保存与加载环境

Anaconda 有一个特别有用的特性就是可以分享运行环境,这样其他人就可以下载所有的包,在合适的版本下使用您的代码。您可以通过 conda env export > environment.yaml保存包至YAML文件。在第一部分conda env目录下中导出书写所有在这个运行环境下的包,其中也包括python版本。

将输出环境打印到终端

以上您可以看到运行环境的名称和所有附属已列出来。在第二部分命令输出命令,用> environment.yaml可以写输出文本至YAML文件environment.yaml。这个文件可以被分享而且其他人可以与您创建相同运行环境来使用这个项目。从运行环境文件中创建运行环境用conda env 创建-f environment.yaml。这个将以在environment.yaml中已列出的名字来创建一个新的运行环境。

列表环境

如果您忘记了运行环境的名字(这种情况在我身上时有发生),用conda env list可以列出所有您创建过的环境名称。您应该可以看到在最近您访问的环境名称后面会被标星。对于默认运行环境,当您不是一个人在使用这个运行环境时它被称为根。

移除环境

你可以将不再需要使用的运行环境移除,conda env remove -n env_name 将会移除所有特定运行环境(这里,命名为env_name)

Best practices

(Translated by 蔡静静)

Using environments

One thing that’s helped me tremendously is having separate environments for Python 2 and Python 3. I used conda create -n py2 python=2 and conda create -n py3 python=3 to create two separate environments, py2 and py3. Now I have a general use environment for each Python version. In each of those environments, I've installed most of the standard data science packages (numpy, scipy, pandas, etc.)

I’ve also found it useful to create environments for each project I’m working on. It works great for non-data related projects too like web apps with Flask. For example, I have an environment for my personal blog using Pelican.

Sharing environments

When sharing your code on GitHub, it's good practice to make an environment file and include it in the repository. This will make it easier for people to install all the dependencies for your code. I also usually include a pip requirements.txt file using pip freeze (learn more here) for people not using conda.

最好的做法

运行环境

有件事帮助了我很多,那就是拥有Python 2和Python 3独立的环境。我用“conda create -n py2 python=2”及“conda create -n py3 python=3”两行代码创造出两个独立的运行环境,也就是py2和py3。现在,针对各个版本的Python,我有一个通用的运行环境。在每一个运行环境里,我安装了大部分标准数据科学包(numpy, scipy, pandas等等)。

我还发觉,为我正在处理工作的每个项目创建环境很有用。它非常适合非数据相关的项目,就像Web应用程序与Flask。例如,我就有一个使用Pelican的个人博客的环境。

共享环境

在GitHub上分享代码时,制作一个环境文件夹并将其包含在信息存储库中是个好习惯。这将使人们更易安装你的代码的所有依赖项。我也通常使用pip冻结(点击这里了解更多)将pip requirements.txt文件包含在内,方便那些不使用conda的人。

More to learn

更多的学习

(Translated by ) (田梁穗儿 译)

To learn more about conda and how it fits in the Python ecosystem, check out this article by Jake Vanderplas: Conda myths and misconceptions. And here's the conda documentation you can reference later.

想要了解更多关于Conda以及它如何适应Python的生态系统,可以看看由Jake Vanderplas写的这篇文章:Conda神话与误解。这里是Conda文档供您稍后参考。

What are Jupyter notebooks?

什么是Jupyter Notebooks?

Welcome to this lesson on using Jupyter notebooks. The notebook is a web application that allows you to combine explanatory text, math equations, code, and visualizations all in one easily sharable document. For example, here's one of my favorite notebooks shared recently, the analysis of gravitational waves from two colliding blackholes detected by the LIGO experiment. You could download the data, run the code in the notebook, and repeat the analysis, in effect detecting the gravitational waves yourself!

Notebooks have quickly become an essential tool when working with data. You'll find them being used for data cleaning and exploration, visualization, machine learning, and big data analysis. Here's an example notebook I made for my personal blog that shows off many of the features of notebooks. Typically you'd be doing this work in a terminal, either the normal Python shell or with IPython. Your visualizations would be in separate windows, any documentation would be in separate documents, along with various scripts for functions and classes. However, with notebooks, all of these are in one place and easily read together.

Notebooks are also rendered automatically on GitHub. It’s a great feature that lets you easily share your work. There is also http://nbviewer.jupyter.org/ that renders the notebooks from your GitHub repo or from notebooks stored elsewhere.

欢迎来到本课-使用Jupyter Notebook。此Notebook是一个Web应用程序,允许你把所有的说明性文字,数学公式,代码和可视化内容结合在一个可轻松共享的文档里。例如,这是我最近特别喜欢共享的Notebook之一,激光干涉引力波观测站(LIGO)对从两个碰撞黑洞发出引力波的检波实验的分析。你可以下载数据,在Notebook上运行代码和重复分析,实际上你可以自己检测引力波。

在使用数据时,Notebooks很快成为了必不可少的工具,你会发现它们被用于数据清理和探索,可视化,机器学习和大数据分析。这是一个我为自己的个人博客制作的Notebook,展示了Notebook的许多特性。通常你会在一个终端做这项工作,无论是普通的Python shell还是IPython,您的可视化将在单独的窗口,任何文档以及各种脚本的功能和类将在单独的文档。但是用Notebook,所有的这些都在一个地方,很容易一起阅读。

Notebooks也在Github上自动呈现。这是一个伟大的功能,让你轻松的分享你的工作。也有http://nbviewer.jupyter.org 能够很好地展现你存在Github repo或别处的Notebook文档。

Literate programming

(Translated by Li Lijing ) 翻译者:李丽京

Notebooks are a form of literate programming proposed by Donald Knuth in 1984. With literate programming, the documentation is written as a narrative alongside the code instead of sitting off by it's own. In Donald Knuth's words,Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.

After all, code is written for humans, not for computers. Notebooks provide exactly this capability. You are able to write documentation as narrative text, along with code. This is not only useful for the people reading your notebooks, but for your future self coming back to the analysis. Just a small aside: recently, this idea of literate programming has been extended to a whole programming language, Eve.


How notebooks work

Jupyter notebooks grew out of the IPython project started by Fernando Perez. IPython is an interactive shell, similar to the normal Python shell but with great features like syntax highlighting and code completion. Originally, notebooks worked by sending messages from the web app (the notebook you see in the browser) to an IPython kernel (an IPython application running in the background). The kernel executed the code, then sent it back to the notebook. The current architecture is similar, drawn out below.


文艺编程(Literate programming)

Notebook是唐纳德·克努特(高德纳)在1984年提出的一种文艺编程形式。有了文艺编程,我们就可以将文档编辑为一种叙述性的文字伴随着代码的形式,而不是单纯的代码。用唐纳德·克努特的话来说,与其说我们的主要任务是为了指导电脑做什么,还不如将我们的精力集中在给人类解释我们正在让电脑做什么。

毕竟,代码是为人类编写的,而不是电脑。Notebook就很好地提供了这种功能。你能够将编写文档作为叙事文本,以及代码。这不仅对人们阅读你的Notebook有帮助,而且对未来的你想回头做一些分析也很有用。还有:最近,这个文艺编程的想法已经扩展到一种整体编程语言:Eve。

Notebook如何工作


Notebook是唐纳德·克努特(高德纳)在1984年提出的一种文艺编程形式。有了文艺编程,我们就可以将文档编辑为一种叙述性的文字伴随着代码的形式,而不是单纯的代码。用唐纳德·克努特的话来说,与其说我们的主要任务是为了指导电脑做什么,还不如将我们的精力集中在给人类解释我们正在让电脑做什么。

毕竟,代码是为人类编写的,而不是电脑。Notebook就很好地提供了这种功能。你能够将编写文档作为叙事文本,以及代码。这不仅对人们阅读你的Notebook有帮助,而且对未来的你想回头做一些分析也很有用。还有:最近,这个文艺编程的想法已经扩展到一种整体编程语言:Eve。

From Jupyter documentation

源自Jupyter 文档 (Translated by 刘晴敏 )

The central point is the notebook server. You connect to the server through your browser and the notebook is rendered as a web app. Code you write in the web app is sent through the server to the kernel. The kernel runs the code and sends it back to the server, then any output is rendered back in the browser. When you save the notebook, it is written to the server as a JSON file with a .ipynb file extension.

The great part of this architecture is that the kernel doesn't need to run Python. Since the notebook and the kernel are separate, code in any language can be sent between them. For example, two of the earlier non-Python kernels were for the R and Julia languages. With an R kernel, code written in R will be sent to the R kernel where it is executed, exactly the same as Python code running on a Python kernel. IPython notebooks were renamed because notebooks became language agnostic. The new name Jupyter comes from the combination of Julia, Python, and R. If you're interested, here's a list of available kernels.

Another benefit is that the server can be run anywhere and accessed via the internet. Typically you'll be running the server on your own machine where all your data and notebook files are stored. But, you could also set up a server on a remote machine or cloud instance like Amazon's EC2. Then, you can access the notebooks in your browser from anywhere in the world.

关键点在于notebook的服务器。你通过你的浏览器以及notebook链接到服务器使其作为一个web应用程序来呈现。你在web应用程序中编写的代码通过服务器发送到内核。内核运行代码并将其发送回服务器,然后任何输出都会呈现的浏览器上。当你保存notebook的时候,它将会以JSON文件以及.ipynb的文件扩展名来写入服务器。

这个架构的优点在于内核不需要运行Python。由于notebook和内核是分开的,任何形式的代码都可以在它们之间传送。例如,两个早期版本的非Python的内核就是为R和Julia语言设计的。在R内核中,用R语言编写的代码将会被送到R内核执行,等同于Python代码在Python内核里运行。因为notebooks的含义并不是很清楚,所以人们将早期的Ipython notebooks重新命名了。新名称Jupyter来自于与Julia,Python和R的结合。如果你感兴趣,这里有一个可用内核的表。

另一个好处是服务器可以通过互联网在任何地方运行和访问。通常,你将会在储存了所有数据和notebook文件的自己的机器上来运行服务器。但是,你也可以设置一个远程机器或者一个像Amazon’s EC2的云实例。然后,你可以在世界任何地方访问你浏览器中的notebooks。

Installing Jupyter Notebook

安装Jupyter Notebook

(Translated by Cicely)

By far the easiest way to install Jupyter is with Anaconda. Jupyter notebooks automatically come with the distribution. You'll be able to use notebooks from the default environment.

To install Jupyter notebooks in a conda environment, use conda install jupyter notebook. Jupyter notebooks are also available through pip with pip install jupyter notebook.


Launching the notebook server

启动notebook服务器

To start a notebook server, enter jupyter notebook in your terminal or console. This will start the server in the directory you ran the command in. That means any notebook files will be saved in that directory. Typically you'd want to start the server in the directory where your notebooks live. However, you can navigate through your file system to where the notebooks are.

When you run the command (try it yourself!), the server home should open in your browser. By default, the notebook server runs at http://localhost:8888. If you aren't familiar with this, localhost means your computer and 8888 is the port the server is communicating on. As long as the server is still running, you can always come back to it by going to http://localhost:8888 in your browser.


If you start another server, it'll try to use port 8888, but since it is occupied, the new server will run on port 8889. Then, you'd connect to it at http://localhost:8889. Every additional notebook server will increment the port number like this. If you tried starting your own server, it should look something like this:


You might see some files and folders in the list here, it depends on where you started the server from. Over on the right, you can click on "New" to create a new notebook, text file, folder, or terminal. The list under "Notebooks" shows the kernels you have installed. Here I'm running the server in a Python 3 environment, so I have a Python 3 kernel available. You might see Python 2 here. I've also installed kernels for Scala 2.10 and 2.11 which you see in the list. If you run a Jupyter notebook server from a conda environment, you'll also be able to choose a kernel from any of the other environments (see below). To create a new notebook, click on the kernel you want to use.

目前安装Jupyter最简便的方式是使用Anaconda,其自带了Jupyter notebooks工具包,用户可以在默认环境下使用notebooks。

要在conda环境中安装Jupyter notebooks,请使用conda install jupyter notebook。同样可以通过pip命令完成Jupyter notebooks的安装:pip install jupyter notebook。 要启动notebook服务器,请在终端或控制台中输入jupyter notebook,随后将在您运行命令的目录中启动服务器,这意味着任何notebook文件都会储存在这个目录下。通常情况下,您希望在notebooks的目录下启动服务器。您可以通过文件系统浏览到notebook所在的位置。

当您运行那个命令(自己尝试!),服务器主页将在您的浏览器中打开。默认情况下,notebook服务器运行在http://localhost:8888端口。如果您对此不熟悉,localhost意味着您的计算机,8888是服务器正在通信的端口。只要服务器仍在运行,您随时可以返回,只要你在浏览器中访问http://localhost:8888即可。

如果您需要启动另外一个服务器,它会先尝试使用端口8888。由于端口已经被占用,新的服务器将在端口8889上运行。然后,您会连接到http://localhost:8889。每新增一个notebook服务器都会使得这个端口号递增1:

您可在此处的列表中看到一些文件和文件夹,这取决于您从哪里启动服务器。在右侧,您可以单击“新建”创建新的notebook、文本文件、文件夹或终端。“Notebooks”下的列表显示您已安装的内核。这里我在Python 3环境中运行服务器,所以我有一个Python 3内核可用。 您可在这里看到Python 2。 我还为列表中的Scala 2.10和2.11安装了内核。 如果从conda环境运行Jupyter notebook服务器,您还可以从其他任何环境中选择内核(见下文)。 要创建新的notebook,请单击要使用的内核。

conda environments in Jupyter

(Translated by W先森)

The tabs at the top show Files, Running, and Cluster. Files shows all the files and folders in the current directory. Clicking on the Running tab will list all the currently running notebooks. From there you can manage them. Clusters previously was where you'd create multiple kernels for use in parallel computing. Now that's been taken over by ipyparallel so there isn't much to do there.

If you're running the notebook server from a conda environment, you'll also have access to a "Conda" tab shown below. Here you can manage your environments from within Jupyter. You can create new environments, install packages, update packages, export environments and more.

conda tab in Jupyter

Shutting down Jupyter

You can shutdown individual notebooks by marking the checkbox next to the notebook on the server home and clicking "Shutdown." Make sure you've saved your work before you do this though! Any changes since the last time you saved will be lost. You'll also need to rerun the code the next time you run the notebook.

You can shutdown the entire server by pressing control + C twice in the terminal. Again, this will immediately shutdown all the running notebooks, so make sure your work is saved!

顶部导航有文件,执行和集群的选项。点击文件将显示当前目录下的所有的文件夹及文件。点击running将列出所有正在执行的脚本并可以进行管理操作。群组原本的作用是显示已创建的用于并行计算的多CPU核,现在由于已被ipyoaraller代所以并没有什么用处。

如果你从conda环境运行脚本服务器,将可以看到一个“Conda”按钮。用此按钮你可以从Jupyter里面管理环境。你可以创建新环境,安装包,升级包和导出环境等。

关闭Jupyter

要关闭单个脚本,你可以在服务器主页处勾选对应脚本然后点击关闭。请在关闭前确保已经保存!因为关闭后,你的所有修改都将会丢失。下一次你执行脚本时仍然需要重新运行代码。

你也可以在终端按两次“control + C”来关闭整个服务器,再次强调在作出关闭动作前请保证做好保存,因为该动作会立即关闭所有的在运行的脚本。

个人工具
名字空间
操作
导航
工具箱