【问题标题】:Can python shell have some pre-input?python shell可以有一些预输入吗?
【发布时间】:2011-05-13 06:48:56
【问题描述】:

在 python shell 中测试时,我总是需要输入一些导入,例如:

Python 2.5.4 (r254:67916, Jun 24 2010, 15:23:27) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import sys
>>>import datetime

有人可以帮我自动完成这些吗?这意味着我运行了一些命令进入它已经为我导入的 python shell,还有一个 python shell 等待我继续输入命令。

谢谢。

【问题讨论】:

    标签: python linux command-line


    【解决方案1】:

    试试:

    python -i -c "import sys; import datetime;"
    

    更多信息:

    -i     : inspect interactively after running script; forces a prompt even
             if stdin does not appear to be a terminal; also PYTHONINSPECT=x
    

    &

    -c cmd : program passed in as string (terminates option list)
    

    【讨论】:

      【解决方案2】:

      使用要在启动期间执行的命令创建一个文件,并将环境变量PYTHONSTARTUP 设置为该文件的位置。然后交互式解释器将加载并执行该文件。见http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file

      在旁注中,您可能希望将ipython 视为在交互模式下工作时改进的 Python shell。

      【讨论】:

      • 启动时总是这样运行的,我有时只是想指定它,也许我的问题不清楚,但还是谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多