# Ipython 解释器 ## 进入ipython 通常我们并不使用**Python**自带的解释器,而是使用另一个比较方便的解释器——**ipython**解释器,命令行下输入: ```py ipython ``` 即可进入**ipython**解释器。 所有在**python**解释器下可以运行的代码都可以在**ipython**解释器下运行: In [1]: ```py print "hello, world" ``` ```py hello, world ``` 可以进行简单赋值操作: In [2]: ```py a = 1 ``` 直接在解释器中输入变量名,会显示变量的值(不需要加`print`): In [3]: ```py a ``` Out[3]: ```py 1 ``` In [4]: ```py b = [1, 2, 3] ``` ## ipython magic命令 **ipython**解释器提供了很多以百分号`%`开头的`magic`命令,这些命令很像linux系统下的命令行命令(事实上有些是一样的)。 查看所有的`magic`命令: In [5]: ```py %lsmagic ``` Out[5]: ```py Available line magics: %alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics. ``` `line magic` 以一个百分号开头,作用与一行; `cell magic` 以两个百分号开头,作用于整个cell。 最后一行`Automagic is ON, % prefix IS NOT needed for line magics.`说明在此时即使不加上`%`也可以使用这些命令。 使用 `whos` 查看当前的变量空间: In [6]: ```py %whos ``` ```py Variable Type Data/Info ---------------------------- a int 1 b list n=3 ``` 使用 `reset` 重置当前变量空间: In [7]: ```py %reset -f ``` 再查看当前变量空间: In [8]: ```py %whos ``` ```py Interactive namespace is empty. ``` 使用 `pwd` 查看当前工作文件夹: In [9]: ```py %pwd ``` Out[9]: ```py u'C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01\. python tools' ``` 使用 `mkdir` 产生新文件夹: In [10]: ```py %mkdir demo_test ``` 使用 `cd` 改变工作文件夹: In [11]: ```py %cd demo_test/ ``` ```py C:\Users\lijin\Documents\Git\python-tutorial\01\. python tools\demo_test ``` 使用 `writefile` 将cell中的内容写入文件: In [12]: ```py %%writefile hello_world.py print "hello world" ``` ```py Writing hello_world.py ``` 使用 `ls` 查看当前工作文件夹的文件: In [13]: ```py %ls ``` ```py 驱动器 C 中的卷是 System 卷的序列号是 DC4B-D785 C:\Users\lijin\Documents\Git\python-tutorial\01\. python tools\demo_test 的目录 2015/09/18 11:32