Running Python Script with Launchd: import not found

I am trying to configure a script to run a python program at regular intervals using launchd . The python program fails because it cannot find my import --- I know this because I caught errors in the log file. To fix this problem, I created ~\.MacOSX\environment.plist and duplicated my PYTHONPATH there, logged out and logged in again. It seems that this is not enough to solve the problem, and I do not understand what else to try.

I am running OSX, 10.8.3.

Related topics:

UPDATE:

It seems that I can run the following command:

 launchctl setenv PYTHONPATH $PYTHONPATH 

and the script will execute successfully. So, to change my question:

  • Where is it stored? I checked ~\.launchd.conf and \etc\.launchd.conf , and did not exist.
  • Presumably, this setting is reset upon reboot. Where can I change this information so launchd finds it?
+2
source share
2 answers

To configure the environment for a specific task, you must use the EnvironmentVariables key in the task definition itself:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.example.app</string> <key>Program</key> <string>/path/to/your/script</string> <key>EnvironmentVariables</key> <dict> <key>PYTHONPATH</key> <string>/your/python/path</string> </dict> </dict> </plist> 

You can define default environment variables for launchd(8) services by editing /etc/launchd.conf for daemons or /etc/launchd-user.conf for agents. The latter works, but is not documented. Currently registered (but not supported) configuration file for each user $HOME/.launchd.conf .

These configuration files contain a list of launchctl(1) subcommands. Do you want to:

 setenv PYTHONPATH /your/python/path 

Update: /etc/launchd.conf not supported on Mac OS X 10.10 or higher. On these systems, you will have to define environment variables based on each job.

+5
source

None of the above worked for me (OS X 10.11.3). I read the breakthrough: the script works fine in the terminal, but not from launchd and belatedly understands that you can write an absolute full path to the python version with the correct modules, D'o.

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.example.app</string> <key>ProgramArguments</key> <string>/path/to/your/python</string> <string>/path/to/your/script</string> </dict> </plist> 
+2
source

Source: https://habr.com/ru/post/1346995/


All Articles