I hope this is not a duplicate post however I have spent about 16 hours researching a solution for my problem and have not found it yet. I am attempting to run a python GUI that acts as an interface for a robot communication. The file as well as other necessary files such as .ini files are located in a folder in the /pi directory called "op_interface". The .py executes fine when navigating to the directory and then running the python file as so

cd /op_interface
sudo python op_interface.py

My goal is for this GUI to start any time that the Pi is rebooted. I have read multiple posts about using crontab, lxterminal on boot using "etc/xdg/lxsession/LXDE-pi/autostart", and many other ways that should work to do this but have been unable to get them to work with mine.

I believe I have narrowed the issue down to none of these methods being able to "locate" the other files that are needed to run my GUI (my GUI calls a couple other .py files to function). For instance when I type sudo python /home/pi/op_interface/op_interface.py in terminal to try to execute my GUI, I receive an error about it not being able to find the information needed from an .ini file in the same "op_interface" folder.

Because of it not being able to find those files, I believe that cron also does the same thing and I am wondering how I would need to point cron to this entire folder?

I currently have the final line in cron as @reboot sudo python /home/pi/op_interface/op_interface.py and have tried other variations such as @reboot sudo usr/bin/python /home/pi/op_interface/op_interface.py and @reboot sudo usr/bin/python2 /home/pi/op_interface/op_interface.py which did not work either.

I have also placed #!/usr/bin/env python at the top of the code in my .py file as well and it is still not working.

I am using Raspbian NOOBS V2.4.4 on a R-Pi 3B. If anyone can see what I am doing wrong or knows how to get this working, your advice would be much appreciated. If you would like actual python code samples, I can provide that as well. Thank you.

  • 1
    Jobs run from cron use a shell with different paths and environment variables than your user shell. That's almost certainly the problem. If you search based on that you should find a lot of information here and elsewhere.
    – Brick
    Commented Oct 30, 2017 at 21:54
  • 2
    Why not add a batch file to the directory which includes the cd into the directory and then calls the python scripts? Run that batch file from your crontab.
    – joan
    Commented Oct 30, 2017 at 21:58
  • 1
    I suppose your problem may also come from the way you load your ".ini" files in our python code. Sticking to absolute path instead of using relative one may may be an option. Commented Oct 30, 2017 at 22:02

1 Answer 1


Thank you all for your advice on my question. Based on the suggestion by user name joan, I did some research and ended up going the route of making a batch file to the correct directory. I am leaving the exact way that I did this here, for anyone else who runs into a similar issue. Thank you again to all.

  1. Create batch script (yourscript.sh file) in /home/pi directory with the following text inside: enter image description here
  2. At the top of your code for your python script, add the line "#!/usr/bin/env python" without quotes
  3. Allow both the batch and the python scripts to be executable by typing the following into terminal:

"chmod +x /path/to/folder/yourscript.sh" without quotes

"chmod +x /path/to/folder/yourscript.py" without quotes

  1. Test that yourscript.sh correctly runs yourscript.py by typing the following into terminal:

"sudo cd /path/to/yourscript.sh" without quotes

  1. In terminal type the following to edit the autostart script:

"sudo nano ~/.config/lxsession/LXDE-pi/autostart" without quotes

  1. Within the nano editor, add the following line before @xscreensaver –no-splash:

"@/path/to/yourscript.sh" without quotes

Ctrl + X to exit, “Y” to save, “Enter” to confirm

  1. Sudo reboot and it works now for me
  • you are adding extra complexity by using the shell script to launch the python script. You can simplify the process by making the python script able to be executed from any directory. If your python script is using relative paths to find files, you can use the os.path module to determine the absolute path of the directory the python script resides in. This may require modification of the python script, but is a much more resilient approach and will make the script portable since it will not rely on a hard-coded path. script_path = os.path.abspath(os.path.dirname(file))
    – bcarroll
    Commented Mar 7, 2019 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.