3

I have a file called main.py and has the following code:

from os import system
from time import sleep
from datetime import datetime

while True:
    print(datetime.now(),"-+- Starting")
    system('file.py')
    print(datetime.now(),"-+- Crash")
    sleep(300)

However, if I try to execute it from the shell using python3 main.py I get the following error:

sh: 1: file.py: not found
^CTraceback (most recent call last):
  File "main.py", line 14, in <module>
    sleep(300)  # sleep for 5 minutes

file.py as well as main.py are both located in the same directory under /home/username/Documents/py-script/.

I even changed system('file.py') to the line below but I still can't execute file.py.

system('/home/username/Documents/py-script/file.py')

The program runs fine on windows, are there any additional steps I need to take in order to make it work on my Ubuntu desktop (not VM). I assigned 744 to both files as well.

1 Answer 1

3

You need to add the shebang line (#! interpreter) to 'file.py'

#!/usr/bin/python or #!/usr/bin/env python- This will instruct to execute the file using python. Also make sure the file has enough permission to execute.

or

you should call the program with python prefix

from os import system
from time import sleep
from datetime import datetime

while True:
    print(datetime.now(),"-+- Starting")
    system('python file.py')
    print(datetime.now(),"-+- Crash")
    sleep(300)
3
  • Or with /usr/bin/env python
    – A.B.
    Jul 10, 2015 at 4:43
  • I forgot to mention I was adding the shebang line, but I was adding it to 'main.py'. Anyway I used python file.py and it ran as expected. Thanks
    – Leb
    Jul 10, 2015 at 12:50
  • Glad that worked!
    – devav2
    Jul 10, 2015 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .