Shebang 不适用于 python3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22222473/
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
Shebang doesn't work with python3
提问by zer0uno
I have the following program:
我有以下程序:
#!/usr/local/bin/python3
print("Hello")
Via terminal I do test.pyand I get:
通过终端我做test.py,我得到:
Traceback (most recent call last):
File "/usr/lib/python3.3/site.py", line 629, in <module>
main()
File "/usr/lib/python3.3/site.py", line 614, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python3.3/site.py", line 284, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python3.3/site.py", line 260, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python3.3/site.py", line 250, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python3.3/sysconfig.py", line 610, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python3.3/sysconfig.py", line 560, in get_config_vars
_init_posix(_CONFIG_VARS)
File "/usr/lib/python3.3/sysconfig.py", line 432, in _init_posix
from _sysconfigdata import build_time_vars
File "/usr/lib/python3.3/_sysconfigdata.py", line 6, in <module>
from _sysconfigdata_m import *
ImportError: No module named '_sysconfigdata_m'
Instead if I type python3 test.pyit works, I get:
相反,如果我输入python3 test.py它有效,我会得到:
Hello
Hello
P.S. which python3----> /usr/local/bin/python3
附which python3言---->/usr/local/bin/python3
回答by DomTomCat
Generally, take care of some pitfalls:
一般来说,要注意一些陷阱:
- set the executable flagon the script:
chmod u+x test.py - try to execute with a preceding dot"./", so call
./test.pyotherwise it might execute some other script from within yourPATH also make sure you don't have windows line endings, this seems to prevent the shebang evaluation, too. There are some suggestions around, e.g. in this answer, on how to convert the format.
If
python3 test.pyworks, then the windows line endings are probably your problem.#!/usr/bin/env python3is the best way to define the shebang, since the python binary may be installed somewhere else.envwill inspect thePATHenvironment to find the binary
- 在脚本上设置可执行标志:
chmod u+x test.py - 尝试使用前面的点“./”
./test.py执行,因此调用否则它可能会从您的内部执行一些其他脚本PATH 还要确保你没有 windows 行尾,这似乎也阻止了 shebang 评估。有一些关于如何转换格式的建议,例如在这个答案中。
如果
python3 test.py有效,那么 Windows 行尾可能是您的问题。#!/usr/bin/env python3是定义shebang的最佳方式,因为python二进制文件可能安装在其他地方。env将检查PATH环境以找到二进制文件
EDIT: The OP's kind of error looks like windows line endings to me. I've had them, too, with different output though
编辑:OP 的错误类型对我来说看起来像 windows 行结尾。我也有它们,但输出不同
回答by jfs
You might see ImportError: No module named '_sysconfigdata_m'because /usr/lib/command-not-foundis broken on your system due to the ubuntu bug.
您可能会看到ImportError: No module named '_sysconfigdata_m'因为ubuntu 错误/usr/lib/command-not-found导致系统损坏。
To workaround it, run ./test.py, not test.py-- the current directory is not in $PATHusually (due to security reasons) and therefore you should specify the path explicitly otherwise the command is not found that may lead to trying to run /usr/lib/command-not-foundthat results in the ImportError.
要解决此问题,请运行./test.py,而不是test.py- 当前目录$PATH通常不在(出于安全原因),因此您应该明确指定路径,否则找不到可能导致尝试运行/usr/lib/command-not-found导致ImportError.
If ./test.pyfails with the same error then check that there is no '\r\v\f'(unexpected whitespace) in the shebang(print(repr(open('test.py', 'rb').readline()))). If test.pyuses Windows newlines then the attempt to find '/usr/local/bin/python3\r'(notice: '\r'due to '\r\n'newline) is likely to fail that may trigger the error.
如果./test.py失败并出现相同的错误,则检查'\r\v\f'shebang( print(repr(open('test.py', 'rb').readline()))) 中是否没有(意外的空格)。如果test.py使用 Windows 换行符,则尝试查找'/usr/local/bin/python3\r'(注意:'\r'由于'\r\n'换行符)可能会失败,这可能会触发错误。

