如何设置 Jupyter Notebook 在 Ubuntu 16.04 上运行 IPython

2023-11-16

介绍

IPython 是 Python 的交互式命令行界面。 Jupyter Notebook 为多种语言提供了交互式 Web 界面,包括 IPython。

本文将引导您设置服务器来运行 Jupyter Notebook,并教您如何连接和使用该笔记本。 Jupyter 笔记本(或简称笔记本)是由 Jupyter Notebook 应用程序生成的文档,其中包含计算机代码(例如 Python)和丰富的文本元素(段落、方程、图形、链接等),有助于呈现可重复的研究。

在本指南结束时,您将能够使用在远程服务器上运行的 Ipython 和 Jupyter Notebook 来运行 Python 2.7 代码。出于本教程的目的,使用 Python 2 (2.7.x),因为许多数据科学、科学计算和高性能计算库支持 2.7 而不是 3.0+。

先决条件

要学习本教程,您将需要以下内容:

  • Ubuntu 16.04 小滴
  • 具有 sudo 权限的非 root 用户(使用 Ubuntu 16.04 进行初始服务器设置解释了如何设置。)

本教程中的所有命令都应以非 root 用户身份运行。如果该命令需要 root 访问权限,则前面将显示sudo. 使用 Ubuntu 16.04 进行初始服务器设置解释如何添加用户并授予他们 sudo 访问权限。

第 1 步 — 安装 Python 2.7 和 Pip

在本节中,我们将安装 Python 2.7 和 Pip。

首先,更新系统的包索引。这将确保旧的或过时的软件包不会干扰安装。

  1. sudo apt-get update

接下来,安装 Python 2.7、Python Pip 和 Python Development:

  1. sudo apt-get -y installpython2.7 python-pip python-dev

安装中python2.7将更新到最新版本的Python 2.7,并且python-pip将安装 Pip,它允许我们管理我们想要使用的 Python 包。 Jupyter 的一些依赖项可能需要编译,在这种情况下,您需要能够编译 Python C 扩展,因此我们正在安装python-dev以及。

要验证您是否已安装 python:

  1. python - 版本

这将输出:

Output
Python 2.7.11+

根据 Python 2.7 的最新版本,输出可能会有所不同。

您还可以使用以下命令检查 pip 是否已安装:

  1. pip - 版本

您应该类似于以下内容:

Output
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

同样,根据您的 pip 版本,输出可能会略有不同。

第 2 步 — 安装 Ipython 和 Jupyter Notebook

在本节中,我们将安装 Ipython 和 Jupyter Notebook。

首先,安装Ipython:

  1. sudo apt-get -y installipython ipython-笔记本

现在我们可以继续安装 Jupyter Notebook:

  1. sudo -H pip install朱皮特

根据 Ubuntu apt-get 存储库中 pip 的版本,您在尝试安装 Jupyter 时可能会收到以下错误:

Output
You are using pip version 8.1.1, however version 8.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

如果是这样,您可以使用pip将pip升级到最新版本:

  1. sudo -H pip install - 升级 pip

升级 pip,然后再次尝试安装 Jupyter:

  1. sudo -H pip install朱皮特

第 3 步 — 运行 Jupyter Notebook

您现在拥有运行 Jupyter Notebook 所需的一切!要运行它,请执行以下命令:

  1. Jupyter笔记本

如果您在安装了 JavaScript 的系统上运行 Jupyter,它仍然会运行,但可能会出现错误,指出 Jupyter Notebook 需要 JavaScript:

Output
Jupyter Notebook requires JavaScript. Please enable it to proceed. ...

要忽略该错误,您可以按Q然后按Y确认。

A log of the activities of the Jupyter Notebook will be printed to the terminal. When you run Jupyter Notebook, it runs on a specific port number. The first notebook you are running will usually run on port 8888. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:

