Python不允许使用env python附加注释 [英] Python does not allow additional comment with env python

查看:144
本文介绍了Python不允许使用env python附加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有一条注释行问题。我用vim创建了 ages.py ,这是我的脚本

I have a commenting line problem in python. I created a ages.py with vim and here is my script

#!/usr/bin/env python
ages={"dad":42, "mom":35, "lisa":7}
for item in ages:
        print item

当我在上方添加注释时!/ usr / bin ,例如

# this is a python script
#!/usr/bin/env python
ages={"dad":42, "mom":35, "lisa":7}
for item in ages:
        print item

,然后返回包含相同脚本的目录,并通过写入终端 ages.py来运行该脚本,但出现此错误

and after I go back to directory, which includes the same script, and run the script with writing to terminal ages.py but I get this error

$ ./ages.py
./ages.py: line 3: mom:35,: command not found
./ages.py: line 5: syntax error near unexpected token `print'
./ages.py: line 5: 'print item'

我知道当我删除#!/ usr / bin / env python时非常适合发表评论。但是,我想添加此行来运行脚本,只写其名称进行提示。否则,我必须每次都运行 python ages.py 来运行它,这是我的负担。

I know that when I remove the #!/usr/bin/env python line # works perfectly for commenting. However, I would like to add this line to run the script only writing its name to prompt. Otherwise, I have to write python ages.py every single time to run it, which I see as a burden.

第一个问题:我如何在仍然有'#!/ usr / bin / env python'行的情况下对 .py 脚本进行注释

1st Q: How can I comment to .py script with still having the '#!/usr/bin/env python' line

PS:我已经尝试过 '''在注释行之前和之后都行不通。

P.S: I've already tried """ ''' before and after comment line, it does not work either.

第二个问:是否可以运行<$ c在python环境中的$ c> .py 脚本吗?例如,我可以在octave中运行 .m 文件,只需将其名称写入命令行即可。我不知道python中是否有相同的方法?

2nd Q: Is there a way to run a .py script in python environment? For instance, I can run .m file in octave simply writing its name to command line. But I could not figure out if there is a same way in python?

PS:我知道问题是如此的简单,值得-1,但是,对于新的python用户,所有的答案都将提供非常有用的信息,谢谢大家,这是一个非常微妙的解决方案,但是没有它,我经常遇到错误

P.S: I know the questions are so easy and deserve -1, however, for new python users, the all answers will provide great information, thanks for all of them. It is so subtle solution but without it, I've constantly got the errors

推荐答案


  1. 包含#!的行!/ usr / bin / env pyth ,必须先在下面添加您的评论;

  1. the line containing #!/usr/bin/env python must be first, you can add your comments below;

是的,您可以这样做像这样: exec(open('yourscript.py')。read())-但是,有时结果可能与您的期望有所不同。

yes, you can do it like this: exec(open('yourscript.py').read()) - however, results might sometimes differ from your expectations.

这篇关于Python不允许使用env python附加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