[英]How do I get linux to automatically run my python script in the Python interpreter?
I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. 我已经决定,移出.NET泡沫并开始尝试其他技术对我有好处。 I have Ubuntu12 running and python2.7 and 3.2 are installed. 我运行了Ubuntu12并安装了python2.7和3.2。 I can run code directly in the interpreters. 我可以直接在解释器中运行代码。
I have a basic script on the filesystem called Standalone.py: 我在文件系统上有一个名为Standalone.py的基本脚本:
#!/usr/bin/env python3.2
import sys
print("this is a standalone script.")
When I'm at my bash prompt I type $ python3.2 Standalone.py
. 当我在我的bash提示符下输入$ python3.2 Standalone.py
。 I get a response saying this is a standalone script
. 我得到一个回复说this is a standalone script
。 But when I type $ Standalone.py
then it tells me that the command is not found. 但是当我输入$ Standalone.py
它会告诉我找不到命令。
How do I run such scripts? 我该如何运行这样的脚本?
Thanks for any help. 谢谢你的帮助。
I changed the permissions of Standalone.py to 755. Then I ran the command: 我将Standalone.py的权限更改为755.然后我运行了命令:
$ ./Standalone.py
and received the message: 并收到消息:
: No such file or directory
I then switched the permissions of Standalone.py back to 644. Then when I ran 然后我将Standalone.py的权限切换回644.然后我跑了
$ ./Standalone.py
I received the message 我收到了这条消息
-bash: ./Standalone.py: Permission denied
Is there something I'm missing? 有什么我想念的吗?
You need to make the script executable using 您需要使用可执行脚本
chmod +x Standalone.py
Usually, the current directory is not searched for executable files, so you need to use 通常,不会搜索当前目录中的可执行文件,因此您需要使用
./Standalone.py
to tell the shell that the script is in the current directory. 告诉shell脚本在当前目录中。
Make sure your script file has linux newline (just \\n
) not windows newline ( \\r\\n
). 确保你的脚本文件有linux换行符(只是\\n
)而不是windows换行符( \\r\\n
)。 Did you write the script on windows? 你在Windows上编写脚本了吗? This happened to me once. 这发生在我身上一次。 You should check your editor settings. 您应该检查您的编辑器设置。
Your script should start with #!/usr/bin/python
not #!/usr/bin/env python3.2
你的脚本应该以#!/usr/bin/python
开头而不是#!/usr/bin/env python3.2
Make sure you're in the folder where your script is located you can check with ls
确保您位于脚本所在的文件夹中,您可以使用ls
进行检查
chmod +x Standalone.py
./Standalone.py
At first, to excecute a script it need to be executable. 首先,要执行脚本,它需要是可执行的。 So you either have to do a chmod +x $file or a chmod 0740 $file. 所以你要么做chmod + x $文件还是chmod 0740 $文件。 When you set the file permission to 644 you are putting the execute right away, so if gives you an error. 当您将文件权限设置为644时,您将立即执行,因此如果给您一个错误。 If you are unsure of execution right and octal notation, you can use this : http://permissions-calculator.org/decode/0644/ . 如果您不确定执行权和八进制表示法,可以使用: http : //permissions-calculator.org/decode/0644/ 。 To really answer your question then, if you want to call the script with $file.py it needs to be in your PATH variable. 要真正回答你的问题,如果你想用$ file.py调用脚本,它需要在你的PATH变量中。 You can display it with echo $PATH. 您可以使用echo $ PATH显示它。 Those are the directories that are searched for script to execute. 这些是搜索要执行的脚本的目录。 So you simply need to give your script the executable right and put it in one of the directory given by your PATH. 因此,您只需要为脚本提供正确的可执行文件,并将其放在PATH提供的目录之一中。
Can you check if /usr/bin/python or /usr/bin/python3.2 exists 你能检查/ usr / bin / python或/usr/bin/python3.2是否存在
Execute below comamnd: 执行以下comamnd:
which python3.2
and then use the resulting path on top of you script. 然后在脚本顶部使用生成的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.