Output
[I NotebookApp] Serving notebooks from local directory: /home/sammy [I NotebookApp] 0 active kernels [I NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/ [I NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

If you are running Jupyter Notebook on a local Linux computer (not on a Droplet), you can simply navigate to localhost:8888 to connect to Jupyter Notebook. If you are running Jupyter Notebook on a Droplet, you will need to connect to the server using SSH tunneling as outlined in the next section.

此时,您可以保持 SSH 连接打开并保持 Jupyter Notebook 运行,也可以退出应用程序并在设置 SSH 隧道后重新运行它。让我们保持简单并停止 Jupyter Notebook 进程。一旦 SSH 隧道工作正常,我们将再次运行它。要停止 Jupyter Notebook 进程,请按CTRL+C, type Y,并击中ENTER确认。将显示以下内容:

Output
[C 12:32:23.792 NotebookApp] Shutdown confirmed [I 12:32:23.794 NotebookApp] Shutting down kernels

步骤 4 — 使用 SSH 隧道连接到服务器

在本节中,我们将学习如何使用 SSH 隧道连接到 Jupyter Notebook Web 界面。由于 Jupyter Notebook 运行在 Droplet 上的特定端口上(例如:8888, :8889等),SSH 隧道使您能够安全地连接到 Droplet 的端口。

接下来的两小节介绍如何从 1) Mac 或 Linux 和 2) Windows 创建 SSH 隧道。请参阅您本地计算机的小节。

与 Mac 或 Linux 建立 SSH 隧道

如果您使用的是 Mac 或 Linux,创建 SSH 隧道的步骤类似于如何使用 Linux 或 Mac 将 SSH 密钥与 DigitalOcean Droplet 结合使用指南中除了添加了其他参数外ssh命令。本小节将概述所需的附加参数ssh命令隧道成功。

可以通过运行以下 SSH 命令来完成 SSH 隧道:

  1. ssh -L 8000:localhost:8888 your_server_username@your_server_ip

The ssh command opens an SSH connection, but -L specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side (Droplet). This means that whatever is running on the second port number (i.e. 8888) on the Droplet will appear on the first port number (i.e. 8000) on your local computer. You should change 8888 to the port which Jupyter Notebook is running on. Optionally change port 8000 to one of your choosing (for example, if 8000 is used by another process). Use a port greater or equal to 8000 (ie 8001, 8002, etc.) to avoid using a port already in use by another process. server_username is your username (i.e. sammy) on the Droplet which you created and your_server_ip is the IP address of your Droplet. For example, for the username sammy and the server address 111.111.111.111, the command would be:

  1. ssh -L 8000:localhost:8888 sammy@111.111.111.111

如果运行后没有出现错误ssh -L命令,您可以运行 Jupyter Notebook:

  1. Jupyter笔记本

Now, from a web browser on your local machine, open the Jupyter Notebook web interface with http://localhost:8000 (or whatever port number you chose).

使用 Windows 和 Putty 进行 SSH 隧道

如果您使用的是 Windows,您还可以使用 Putty 轻松创建 SSH 隧道,如中所述如何在 DigitalOcean Droplet 上通过 PuTTY 使用 SSH 密钥(Windows 用户).

首先,输入服务器 URL 或 IP 地址作为主机名,如下所示:

Set Hostname for SSH Tunnel

Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. Enter the local port number to use to access Jupyter on your local machine. Choose 8000 or greater (ie 8001, 8002, etc.) to avoid ports used by other services, and set the destination as localhost:8888 where :8888 is the number of the port that Jupyter Notebook is running on. Now click the Add button, and the ports should appear in the Forwarded ports list:

Forwarded ports list

Finally, click the Open button to connect to the server via SSH and tunnel the desired ports. Navigate to http://localhost:8000 (or whatever port you chose) in a web browser to connect to Jupyter Notebook running on the server.

第 5 步 — 使用 Jupyter Notebook

本节介绍使用 Jupyter Notebook 的基础知识。此时,您应该已经运行了 Jupyter Notebook,并且您应该使用 Web 浏览器连接到它。 Jupyter Notebook 非常强大,具有很多功能。本节将概述一些帮助您开始使用笔记本的基本功能。 Jupyter Notebook 将自动显示其运行目录中的所有文件和文件夹。

