Starting a python script on boot (startx) with an absolute path, in which there are relative paths

StackOverflow https://stackoverflow.com/questions/23492589

  •  16-07-2023
  •  | 
  •  

Question

I realise this question may already exist, but the answers I've found haven't worked and I have a slightly different setup.

I have a python file /home/pi/python_games/frontend.py that I am trying to start when lxde loads by placing @python /home/pi/python_games/frontend.py in /etc/xdg/lxsession/LXDE/autostart.

It doesn't run and there are no error messages.

When trying to run python /home/pi/python_games/frontend.py, python complains about not being able to find the files that are loaded using relative links eg: /home/pi/python_games/image.png is called with image.png. Obviously one solution would be to give these resources absolute paths, but the python program also calls other python programs in its directory that also have relative paths, and I don't want to go changing all them.

Anyone got any ideas?

Thanks Tom

Was it helpful?

Solution

you could change your current working directory inside the script before you start calling your relative imports, use os.chdir("absolute path on where your script lives").

OTHER TIPS

Rather than change your current working directory, in yourfrontend.pyscript you could use the value of the predefined__file__module attribute, which will be the absolute pathname of the script file, to determine absolute paths to the other files in the same directory.

Functions in theos.pathmodule, such assplit()andjoin(), will make doing this fairly easy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top