如何让linux在Python解释器中自动运行我的python脚本?

时间:2022-06-07 07:14:18

I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.

我已经决定,在我的.NET泡沫之外移动并开始尝试其他技术对我有好处。我运行了Ubuntu12并安装了python2.7和3.2。我可以直接在解释器中运行代码。

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. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.

当我在我的bash提示符下时,我输入$ python3.2 Standalone.py。我得到一个回复​​说这是一个独立的脚本。但是当我键入$ Standalone.py时,它告诉我找不到命令。

How do I run such scripts?

我该如何运行这样的脚本?

Thanks for any help.

谢谢你的帮助。

update

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?

有什么我想念的吗?

5 个解决方案

#1


9  

  1. You need to make the script executable using

    您需要使用可执行脚本

    chmod +x Standalone.py
    
  2. 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脚本在当前目录中。

#2


3  

Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.

确保你的脚本文件有linux换行符(只是\ n)而不是windows换行符(\ r \ n)。你在Windows上编写脚本了吗?这发生在我身上一次。您应该检查您的编辑器设置。

#3


2  

  1. Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2

    你的脚本应该以#!/ usr / bin / python开头而不是#!/ usr / bin / env python3.2

  2. Make sure you're in the folder where your script is located you can check with ls

    确保您位于脚本所在的文件夹中,您可以使用ls进行检查

  3. chmod +x Standalone.py

    chmod + x Standalone.py

  4. ./Standalone.py

#4


0  

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. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : 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. You can display it with 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.

首先,为了执行脚本,它需要是可执行的。所以你要么做chmod + x $文件还是chmod 0740 $文件。当您将文件权限设置为644时,您将立即执行,因此如果给您一个错误。如果您不确定执行权和八进制表示法,可以使用:http://permissions-calculator.org/decode/0644/。要真正回答你的问题,如果你想用$ file.py调用脚本,它需要在你的PATH变量中。您可以使用echo $ PATH显示它。这些是搜索要执行的脚本的目录。因此,您只需要为脚本提供正确的可执行文件,并将其放在PATH提供的目录中。

#5


0  

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.

然后在脚本顶部使用生成的路径。

#1


9  

  1. You need to make the script executable using

    您需要使用可执行脚本

    chmod +x Standalone.py
    
  2. 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脚本在当前目录中。

#2


3  

Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.

确保你的脚本文件有linux换行符(只是\ n)而不是windows换行符(\ r \ n)。你在Windows上编写脚本了吗?这发生在我身上一次。您应该检查您的编辑器设置。

#3


2  

  1. Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2

    你的脚本应该以#!/ usr / bin / python开头而不是#!/ usr / bin / env python3.2

  2. Make sure you're in the folder where your script is located you can check with ls

    确保您位于脚本所在的文件夹中,您可以使用ls进行检查

  3. chmod +x Standalone.py

    chmod + x Standalone.py

  4. ./Standalone.py

#4


0  

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. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : 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. You can display it with 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.

首先,为了执行脚本,它需要是可执行的。所以你要么做chmod + x $文件还是chmod 0740 $文件。当您将文件权限设置为644时,您将立即执行,因此如果给您一个错误。如果您不确定执行权和八进制表示法,可以使用:http://permissions-calculator.org/decode/0644/。要真正回答你的问题,如果你想用$ file.py调用脚本,它需要在你的PATH变量中。您可以使用echo $ PATH显示它。这些是搜索要执行的脚本的目录。因此,您只需要为脚本提供正确的可执行文件,并将其放在PATH提供的目录中。

#5


0  

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.

然后在脚本顶部使用生成的路径。