【问题标题】:Random ImageMagick error/"unexpected token" error on basic script that has neither基本脚本上的随机 ImageMagick 错误/“意外令牌”错误
【发布时间】:2020-06-09 18:06:24
【问题描述】:

现在,我正在从 bash 运行一个基本的 python 脚本,如下所示:

import sys

def main():
    sys.stdout.write("hi")

if __name__=="__main__":
    main()

当使用 python (py justdosomething.py) 运行时,它运行良好。当我尝试从 bash 运行它时,它会从 imagemagick 发出大量错误消息,尽管我没有在此文件中导入它:

Version: ImageMagick 6.9.4-1 Q16 x86_64 2016-05-11 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules 
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
Usage: import [options ...] [ file ]

Image Settings:
  -adjoin              join images into a single multi-image file
  -border              include window border in the output image
  -channel type        apply option to select image channels
  -colorspace type     alternate image colorspace
  -comment string      annotate image with comment
  -compress type       type of pixel compression when writing the image
  -define format:option
                       define one or more image format options
  -density geometry    horizontal and vertical density of the image
  -depth value         image depth
  -descend             obtain image by descending window hierarchy
  -display server      X server to contact
  -dispose method      layer disposal method
  -dither method       apply error diffusion to image
  -delay value         display the next image after pausing
  -encipher filename   convert plain pixels to cipher pixels
  -endian type         endianness (MSB or LSB) of the image
  -encoding type       text encoding type
  -filter type         use this filter when resizing an image
  -format "string"     output formatted image characteristics
  -frame               include window manager frame
  -gravity direction   which direction to gravitate towards
  -identify            identify the format and characteristics of the image
  -interlace type      None, Line, Plane, or Partition
  -interpolate method  pixel color interpolation method
  -label string        assign a label to an image
  -limit type value    Area, Disk, Map, or Memory resource limit
  -monitor             monitor progress
  -page geometry       size and location of an image canvas
  -pause seconds       seconds delay between snapshots
  -pointsize value     font point size
  -quality value       JPEG/MIFF/PNG compression level
  -quiet               suppress all warning messages
  -regard-warnings     pay attention to warning messages
  -respect-parentheses settings remain in effect until parenthesis boundary
  -sampling-factor geometry
                       horizontal and vertical sampling factor
  -scene value         image scene number
  -screen              select image from root window
  -seed value          seed a new sequence of pseudo-random numbers
  -set property value  set an image property
  -silent              operate silently, i.e. don't ring any bells 
  -snaps value         number of screen snapshots
  -support factor      resize support: > 1.0 is blurry, < 1.0 is sharp
  -synchronize         synchronize image to storage device
  -taint               declare the image as modified
  -transparent-color color
                       transparent color
  -treedepth value     color tree depth
  -verbose             print detailed information about the image
  -virtual-pixel method
                       Constant, Edge, Mirror, or Tile
  -window id           select window with this id or name

Image Operators:
  -annotate geometry text
                       annotate the image with text
  -colors value        preferred number of colors in the image
  -crop geometry       preferred size and location of the cropped image
  -encipher filename   convert plain pixels to cipher pixels
  -geometry geometry   preferred size or location of the image
  -help                print program options
  -monochrome          transform image to black and white
  -negate              replace every pixel with its complementary color 
  -repage geometry     size and location of an image canvas
  -quantize colorspace reduce colors in this colorspace
  -resize geometry     resize the image
  -rotate degrees      apply Paeth rotation to the image
  -strip               strip image of all profiles and comments
  -thumbnail geometry  create a thumbnail of the image
  -transparent color   make this color transparent within the image
  -trim                trim image edges
  -type type           image type

Miscellaneous Options:
  -debug events        display copious debugging information
  -help                print program options
  -list type           print a list of supported option arguments
  -log format          format of debugging information
  -version             print version information

By default, 'file' is written in the MIFF image format.  To
specify a particular image format, precede the filename with an image
format name and a colon (i.e. ps:image) or specify the image type as
the filename suffix (i.e. image.ps).  Specify 'file' as '-' for
standard input or output.
import: delegate library support not built-in `' (X11) @ error/import.c/ImportImageCommand/1297.

然后它给了我两条看似无关的错误消息:

./justdosomething.py: line 3: syntax error near unexpected token `('
./justdosomething.py: line 3: `def main():'

为什么从 bash 运行它会导致完全不相关且未使用的库引发错误?为什么“def main()”无法识别为命令?我在这里迷路了。

【问题讨论】:

  • 你用什么命令得到这个错误?您究竟是如何从 bash 运行它的?你是在 bash 上还是在 windows 上?
  • 你需要一个 shebang 作为第一行 #!/bin/env python#!/usr/bin/env python

标签: python python-3.x bash imagemagick


【解决方案1】:

将以下内容添加为脚本的第一行。

#!/usr/bin/env python

为什么从 bash 运行它会导致完全不相关且未使用的库引发错误?

bash 解释器不理解 Python 编程语言。相反,它调用 import 实用程序(由 ImageMagick 提供)。

由于sys 不是import(实用程序)的有效参数,它会写入stderr + 基本使用信息。

为什么“def main()”会无法识别为命令?

简单。那是 Python——不是 Bash 能理解的任何东西。您需要调用 Python 运行时来执行 Python 脚本。

【讨论】:

  • 我必须运行 usr/bin/env python,但这有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-08-11
  • 2018-10-07
  • 1970-01-01
  • 2018-03-29
相关资源
最近更新 更多