简体   繁体   English

Ubuntu Python shebang线不起作用

[英]Ubuntu Python shebang line not working

Unable to get shebang line working in Ubuntu for python script.无法让 shebang 行在 Ubuntu 中为 python 脚本工作。 I only get a command not found error each time.我每次只得到一个命令未找到错误。

test.py测试.py

#!/usr/bin/env python

print ('!')

Ran

:which python
/usr/bin/python

Played around with different locations for python in the shebang but no luck including what was provided by which python.在shebang中玩了python的不同位置,但没有运气,包括哪个python提供的东西。 Any tips on how to troubleshoot this?有关如何解决此问题的任何提示?

Thanks谢谢

If you are trying to run the command as如果您尝试将命令运行为

$ test.py

the error may not have anything to do with the shebang.该错误可能与shebang没有任何关系。 Rather, the directory that test.py resides in is not in your PATH .相反, test.py 所在的目录不在您的PATH中。 Try尝试

$ ./test.py

to bypass PATH lookup.绕过PATH查找。

(This is in addition to making sure that the script itself is executable.) (这是为了确保脚本本身是可执行的。)

On the python docs page it says:在 python文档页面上它说:

To easily use Python scripts on Unix, you need to make them executable, eg with要在 Unix 上轻松使用 Python 脚本,您需要使它们可执行,例如

$ chmod +x script and put an appropriate Shebang line at the top of the script. $ chmod +x 脚本并在脚本顶部放置适当的 Shebang 行。 A good choice is usually一个好的选择通常是

#!/usr/bin/env python which searches for the Python interpreter in the whole PATH. #!/usr/bin/env python 在整个 PATH 中搜索 Python 解释器。 However, some Unices may not have the env command, so you may need to hardcode /usr/bin/python as the interpreter path.但是,某些 Unices 可能没有 env 命令,因此您可能需要硬编码 /usr/bin/python 作为解释器路径。

I don't know if this applies for you or not.我不知道这是否适用于你。

Apart from executing the script with a preceding dot or making it executable, there might be another issue:除了使用前面的点执行脚本或使其可执行之外,可能还有另一个问题:

If you try to use a script written with a windows editor, it may contain windows line endings.如果您尝试使用用 windows 编辑器编写的脚本,它可能包含 windows 行尾。 Removing these can make the shebang work again.删除这些可以使 shebang 再次工作。

To remove such line endings, refer to How to convert Windows end of line in Unix end of line (CR/LF to LF) for instance.要删除此类行尾,请参阅How to convert Windows end of line in Unix end of line (CR/LF to LF)例如。

See also my general remarks on failed shebang evaluations at my other answer .另请参阅我在其他答案中对失败的 shebang 评估的一般评论。

Make sure that the " FIRST LINE " is the shebang.确保“第一行”是shebang。 Do not give any newline character in the beginning of the file.不要在文件的开头给出任何换行符。 " No newline character in beginning " 开头没有换行符

This may be due to a kernel misconfiguration.这可能是由于内核配置错误。 Take a look at your kernel's config options, and check if CONFIG_BINFMT_SCRIPT is set:查看内核的配置选项,并检查是否设置了CONFIG_BINFMT_SCRIPT

zcat /proc/config.gz | grep CONFIG_BINFMT_SCRIPT

If the output of this command is anything besides CONFIG_BINFMT_SCRIPT=y , this means that your kernel will not allow you to use shebangs.如果此命令的输出是CONFIG_BINFMT_SCRIPT=y以外的任何内容,这意味着您的内核将不允许您使用 shebangs。 You will need to get a new kernel or recompile your current kernel with CONFIG_BINFMT_SCRIPT=y .您将需要获得一个新内核或使用CONFIG_BINFMT_SCRIPT=y重新编译当前内核。

You can also check your line ending in gitbash/pycharm terminal by typing,您还可以通过键入来检查以 gitbash/pycharm 终端结尾的行,

cat .gitattributes猫 .git 属性

this will list the setup, for linux setup paste this *.sh text eol=lf这将列出设置,对于 linux 设置粘贴此 *.sh 文本 eol=lf

if this doesnt work, then better create a new branch and delete the exisiting file and create newfile.如果这不起作用,那么最好创建一个新分支并删除现有文件并创建新文件。

This worked for me这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM