调用了错误的 Python 解释器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/904170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:05:49  来源:igfitidea点击:

The wrong python interpreter is called

pythonpython-3.x

提问by Lucas

I updated my python interpreter, but I think the old one is still called. When I check for the version I get:

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

$ python -V
Python 3.0.1

But I believe the old interpreter is still being called. When I run the command:

但我相信旧的解释器仍在被调用。当我运行命令时:

python myProg.py

The script runs properly. But when I invoke it with the command

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

./myProg.py

I get the error message:

我收到错误消息:

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

Which apparently is due to the old interpreter being called. How can I fix this? I run Mac OS X 10.5. Has it something to do with the first line:

这显然是由于调用了旧的解释器。我怎样才能解决这个问题?我运行 Mac OS X 10.5。是否与第一行有关:

#!/usr/bin/python

I just started out with python and am not very familiar with interpreted languages, so I am not too sure what is going on.

我刚开始使用 python,对解释型语言不是很熟悉,所以我不太确定发生了什么。

Edit: Wow, that was quick. Thanks a lot!

编辑:哇,这很快。非常感谢!

回答by mipadi

According to the first line of the script, #!/usr/bin/python, you are calling the Python interpreter at /usr/bin/python(which is most likely the one that ships with Mac OS X). You have to change that path to the path where you installed your Python 3 interpreter (likely /usr/local/bin/pythonor /opt/local/bin/python); oryou can just change that line to read #!/usr/bin/env python, which will call the pythonlisted first in your PATHvariable (which seems to be the newer version you installed).

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

回答by freespace

Firstly, the recommended shebang line is:

首先推荐的shebang线是:

#!/usr/bin/env python

This will make sure the python interpreter that is invoked when you ./foo.pyis the same interpreter that is invoked when you invoke python from the command line.

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

From your description, I suspect that if you did:

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

which python

It would not give you /usr/bin/python. It would give you something else, which is where the python 3 interpreter lives. You can either modify your shebang line to the above, or replace the path to the python interpreter with the path returned by which.

它不会给你/usr/bin/python。它会给你其他东西,这就是 python 3 解释器所在的地方。您可以将 shebang 行修改为上述内容,也可以将 python 解释器的路径替换为which.

回答by EvanK

It's very possibly what you suspect, that the shebang line is calling the older version. Two things you might want to check:

您很可能怀疑,shebang 行调用的是旧版本。您可能需要检查两件事:

1) what version is the interpreter at /usr/bin/python:

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

/usr/bin/python -V

2) where is the python 3 interpreter you installed:

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

which python

If you get the correct one from the command line, then replace your shebang line with this:

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

#!/usr/bin/env python

Addendum:You could also replace the older version of python with a symlink to python 3, but beware that any major OS X updates (ie: 10.5.6 to 10.5.7) will likely break this:

附录:您还可以使用指向 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

回答by bbuser

Try which python. I will tell you which python interpreter is used in your environment. If it is not /usr/bin/pythonlike in the script, then your suspicion is confirmed.

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

回答by Nathaniel Flath

run 'which python' - if this gives a different answer than /usr/bin/python, change #!/usr/bin/python to have that path instead.

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

回答by Jonathan Leffler

It may be a bit odd providing a Perl script to answer a Python question, but it works for Python just as well as it does for Perl. This is a script called 'fixin', meaning 'fix interpreter'. It changes the shebang line to the correct string for your current PATH.

提供 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: 
$ perl fixin fixin
Changing fixin to /usr/bin/perl
$
[-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; }

The code is derived from a script of the same name in the original Camel Book ('Programming Perl', first edition). This copy has been hacked a bit since then - and should be hacked some more. But I use it routinely -- indeed, I just copied it from one Mac to another, and since I've not installed Perl 5.10.0 on the second, I ran:

该代码源自原始 Camel Book('Programming Perl',第一版)中的同名脚本。此副本从那时起已被黑客入侵了一些 - 应该更多地被黑客入侵。但我经常使用它——事实上,我只是将它从一台 Mac 复制到另一台 Mac,由于我没有在第二台安装 Perl 5.10.0,我运行:

##代码##

Thereby changing from the private install Perl to the standard one.

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

Exercise for the reader - rewrite the script in Python.

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