PYTHONPATH not used in Jupyter lab as service

I have Jupyter lab running as a service in a virtual environment on an Ubuntu machine (24 LTS I think). It works perfectly except that it doesn’t load my PYTHONPATH from ~/.bashrc. Here is the service plist file contents:

[Unit]
Description=JupyterLab Service

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/drkrauss/Dev/venv/bin/jupyter lab --config=/home/drkrauss/.jupyter/jupyter_lab_config.py
#User=drkrauss
#Group=drkrauss
WorkingDirectory=/home/drkrauss/
#Restart=always
RestartSec=10
Restart=on-failure

[Install]
WantedBy=default.target

Interestingly, when I run the command

/home/drkrauss/Dev/venv/bin/jupyter lab

in a terminal after login, the PYTHONPATH is read correctly. I think that is just saying that running the command in a terminal means .bashrc has already been sourced. So I guess the service doesn’t source .bashrc first???

I am fairly surprised that googling around has not lead to a good answer.

Ideally I would love to source my .bashrc file before starting the juptyer lab server, but if I had to maintain a separate PYTHONPATH, I could live with that.

How do I set PYTHONPATH for Jupyter lab running in a venv as a service?

Thanks,

Ryan

I solved my problem and am posting in case it helps anyone else. The issue is that systemd services intentionally do not source the users .bashrc file because some of that might be undesirable in certain environments. Instead, you can create an EnvironmentFile that is essentially just the parts of .bashrc needed for your service. So, I put the PYTHONPATH in my .env file:

PYTHONPATH=/home/drkrauss/git/krauss_personal:/home/drkrauss/git/report_generation:/home/drkrauss/git/tk_grading_gui

and then point to that .env file in the plist file for the service:

[Service]
Type=simple
EnvironmentFile=/home/drkrauss/Work_vault_ios/linux/python_path.env

Hopefully this helps someone else in the future.

Ryan

2 Likes