Sahand
Sahand

Reputation: 8380

How to run executable with Python code inside

I have the following file:

$ cat my_exec.sh 
#!/usr/bin/env python

print(10)

It should just print 10. However, I can't get it to do so:

$ sudo ./my_exec.sh

sudo: ./my_exec.sh: command not found

$ sh my_exec.sh 

my_exec.sh: line 3: syntax error near unexpected token `10'
my_exec.sh: line 3: `print(10)'

How do I run my file?

Upvotes: 1

Views: 159

Answers (3)

lorg
lorg

Reputation: 1170

  • Change the shebang to #!/usr/bin/env python
  • Change the filename to my_exec.py, as is convention for python files
  • You can run with python my_exec.py
  • You can chmod +x my_exec.py and then ./my_exec.py

Upvotes: 2

MichaelDuth
MichaelDuth

Reputation: 11

You must enter the directory that you have saved you file through the cmd with cd command. After that you just execute the file with : python name_of_the_file.py . But first you must make it executable with chmod command

For example if you have saved your file at Desktop with the name mycode.py :

cd Desktop
chmod +x mycode.py
python mycode.py

Upvotes: 0

George
George

Reputation: 126

You can run it via the python command:

$ python my_exec.sh

To run it as simply ./my_exec.sh, you need to make the file executable first:

$ chmod 755 my_exec.sh

Also note that by convention python files end in .py .

Upvotes: 2

Related Questions