要创建新的笔记本文件,请选择New > Python 2从右上角的下拉菜单中:

Create a new Python 2 notebook

这将打开一个笔记本。我们现在可以在单元格中运行 Python 代码或将单元格更改为 markdown。例如,通过单击更改第一个单元格以接受 MarkdownCell > 细胞类型 > Markdown从顶部导航栏。我们现在可以使用 Markdown 编写笔记,甚至可以将用 LaTeX 编写的方程式放在$$符号。例如,将其更改为 markdown 后,在单元格中键入以下内容:

# Simple Equation

Let us now implement the following equation:
$$ y = x^2$$

where $x = 2$

要将 Markdown 转换为富文本,请按CTRL+ENTER,结果应如下:

results of markdown

您可以使用 Markdown 单元格来做笔记并记录您的代码。让我们实现这个简单的方程并打印结果。选择Insert > 在下面插入单元格插入单元格并输入以下代码:

x = 2
y = x*x
print y

要运行代码,请按CTRL+ENTER。结果应该如下:

simple equation results

您现在可以像使用任何其他 Python 开发环境一样包含库并使用笔记本!

结论

恭喜!您现在应该能够使用在 Droplet 上运行的 Jupyter 笔记本使用 Markdown 编写可重现的 Python 代码和注释。要快速浏览 Jupyter Notebook,请选择Help > 用户界面导览从顶部导航菜单。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置 Jupyter Notebook 在 Ubuntu 16.04 上运行 IPython 的相关文章

