【问题标题】:The wrong python interpreter is called错误的python解释器被调用
【发布时间】:2010-10-28 14:21: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,对解释语言不是很熟悉,所以我不太清楚发生了什么。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试which python。我会告诉你在你的环境中使用了哪个 python 解释器。 如果不是脚本中的/usr/bin/python,那么你的怀疑就被证实了。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          首先推荐的shebang线路是:

          #!/usr/bin/env python
          

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

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

          which python
          

          它不会给你/usr/bin/python。它会给你一些别的东西,这就是 python 3 解释器所在的地方。您可以将您的 shebang 行修改为上述内容,或者将 python 解释器的路径替换为 which 返回的路径。

          【讨论】:

            【解决方案5】:

            您很可能怀疑 shebang 线路正在调用旧版本。您可能需要检查两件事:

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

            /usr/bin/python -V
            

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

            which python
            

            如果你从命令行得到正确的,那么用这个替换你的 shebang 行:

            #!/usr/bin/env python
            

            附录:您也可以将旧版本的 python 替换为指向 python 3 的符号链接,但请注意任何主要的 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
            

            【讨论】:

            • 用符号链接替换 ​​/usr/bin/python 可能不是最好的主意。一些 OS X 实用程序可能引用 /usr/bin/python,由于 Python 3K 不是 100% 向后兼容 Python 2.x,因此您冒着破坏其中一些实用程序的风险。
            • 是的,我建议不要乱用 OS X 附带的 Python。
            • 我特别建议不要将系统 python 替换为故意不兼容的版本(即 python 3.x)
            • @mipadi:你说“你冒着破坏其中一些实用程序的风险”......这不太正确......你有效地保证这些实用程序会破坏,这不仅仅是风险! python 3.x 的全部前提是破坏兼容性。
            【解决方案6】:

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

            #!/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 Book(“Programming Perl”,第一版)中的同名脚本。从那时起,这个副本已经被黑客入侵了一些 - 并且应该被黑客攻击更多。但我经常使用它——事实上,我只是将它从一台 Mac 复制到另一台,因为我没有在第二台安装 Perl 5.10.0,所以我跑了:

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

            从而从私有安装 Perl 更改为标准安装。

            读者练习 - 用 Python 重写脚本。

            【讨论】:

              猜你喜欢
              • 2014-05-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-12-16
              • 2019-08-30
              • 2011-09-21
              • 1970-01-01
              • 2015-06-14
              相关资源
              最近更新 更多