代码之家  ›  专栏  ›  技术社区  ›  Lucas

调用了错误的python解释器

  •  11
  • Lucas  · 技术社区  · 15 年前

    我更新了我的python解释器,但我认为旧的解释器仍然被调用。当我检查我得到的版本时:

    $ python -V
    Python 3.0.1
    

    但我相信老翻译仍在被呼叫中。当我运行命令时:

    python myProg.py
    

    脚本运行正常。但当我用命令调用它时

    ./myProg.py
    

    我收到错误消息:

    AttributeError: 'str' object has no attribute 'format'
    

    这显然是由于老口译员被叫了。我怎么修这个?我运行Mac OS X 10.5。是否与第一行有关:

    #!/usr/bin/python
    

    我刚开始学习Python,对解释语言不太熟悉,所以我不太确定会发生什么。

    编辑:哇,太快了。谢谢!

    6 回复  |  直到 15 年前
        1
  •  16
  •   mipadi    15 年前

    根据剧本的第一行, #!/usr/bin/python ,您正在调用python解释器 /usr/bin/python (很可能是Mac OS X附带的版本)。您必须将该路径更改为安装Python3解释器的路径(可能 /usr/local/bin/python /opt/local/bin/python ; 你可以把那行改成 #!/usr/bin/env python ,这将调用 python 在您的 PATH 变量(似乎是您安装的较新版本)。

        2
  •  6
  •   freespace    15 年前

    首先,推荐shebang线为:

    #!/usr/bin/env python
    

    这将确保在 ./foo.py 与从命令行调用python时调用的解释器相同。

    根据你的描述,我怀疑如果你这样做了:

    which python
    

    它不会给你 /usr/bin/python . 它还提供了其他一些东西,这就是Python3解释器的所在。您可以将shebang行修改为上面的行,或者用返回的路径替换python解释器的路径。 which .

        3
  •  3
  •   bbuser    15 年前

    尝试 which python . 我将告诉您在您的环境中使用的是哪个Python解释器。 如果不是 /usr/bin/python 就像剧本里的那样,你的怀疑被证实了。

        4
  •  3
  •   EvanK    15 年前

    很有可能是你所怀疑的,shebang线正在调用旧版本。您可能需要检查两件事:

    1)在/usr/bin/python的解释器是什么版本:

    /usr/bin/python -V
    

    2)您安装的python 3解释器在哪里:

    which python
    

    如果从命令行中得到正确的命令行,则将shebang行替换为:

    #!/usr/bin/env python
    

    附录: 您也可以用到python 3的符号链接替换旧版本的python,但请注意,任何主要的OS X更新(即:10.5.6到10.5.7)都可能会破坏这一点:

    sudo mv /usr/bin/python /usr/bin/python25
    sudo ln -s /path/to/python/3/python /usr/bin/python
    
        5
  •  2
  •   Nathaniel Flath    15 年前

    运行'which python'-如果这给出的答案与/usr/bin/python不同,请更改!/usr/bin/python改为使用该路径。

        6
  •  1
  •   Jonathan Leffler    15 年前

    提供一个Perl脚本来回答一个Python问题可能有点奇怪,但它对Python的工作方式和对Perl的工作方式一样。这是一个脚本,名为' fixin ,表示“修复解释器”。它将shebang行更改为当前路径的正确字符串。

    #!/Users/jleffler/perl/v5.10.0/bin/perl
    #
    #   @(#)$Id: fixin.pl,v 1.3 2003/03/11 21:20:08 jleffler Exp $
    #
    #   FIXIN: from Programming Perl
    #   Usage: fixin [-s] [file ...]
    
    # Configuration
    $does_hashbang = 1;     # Kernel recognises #!
    $verbose = 1;           # Verbose by default
    
    # Construct list of directories to search.
    @absdirs = reverse grep(m!^/!, split(/:/, $ENV{'PATH'}, 999));
    
    # Process command line arguments
    if ($ARGV[0] eq '-s')
    {
        shift;
        $verbose = 0;
    }
    die "Usage: $0 [-s] [file ...]\n" unless @ARGV || !-t;
    
    @ARGV = '-' unless @ARGV;
    
    # Process each file.
    FILE: foreach $filename (@ARGV)
    {
        open(IN, $filename) || ((warn "Can't process $filename: $!\n"), next);
        $_ = <IN>;
        next FILE unless /^#!/;     # Not a hash/bang file
    
        chop($cmd = $_);
        $cmd =~ s/^#! *//;
        ($cmd, $arg) = split(' ', $cmd, 2);
        $cmd =~ s!^.*/!!;
    
        # Now look (in reverse) for interpreter in absolute path
        $found = '';
        foreach $dir (@absdirs)
        {
            if (-x "$dir/$cmd")
            {
                warn "Ignoring $found\n" if $verbose && $found;
                $found = "$dir/$cmd";
            }
        }
    
        # Figure out how to invoke interpreter on this machine
        if ($found)
        {
            warn "Changing $filename to $found\n" if $verbose;
            if ($does_hashbang)
            {
                $_ = "#!$found";
                $_ .= ' ' . $arg if $arg ne '';
                $_ .= "\n";
            }
            else
            {
                $_ = <<EOF;
    :
    eval 'exec $found $arg -S \$0 \${1+"\$@"}'
        if \$running_under_some_shell;
    EOF
            }
        }
        else
        {
            warn "Can't find $cmd in PATH, $filename unchanged\n" if $verbose;
            next FILE;
        }
    
        # Make new file if necessary
        if ($filename eq '-') { select(STDOUT); }
        else
        {
            rename($filename, "$filename.bak") ||
                ((warn "Can't modify $filename"), next FILE);
            open(OUT, ">$filename") ||
                die "Can't create new $filename: $!\n";
            ($def, $ino, $mode) = stat IN;
            $mode = 0755 unless $dev;
            chmod $mode, $filename;
            select(OUT);
        }
    
        # Print the new #! line (or the equivalent) and copy the rest of the file.
        print;
        while (<IN>)
        {
            print;
        }
        close IN;
        close OUT;
    }
    

    代码是从原始camel手册(编程perl,第一版)中同名的脚本派生而来的。从那以后,这本书已经被黑客入侵了一点——应该再被黑客入侵一些。但我经常使用它——实际上,我只是将它从一个Mac复制到另一个Mac,由于我没有在第二个Mac上安装Perl5.10.0,所以我运行了:

    $ perl fixin fixin
    Changing fixin to /usr/bin/perl
    $
    

    因此,从私有安装Perl改为标准安装Perl。

    为读者做练习-用python重写脚本。