提交 8f52670d 编写于 作者: W wizardforcel

1.2

上级 1cddc478
# A first taste of Python tools
# Python 工具的初次尝试
## Launch a commandline app
## 加载命令行应用
Launch `Terminal.app` (Mac) or whatever the `bash` *terminal*, *shell*, or *commandline prompt* program is on your flavor of UNIX. You should see a blinking cursor and a `$` prompt:
启动`Terminal.app`(Mac)或任何`bash` *终端**shell*,它是 UNIX 风格的*命令行提示符*程序。你应该看到一个闪烁的光标和一个`$`提示符:
```bash
$
```
The `$` sign above is just the prompt that indicates the terminal is waiting for you to type something. After command executes, you will see the `$` prompt again.
上面的`$`符号只是提示,终端正在等待您输入内容。执行命令后,您将再次看到`$`提示符。
The commandline is a very low-level interface for communicating with the operating system of your computer. You can think of the terminal as the diagnostics computer that mechanics plug into your car to really take control. The dashboard is analogous to the windowing graphical interface we use most of the time. Becoming a programmer is like becoming a mechanic; sometimes you need more powerful but more complicated tools to operate machinery.
命令行是一个非常低级的接口,用于与计算机的操作系统进行通信。您可以将终端视为诊断计算机,机械师可以将其插入汽车中,来获取控制权。仪表板类似于我们大多数时候使用的窗口图形界面。成为一名程序员就像成为一名机械师;有时你需要更强大但更复杂的工具来操作机器。
You should more or less always have a command shell running in case you need to do something low-level. The first thing a mechanic does when he or she starts to work is to connect the diagnostic computer. So should you.
你应该或多或少总是运行一个命令行 shell,以防你需要做一些低级别的事情。机械师在他或她开始工作时所做的第一件事,就是连接诊断计算机。你也应该这样。
The command line is actually a full programming language with loops and everything but most of the time we simply execute commands. Commands have arguments just like function calls in a programming language have arguments. Here's how to say hello from the command line:
命令行实际上是一个完整的编程语言,包含循环和所有内容,但大多数时候我们只是执行命令。命令具有参数,就像编程语言中的函数调用具有参数一样。以下是如何在命令行打招呼:
```bash
$ echo "hello"
......@@ -22,27 +22,27 @@ hello
$
```
Commands are terminated by hitting the return key.
通过按返回键终止命令。
The `echo` command is analogous to the `print` command in Python code.
`echo`命令类似于 Python 代码中的`print`命令。
After executing the command, the prompt returns indicating you can type another command.
执行该命令后,提示符将返回,指示您可以键入另一个命令。
There are lots of things we can do; here's another one:
我们可以做很多事情; 这是另一个:
```bash
$ date
Wed Jul 12 14:18:51 PDT 2017
```
We can pass an argument (like a function call in Python) to commands:
我们可以将一个参数传递给命令(类似 Python 中的函数调用):
```bash
$ date "+%Y-%m-%d"
2018-07-08
```
Here's a cool example of a loop from the command line to examine all `.md` files (you don't need to run this one as part of the lab; I'm just showing off here):
这是一个很酷的例子,来自命令行的循环检查所有`.md`文件(你不需要在实验中运行这个文件;我只是在这里展示):
```bash
$ for f in *.md; do echo $f; done
......@@ -63,25 +63,25 @@ sqrt.md
```
## Environment Sanity check
## 环境健全性检查
在命令行中,键入以下命令以验证您是否可以访问`python3`
From the command line, type in the following command to verify that you have access to `python3`:
```bash
$ which python3
/Users/parrt/anaconda3/bin/python3
```
Or, better yet, make sure the default Python is version 3:
或者,更好的是,确保默认的 Python 是版本 3:
```bash
$ which python
/Users/parrt/anaconda3/bin/python
```
The Python program should be in the Anaconda `bin` (binary) directory, that holds all of the binary executables. If you don't see anaconda somewhere in the output from the `which` command, you need to look at the documentation for Anaconda and figure out why you are not running the appropriate Python. (Most likely something is wrong with your `PATH` environment variable.) That's okay for now as we can use any 3.X version of Python running.
Python 程序应该在 Anaconda`bin`(二进制)目录中,该目录包含所有二进制可执行文件。如果你没有在`which`命令的输出中看到 anaconda,你需要查看 Anaconda 的文档,并找出你没有运行适当的 Python 的原因。(很可能你的`PATH`环境变量有问题。)现在可以使用任何 3.X 版本的 Python 运行了。
Next, run the program and make sure you have a recent version like 3.6.5:
接下来,运行该程序并确保您有最新版本如 3.6.5:
```bash
$ python
......@@ -91,7 +91,7 @@ Type "help", "copyright", "credits" or "license" for more information.
>>>
```
Next try importing a library:
接下来尝试导入一个库:
```bash
$ python
......@@ -102,17 +102,17 @@ Type "help", "copyright", "credits" or "license" for more information.
>>>
```
You should not get an error.
你不应该得到错误。
See also [Computer environment sanity check](https://mlbook.explained.ai/intro.html#sec:2.2.1) from my book [The mechanics of machine learning](https://mlbook.explained.ai).
另请参阅我的书[机器学习机制](https://mlbook.explained.ai)中的[计算机环境健全性检查](https://mlbook.explained.ai/intro.html#sec:2.2.1)
## Interactive Python
## 交互式 Python
Now let's jump into an interactive Python shell. In the abstract, it's the same as the bash command shell that controls your computer. Both are programming languages; they just have different specialties. When we jump from the bash shell into Python's shell, it's like jumping across the border from France into Germany. We have to stop speaking French and start speaking German. (Or, keep speaking English if you're American because we're lazy and don't know any foreign languages. haha) When you exit the Python interpreter, you will be back in the bash world, just as if you had returned from Germany to France.
现在让我们跳转到交互式 Python shell。简单来说,它与控制计算机的 bash 命令行 shell 相同。两者都是编程语言;他们只是有不同的专长。当我们从 bash shell 跳到 Python 的 shell 时,它就像是从法国跳到德国边境。我们不得不停止说法语并开始讲德语。(或者,如果你是美国人,请继续说英语,因为我们很懒,不懂任何外语。哈哈)当你退出 Python 解释器时,你会回到 bash 世界,就像你从德国到法国。
To enter the Python world, from the bash `$` prompt type `python` as we did in the last section. You should see that it is using the **Anaconda** version. If not, that means you are using the default Python on your system.
要进入 Python 世界,请从 bash `$`提示符输入`python`,就像我们在上一节中所做的那样。您应该看到它正在使用**Anaconda** 版本。 如果没有,这意味着您正在使用系统上的默认 Python。
Now, from the Python prompt ">>>" (we are no longer in `bash`), type `500+1` followed by newline. You should see something like this:
现在,从 Python 提示符`>>>`(我们不再使用`bash`),键入`500 + 1`后跟换行符。你应该看到这样的东西:
```python
$ python
......@@ -124,50 +124,50 @@ Type "help", "copyright", "credits" or "license" for more information.
>>>
```
Python has evaluated the expression and printed the result back to the screen. It is as if we had used a `print` function call, which gives us the same result:
Python 已经求值了表达式并将结果打印回屏幕。好像我们使用了`print`函数调用,这给了我们相同的结果:
```python
>>> print(500+1)
501
```
The Python interactive shell prints expression values immediately, but that is not the case when running a program as a script (i.e., not interactively).
Python 交互式 shell 立即打印表达式值,但在将程序作为脚本运行时(即不是交互式)则不是这种情况。
To exit the interactive shell and go back to the command line (the terminal program), say `quit()` (or control-D) and hit return:
要退出交互式 shell 并返回命令行(终端程序),请键入`quit()`(或`ctrl-D`)并按下回车:
```bash
>>> quit()
$
```
The `$` prompt indicates that you are back at the bash command line. You can no longer type python code.
`$`提示符表示您回到了 bash 命令行。 您不能再键入 python 代码。
## Scripting Python
## 脚本化 Python
Go to a suitable directory (folder) on your disk, or create one, such as `/Users/YOURID/msan501/inclass`. (**Do not use spaces in any of your directory or file names...ever!**) Now create a **text file** called `hello.py` in that directory that contains exactly one line:
访问磁盘上合适的目录(文件夹),或创建一个,例如`/Users/YOURID/msan501/inclass`。(**不要在任何目录或文件名中使用空格......永远不要!**)现在在该目录中创建一个名为`hello.py`**文本文件,其中只包含一行:
```python
500+1
```
Use the editor of your choice, though `nano` is a good one because you will be able to use it on remote servers when we do cloud computing. Sublime and TextEdit.app also work.
使用您选择的编辑器,虽然`nano`是一个很好的编辑器,因为当我们进行云计算时,您将能够在远程服务器上使用它。 Sublime 和 TextEdit.app 也有效。
That "code" is exactly what you typed in first in the interactive Python shell. Save the file in your `inclass` directory or whatever your calling.
这个“代码”正是您在交互式 Python shell 中首先输入的内容。 将文件保存在`inclass`目录或任何调用目录中。
Once you get the Python file written to the disk using your editor, you should be able to jump to that directory using `cd` (change directory) from the commandline:
一旦使用编辑器将 Python 文件写入磁盘,您应该能够使用命令行中的`cd`(更改目录)跳转到该目录:
```bash
$ cd /Users/YOURID/msan501/inclass
```
Use `ls` to get a directory listing:
使用`ls`获取目录列表:
```bash
$ ls
hello.py
```
You verify that you have created the Python script properly by typing the following at the command line:
通过在命令行键入以下内容,验证是否已正确创建 Python 脚本:
```bash
$ cat hello.py
......@@ -175,27 +175,27 @@ $ cat hello.py
$
```
Here are solutions to the most common errors:
以下是最常见错误的解决方案:
1. Do not put `.txt` at the end of the file name; it must be `.py`
2. Do NOT use M$ Word or any other word processor; You think it's text but it's not. There are lots of text editors out there including Mac's `TextEdit.app`. Just make sure save as plain text not "rich text". There are also plenty of text editor such as [Sublime](https://www.sublimetext.com/) and [TextMate](https://macromates.com/). (If you are really hard-core, you will learn `vi` or `emacs`, which you will see me use in class.) You can also use `nano` from the command line for editing directly in the command line window.
1. 不要将`.txt`放在文件名的末尾;它必须是`.py`
2. 不要使用 MS Word 或任何其他文字处理器;你认为它是文本,但事实并非如此。有很多文本编辑器,包括 Mac 的`TextEdit.app`。只需确保保存为纯文本而不是“富文本”。还有很多文本编辑器,如 [Sublime](https://www.sublimetext.com/)[TextMate](https://macromates.com/)。(如果你真的很硬核,你将学习`vi``emacs`,你会看到我在课堂上使用它。)你也可以在命令行中使用`nano`,直接在命令行窗口中进行编辑。
Now, we're going to run that program/script:
现在,我们将运行该程序/脚本:
```bash
$ python hello.py
$
```
We do not get any output. This is a critical difference. The interactive Python shell immediately prints expression values because it is interactive. When you run a file from the commandline, it assumes you wanted to execute the code like a script in batch mode. That is why we do not get any output without a print statement.
我们没有任何输出。这是一个至关重要的区别。交互式 Python shell 会立即打印表达式值,因为它是交互式的。当您从命令行运行文件时,它假定您希望像脚本一样,以批处理模式执行代码。这就是为什么我们没有`print`语句就得不到任何输出。
Now edit that file and change it to
现在编辑该文件并将其更改为:
```python
print(500+1)
```
Save the file and rerun it. Now you should see:
保存文件并重新运行。 现在您应该看到:
```bash
$ python hello.py
......@@ -203,11 +203,11 @@ $ python hello.py
$
```
## Jupyter notebooks (via Jupyter Lab)
## Jupyter 笔记本(通过 Jupyter Lab)
Now, we're going to do the exact same thing except using the Jupyter Lab browser-based environment. (See [Your machine learning development environment](https://mlbook.explained.ai/prep.html#sec:3.1) from the [in-progress ML book](https://mlbook.explained.ai) for more.)
现在,除了使用基于 Jupyter Lab 浏览器的环境之外,我们将做同样的事情。 (请参阅[正在编写的 ML 书](https://mlbook.explained.ai)中的[您的机器学习开发环境](https://mlbook.explained.ai/prep.html#sec:3.1)来了解更多信息。)
Launch Jupyter from the command line::
从命令行启动 Jupyter:
```bash
$ jupyter lab
......@@ -218,16 +218,16 @@ I 11:27:00.606 LabApp] [jupyter_nbextensions_configurator] enabled 0.2.8
...
```
That will start up a program that launches a tab in your browser as well:
这将启动一个程序,在您的浏览器中启动一个选项卡:
<img src="../notes/img/lab1.png" width="60%">
Clicking on the "Python 3" icon under the "Notebook" category creates a new notebook window for you:
单击`Notebook`类别下的`Python 3`图标,可为您创建一个新的笔记本窗口:
<img src="../notes/img/lab2.png" width="80%">
Type `500+1` into the first cell next to the `In [ ]`. You should see the output 501 generated in the `Out` section below it. Hit control-enter to execute that cell or the right-facing triangle in the toolbar to execute the cell.
`In []`旁边的第一个单元格中键入`500 + 1`。您应该看到在它下面的`Out`部分生成的输出 501。按`control-enter`,或工具栏中的右三角形来执行该单元格。
This is an interactive environment and so you can go back and edit the `500+1` to be, say, `print(500+1)`. Do that and then hit control-enter again to run. You should get the same output.
这是一个交互式环境,所以你可以返回去编辑`500 + 1`,比如说`print(500 + 1)`。 这样做,然后再次点击`control-enter`来运行。 你应该得到相同的输出。
**You should be able to test out small programs or Python snippets very very quickly. Rehearse these procedures until they are second nature.**
\ No newline at end of file
**您应该能够非常快速地测试 Python 小程序或代码段。重复这些过程,直到它们成为第二本能。**
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册