使用交互式 shell 来增强你的 Python
Python shell
![](/data/attachment/album/201805/19/205244xkk5ilwhk7yt5h77.jpg)


Python 编程语言已经成为 IT 中使用的最流行的语言之一。成功的一个原因是它可以用来解决各种问题。从网站开发到数据科学、机器学习到任务自动化，Python 生态系统有丰富的框架和库。本文将介绍 Fedora 软件包集合中提供的一些有用的 Python shell 来简化开发。


### Python Shell


Python Shell 让你以交互模式使用解释器。这在测试代码或尝试新库时非常有用。在 Fedora 中，你可以通过在终端会话中输入 `python3` 来调用默认的 shell。虽然 Fedora 提供了一些更高级和增强的 shell。


### IPython


IPython 为 Python shell 提供了许多有用的增强功能。例如包括 tab 补全，对象内省，系统 shell 访问和命令历史检索。许多功能也被 [Jupyter Notebook](https://ipython.org/notebook.html) 使用，因为它底层使用 IPython。


#### 安装和运行 IPython



```
dnf install ipython3
ipython3

```

使用 tab 补全会提示你可能的选择。当你使用不熟悉的库时，此功能会派上用场。


![](/data/attachment/album/201805/19/205245zpvxpnudxp7e7nie.png)


如果你需要更多信息，输入 `?` 命令来查看文档。对此的更多详细信息，你可以使用 `??` 命令。


![](/data/attachment/album/201805/19/205245l6gub53b6me75je7.png)


另一个很酷的功能是使用 `!` 字符执行系统 shell 命令的能力。然后可以在 IPython shell 中引用该命令的结果。


![](/data/attachment/album/201805/19/205245tzarh4ah1wdhvtvh.png)


IPython 完整的功能列表可在[官方文档](https://ipython.readthedocs.io/en/stable/overview.html#main-features-of-the-interactive-shell)中找到。


### bpython


bpython 并不能像 IPython 做那么多，但它却在一个简单的轻量级包中提供了一系列有用功能。除其他功能之外，bpython 提供：


* 内嵌语法高亮显示
* 在你输入时提供自动补全建议
* 可预期的参数列表
* 能够将代码发送或保存到 pastebin 服务或文件中


#### 安装和运行 bpython



```
dnf install bpython3
bpython3

```

在你输入的时候，`bpython` 为你提供了选择来自动补全你的代码。


![](/data/attachment/album/201805/19/205246tahcccaacaobv0hh.png)


当你调用函数或方法时，会自动显示需要的参数和文档字符串。


![](/data/attachment/album/201805/19/205246gafxpokpcpd3doao.png)


另一个很好的功能是可以使用功能键 `F7` 在外部编辑器（默认为 Vim）中打开当前的 `bpython` 会话。这在测试更复杂的程序时非常有用。


有关配置和功能的更多细节，请参考 bpython [文档](https://docs.bpython-interpreter.org/)。


### 总结


使用增强的 Python shell 是提高生产力的好方法。它为你提供增强的功能来编写快速原型或尝试新库。你在使用增强的 Python shell 吗？请随意在评论区留言。


图片由 [David Clode](https://unsplash.com/photos/d0CasEMHDQs?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) 在 [Unsplash](https://unsplash.com/search/photos/python?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) 上发布




---


via: <https://fedoramagazine.org/enhance-python-interactive-shell/>


作者：[Clément Verna](https://fedoramagazine.org/author/cverna/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The Python programming language has become one of the most popular languages used in IT. One reason for this success is it can be used to solve a variety of problems. From web development to data science, machine learning to task automation, the Python ecosystem is rich in popular frameworks and libraries. This article presents some useful Python shells available in the Fedora packages collection to make development easier.


## Python Shell

The Python Shell lets you use the interpreter in an interactive mode. It’s very useful to test code or try a new library. In Fedora you can invoke the default shell by typing *python3* in a terminal session. Some more advanced and enhanced shells are available to Fedora, though.

## IPython

IPython provides many useful enhancements to the Python shell. Examples include tab completion, object introspection, system shell access and command history retrieval. Many of these features are also used by the [Jupyter Notebook](https://ipython.org/notebook.html) , since it uses IPython underneath.

### Install and run IPython

dnf install ipython3 ipython3

Using tab completion prompts you with possible choices. This features comes in handy when you use an unfamiliar library.

If you need more information, use the documentation by typing the *?* command. For more details, you can use the *??* command.

Another cool feature is the ability to execute a system shell command using the *!* character. The result of the command can then be referenced in the IPython shell.

A comprehensive list of IPython features is available in the [official documentation](https://ipython.readthedocs.io/en/stable/overview.html#main-features-of-the-interactive-shell).

## bpython

bpython doesn’t do as much as IPython, but nonetheless it provides a useful set of features in a simple and lightweight package. Among other features, bpython provides:

- In-line syntax highlighting
- Autocomplete with suggestions as you type
- Expected parameter list
- Ability to send or save code to a pastebin service or file

### Install and run bpython

dnf install bpython3 bpython3

As you type, bpython offers you choices to autocomplete your code.

When you call a function or method, the expected parameters and the docstring are automatically displayed.

Another neat feature is the ability to open the current bpython session in an external editor (*Vim* by default) using the function key *F7*. This is very useful when testing more complex programs.

For more details about configuration and features, consult the bpython [documentation](https://docs.bpython-interpreter.org/).

## Conclusion

Using an enhanced Python shell is a good way to increase productivity. It gives you enhanced features to write a quick prototype or try out a new library. Are you using an enhanced Python shell? Feel free to mention it in the comment section below.

*Photo by David Clode on Unsplash*

## sabino

great suggestions !!!

I learned something new.

Thank you

## Dan

Have you seen xonsh (http://xonsh.org/) ? It’s a shell with the power of Python, very cool.

(I’m trying to use it as default shell, but I’m stuck when using it in x2go).

## Clément Verna

I did, I try it a while ago and thought it was still a little bit too fresh. Maybe I should give a go again 🙂

## Apostolos

ptipython (uses ipython) also offers handy features.

## Jon Hannah

Another great Python 3+ light weight IPython-style REPL is hiss. See https://github.com/sixninetynine/hiss but in pip too!

## Cyril

Just…the image is that of an arboreal pit viper (Tropidolaemus Waglerii?), not a python 😉

## crossingtheair.wordpress.com

Yes, you’re right, that’s not a python.