随机推荐

  • 如何在 CentOS 7 上安装 Vagrant

    Vagrant是一个用于构建和管理虚拟机环境的开源命令行工具 默认情况下 Vagrant 可以在 VirtualBox Hyper V 和 Docker 之上配置计算机 其他提供商如 Libvirt KVM VMware 和 AWS 可以通
  • 如何在 Ubuntu 18.04 上安装和配置 Redis

    Redis 是一个开源的内存数据结构存储 它可以用作数据库 缓存和消息代理 并支持各种数据结构 如字符串 哈希 列表 集合等 Redis 通过 Redis Sentinel 提供高可用性 包括监控 通知 自动故障转移 它还通过 Redis
  • 如何在 Ubuntu 18.04 上安装 IntelliJ IDEA

    智能IDEA是一个功能齐全的 IDEJVM和安卓开发 它具有内置的调试支持 Docker and Docker 组合支持 嵌入式Git控制 集成了主要的构建自动化工具 例如Maven and Gradle 语法高亮 代码完成 ssh终端 代
  • 如何在 Debian 9 上安装 Tomcat 8.5

    Apache Tomcat 是一个开源应用程序服务器 支持 Java Servlet JavaServer Pages Java 表达式语言和 Java WebSocket 技术 它是当今世界上使用最广泛的应用程序和 Web 服务器之一 本
  • Linux 中的命令

    at是一个命令行实用程序 允许您安排在特定时间执行的命令 创建的职位at仅执行一次 在这篇文章中 我们将解释如何使用at及其配套实用程序batch atq atrm查看 删除和创建稍后执行的作业 安装中at 根据分布情况 at您的 Linu
  • 如何在 Ubuntu 18.04 上安装 Webmin

    Webmin是一个用于 Linux UNIX 系统管理的开源 Web 控制面板 Webmin 允许您管理用户 组 磁盘配额以及配置最流行的服务 包括 Web FTP 电子邮件和数据库服务器 在本教程中 我们将向您展示如何在 Ubuntu 1
  • 如何在 Ubuntu 20.04 上安装 Nvidia 驱动程序

    本文介绍如何在 Ubuntu 20 04 上安装 NVIDIA 驱动程序 如果您的 Ubuntu 机器有 NVIDIA GPU 您可以选择开源驱动程序Nouveau以及 NVIDIA 的专有驱动程序 默认情况下 Ubuntu 使用 Nouv
  • 如何列出和删除 UFW 防火墙规则

    UFW 代表简单防火墙 是一个用户友好的前端 用于管理 iptables netfilter 防火墙规则 它是默认的防火墙配置工具Ubuntu并且也可用于其他流行的 Linux 发行版 例如Debian和 Arch Linux 在本教程中
  • 如何在 Vue.js 中使用样式和类绑定

    介绍 在本文中 您将了解 Vue js 中的动态样式和类绑定 随着v bind style指令 您将在单击事件上可视化字体大小 和v bind class 您将观察如何将多个类应用于元素 虽然这可以通过 DOM 操作实现 但 Vue js
  • SQL 类似 - SQL 不类似

    SQL LIKE 与 WHERE 子句一起使用来搜索列的模式 通配符是用于指定模式的通配符 有两个通配符与 LIKE 运算符一起使用 用百分比来表示一次 多次或不出现的情况 下划线用于表示单个字符 To use SQL LIKE opera
  • Spring Security 基于角色的访问授权示例

    今天我们将研究 Spring Security 基于角色的访问和授权示例 然而 在阅读这篇文章之前 请先阅读我之前关于 Spring 4 Security MVC 登录注销示例 以获取有关 Spring 4 Security 的一些基本知识
  • 如何在 Python 3 中构造 For 循环

    在计算机编程中使用循环使我们能够自动化并多次重复类似的任务 在本教程中 我们将介绍 Pythonfor loop A for循环基于循环计数器或循环变量实现代码的重复执行 这意味着for当进入循环之前已知迭代次数时 最常使用循环 这与whi
  • Python静态方法

    Python静态方法 In this quick post we will learn how to create and use a Python static method We will also have a look at wha
  • 如何在 Ubuntu 14.04 上使用 Let's Encrypt 保护 HAProxy

    介绍 Let s Encrypt 是一个新的证书颁发机构 CA 它提供了一种获取和安装免费 TLS SSL 证书的简单方法 从而在 Web 服务器上启用加密的 HTTPS 它通过提供软件客户端 Certbot 来简化流程 该客户端尝试自动执
  • 如何在 Ruby 中使用数组

    介绍 An array是一个表示值列表的数据结构 称为elements 数组允许您在单个变量中存储多个值 在 Ruby 中 数组可以包含任何数据类型 包括数字 字符串和其他 Ruby 对象 这可以压缩和组织您的代码 使其更具可读性和可维护性
  • Python unittest - 单元测试示例

    今天我们将学习Python单元测试并浏览Python单元测试示例程序 在之前的教程中我们了解了python zip 函数 Python单元测试 Python unittest 模块用于测试源代码单元 假设 您需要测试您的项目 您知道该函数将
  • 如何在 Ubuntu VPS 上备份 PostgreSQL 数据库

    什么是 PostgreSQL PostgreSQL 是一个现代数据库管理系统 它经常用于存储和操作与网站和应用程序相关的信息 与任何类型的有价值的数据一样 实施备份方案以防止数据丢失非常重要 本指南将介绍一些备份 PostgreSQL 数据
  • 如何在 React 中构建自动完成组件

    介绍 自动完成是一种输入字段根据用户输入建议单词的功能 这有助于改善应用程序中的用户体验 例如需要搜索的情况 在本文中 您将研究如何在 React 中构建自动完成组件 您将使用固定的建议列表 事件绑定 理解键盘代码以及操作状态管理 先决条件
  • 如何使用 Nmap 扫描开放端口

    介绍 对于许多崭露头角的系统管理员来说 网络是一个广泛且令人难以承受的主题 有各种层 协议和接口 以及许多必须掌握才能理解它们的工具和实用程序 在 TCP IP 和 UDP 网络中 ports是逻辑通信的端点 单个 IP 地址可能运行许多服
  • 如何设置 Jupyter Notebook 在 Ubuntu 16.04 上运行 IPython

    介绍 IPython 是 Python 的交互式命令行界面 Jupyter Notebook 为多种语言提供了交互式 Web 界面 包括 IPython 本文将引导您设置服务器来运行 Jupyter Notebook 并教您如何连接和使用该