Report this

What is the reason for this report?

Call Python Script "Permission denied" - Ubuntu 18.04

Posted on July 28, 2020

Hello …

I am trying to let python script to call another python script.

Running command below the python script to call the test1.py script in Ubuntu 18.04

sudo python3 test1.py

The test1.py contains the code below

import os
count = 1

if count == 1:
            os.system('/opt/pythonm/test.py')
            count = count + 1
            print('Done')
else:
    print('Not Meet the conditio and Exit')
    exit

However, I got the error below

sh: 1: /opt/pythonm/test.py: Permission denied
Done

Thank you for your help.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

My thinking would be add python3 before the file location.

Could be a system permissions issue, I wouldn’t call it though, I would import it and run within the current app or use threading to run along side.

The “Permission denied” error means that the user who is running the Python script doesn’t have sufficient permission to execute the other script.

To fix this, you will need to make test.py executable. You can do this by running the following command:

sudo chmod +x /opt/pythonm/test.py

The chmod +x command changes the permissions of the file to make it executable.

Also, it’s important to note that if test.py is a Python script, it should start with a shebang line that specifies the interpreter for the script, which is Python in this case. If it doesn’t have one, you should add the following line at the very top of test.py:

#!/usr/bin/env python3

This tells the operating system to run the script using Python 3.

Finally, if test.py needs to be run with Python specifically (rather than being run directly as an executable), you may want to change your os.system call to include the python command:

os.system('python3 /opt/pythonm/test.py')

In this case, ensure that the user running the script has permission to use Python to execute scripts.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.