How to get linux to automatically run my python script in a Python interpreter?

I decided that it would be useful for me to go beyond my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run the code directly in the interpreters.

I have a basic script in the Standalone.py file system:

#!/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 an answer saying this is a standalone script . But when I type $ Standalone.py , then it tells me that the command was not found.

How to run such scripts?

Thanks for any help.

Update

I changed the permissions of Standalone.py to 755. Then I ran the command:

 $ ./Standalone.py 

and received a message:

 : No such file or directory 

Then I switched the permissions of Standalone.py back to 644. Then, when I started

 $ ./Standalone.py 

I got a message

 -bash: ./Standalone.py: Permission denied 

Is there something I am missing?

+6
source share
5 answers
  • You need to execute the script executable with

     chmod +x Standalone.py 
  • Usually the current directory does not look for executables, so you need to use

     ./Standalone.py 

    to tell the shell that the script is in the current directory.

+9
source

Make sure your script file has a new linux line (only \n ) and not a new line ( \r\n ). Did you write a script on windows? It happened to me once. You should check your editor settings.

+3
source
  • Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2

  • Make sure you are in the folder where your script is located, which you can check with ls

  • chmod +x Standalone.py

  • ./Standalone.py

+2
source

First, to exclude a script, it must be executable. Thus, you need to either make a chmod + x $ file or a chmod 0740 $ file. When you set the resolution of the file to 644, you immediately execute it, so if you get an error message. If you are unsure of the correct execution and octal notation, you can use this: http://permissions-calculator.org/decode/0644/ . To really answer your question, then if you want to call a script with $ file.py, it should be in your PATH variable. You can display it with echo $ PATH. These are the directories that are executed to execute the script. Thus, you just need to give your script executable right and put it in one of the directories specified by your PATH.

0
source

You can check if / usr / bin / python or / usr / bin / python3.2 exists

Run the following command:

 which python3.2 

and then use the resulting path on top of you script.

0
source

Source: https://habr.com/ru/post/918249/


All Articles