Raspy_D
Posts: 6
Joined: Thu Nov 09, 2017 2:03 am

python with environment variable running at boot

Thu Jul 30, 2020 10:05 pm

Hi to all, I know that the web is full of guides on how to start a python script at the boot, but I'm here to ask your help since I've unsuccessfully tried everything I could, everything I've found.
The script by command line works great without any error/exception. After lots of trying I've understood what blocks it.
My script (python2.7 with debian buster) starts with

Code: Select all

import os
import os.path
import json
prog_path = os.environ.get('prog_path')
Settings = os.path.join(prog_path, 'Settings.json')
where prog_path is an environment variables setted in ~/.bashrc adding this line at the end:
export prog_path="/home/pi/programfldr/prog_path "

what I've found is that this line doesn't allow the script to run

Code: Select all

Settings = os.path.join(prog_path, 'Settings.json')
I made this test: after this line I just ask python simple creating a folder. If I comment the line, it works perfectly, if not it's not works at all.
Can please someone help me to solve it?
Thanks a lot guys

ghp
Posts: 1592
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: python with environment variable running at boot

Fri Jul 31, 2020 5:39 am

Hello, the term "start at boot" is ambiguous.
It can be
- start with systemd
- start with cron
- start when user logs in by bashrc
and possibly some more options.

Assuming you use systemd to start, then provide the environment variables in your service file:

Code: Select all

[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/myscript.py
Environment="prog_path=/home/pi/programfldr/prog_path"

[Install]
WantedBy=multi-user.target
There is also the possibility to set the working directory in the service file

Code: Select all

WorkingDirectory=/home/pi
but whether this is useful for your application depends on what your program is doing.

Raspy_D
Posts: 6
Joined: Thu Nov 09, 2017 2:03 am

Re: python with environment variable running at boot

Fri Jul 31, 2020 7:07 am

Thanks a lot ghp. I'll try as soon as I can.
I tryed making it start with several methods (crontab, normal end as root, systemd, etc)
the WorkingDirectory should be the one where my python script is, correct?

Raspy_D
Posts: 6
Joined: Thu Nov 09, 2017 2:03 am

Re: python with environment variable running at boot

Fri Jul 31, 2020 12:56 pm

Hi. I made a test with partially good result.
Now the main program works perfectly, but it has a subrocess that requires some modules.
Now the error I got is

type 'exceptions.ImportError'
No module named xlrd

I use it for reading excel files

Raspy_D
Posts: 6
Joined: Thu Nov 09, 2017 2:03 am

Re: python with environment variable running at boot

Fri Jul 31, 2020 1:06 pm

I solve the problem using User=pi in [Service] section
;-) thanks for support

Raspy_D
Posts: 6
Joined: Thu Nov 09, 2017 2:03 am

Re: python with environment variable running at boot

Fri Jul 31, 2020 2:59 pm

Can I ask you last support?
I need the same before shutdown/reboot.

I'm trying with something like this, but without success

Code: Select all

[Unit]
Description=Running before shutdown.
Requires=network.target
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/usr/bin/python /home/pi/myscript.py

[Install]
WantedBy=multi-user.target
I even tryed adding User=pi, WorkingDirectory, environment, but still it doesn't work
Thanks really a lot

Return to “Python”