【问题标题】:why does a simple Python/Haskell/etc program freeze/hang when doing an import?为什么一个简单的 Python/Haskell/etc 程序在导入时会冻结/挂起?
【发布时间】:2018-04-09 07:23:44
【问题描述】:

为什么这个 Python 程序会冻结/挂起:

$ ls -l freeze.py
-rwx------ 1 rick rick 24 Oct 27 11:40 freeze.py
$ cat freeze.py
import re
print "hello"
$ ./freeze.py
  C-c

为什么这个 Haskell 程序也会冻结/挂起:

$ ls -l freeze.hs
-rwxrw-r-- 1 rick rick 46 Oct 27 11:22 freeze.hs
$ cat freeze.hs
import Text.Regex.Posix
main = print "hello"
$ ./freeze.hs
  C-c

【问题讨论】:

    标签: haskell scripting imagemagick sh python-import


    【解决方案1】:

    (我知道这是一个基本/新手错误。但我怀疑我是第一个或最后一个犯这个错误的人......所以我会在这里记录它,以便像我这样的未来笨蛋可以用谷歌搜索它。)

    这些脚本冻结是因为它们实际上是 shell 脚本。这些 shell 脚本实际上是在运行import 命令行程序...由ImageMagick 包提供:

    $ sh freeze.hs
      C-c
    $ which import
    /usr/bin/import
    $ man import | head -10
    ... import - saves any visible window on an X server and outputs it as an image file.
    $ import screenshot.ps
        ... and here notice the mouse icon changes to a cross-hair icon ...
        ... so then press the mouse button to finish this operation ...
    $ file screenshot.ps
    screenshot.ps: PostScript document text conforming DSC level 3.0, Level 1
    

    感谢 ImageMagick 工作人员提供了一个不错的、无声的命令行操作。

    至少在各种基于Linux的操作系统上,这些脚本触发import命令行程序的操作。 BSD、Windows、MacOS 等可能会有不同的响应。

    下面是使用 Python 解释器和ghc 编译器对脚本的正确操作:

    $ python freeze.py
    hello
    $ runghc freeze.hs
    "hello"
    

    或者,包括#! shebang

    $ ls -l no_freeze.py
    -rwx------ 1 rick rick 46 Oct 27 11:44 no_freeze.py
    $ cat no_freeze.py
    #!/usr/bin/env python
    import re
    print "hello"
    $ ./no_freeze.py
    hello
    

    Haskell 也是如此:

    $ ls -l no_freeze.hs
    -rwx------ 1 rick rick 68 Oct 27 11:26 no_freeze.hs
    $ cat no_freeze.hs
    #!/usr/bin/env runghc
    import Text.Regex.Posix
    main = print "hello"
    $ ./no_freeze.hs
    "hello"
    

    在我的 MacOS 计算机上,我收到此错误是因为我的 X11 设置的某些部分未完成。对于那些没有安装 X11 的人,我想你会得到 command not found 错误。

    $ import screenshot
    Version: ImageMagick 6.9.5-0 Q16 x86_64 2016-07-02 http://www.imagemagick.org
    ...
    import: delegate library support not built-in `' (X11) @ error/import.c/ImportImageCommand/1297.
    

    【讨论】:

    • 你找到在import语句前引入shell语法错误的方法了吗?我想在我的一些脚本中这样做。
    • 我想到了一些想法。首先,也许我们可以在~/.profile 文件中添加一些内容......配置文件可以访问后续脚本的内容(当不交互时)?其次,将sh 替换为首先检查脚本头部的import。但是,在我看来,最干净的解决方案是让import 命令输出一个文本提示,例如“单击鼠标按钮保存屏幕截图”。然后添加--quiet 标志以进行静默操作。实际上,这种混乱是 Python/Haskell 和 ImageMagick 的开始之间的一个很大的巧合。我可以想象其他语言的其他关键字/命令交互。
    • 没有任何脚本被执行用于非交互使用。使用 bash,您可以使用名为 import 的导出函数 (yuck) 来覆盖该命令。但是在这一点上,将~/bin 放在你的路径上并在那里有一个假的import 程序可能更容易。
    • 为什么要出现语法错误?如果您打算将它作为一个脚本运行,请在脚本顶部放置一个 shebang,以便您的 shell/OS 知道要使用什么解释器。
    • shell 确实提供了及时的错误信息;您只是在脚本中没有任何错误(或没有到达它们),因为您安装了一个名为 import 的程序。这是一个基本的操作问题:如果您想将脚本作为独立的可执行文件运行,它必须有一个 shebang 来指定要使用的正确解释器。
    猜你喜欢
    • 2016-12-06
    • 2022-01-23
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2011-11-13
    • 2016-06-22
    相关资源
    最近更新 更多