IPython 的问题

Issue with IPython

python_test.py 文件中,我插入了:

def my_contains(elem, lst):
    return elem in lst
def my_first(lst):
    return lst[0]

import IPython
IPython.embed()

执行python3 python_test.py后,我得到:

Traceback (most recent call last):
  File "python_test.py", line 6, in <module>
    import IPython
ImportError: No module named 'IPython'

事实上,我希望 shell 在执行我的代码后能够保持打开状态,以便我可以测试该代码。在这一点上有人可以帮助我吗?

来自手册页:

-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception.

我按如下方式测试了您的代码,它对我有用:

python3 -i python_test.py
>>> my_first([3, 2, 1])
3
>>> my_contains(2, [1, 10, 100])
False
>>> my_contains(1, [2, 1, 3])
True

这个答案几乎是逐字复制自

至于IPython,好像是找不到模块,表示没有安装,安装错误,不知道在哪里找,名字不对,或者是使用不当。我在此处 http://ipython.readthedocs.io/en/stable/install/index.html.

进行了安装

安装后,我干脆用ipython代替了python3来加载文件,然后输入一个交互式的shell:

@WillemVanOnsem 在上面的评论中提出了一个很好的观点,即可能需要使用 pip3 而不是 pip。对我来说,pip 工作正常并且安装正确。

pip install ipython
ipython -i python_test.py
Python 3.5.1 (default, Apr 18 2016, 11:46:32)

In [1]: my_contains(1, [1, 2, 3])
Out[1]: True

In [2]: my_first([1, 2, 3])
Out[2]: 1