Launchd runs python script, but import fails

I wrote a python script using appscript to track my current active window. I run it through launchd, but when I do this, it cannot import the script. I installed PYTHONPATH in plist to run, but I think launchdd does not read .pth files in site packages. Is there any way to do this like this?

My script is here: https://github.com/katylava/macwintracker

This is the entry level:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>label</key> <string>com.katylavallee.wintracker</string> <key>ProgramArguments</key> <array> <string>/Users/kyl/Library/Application Support/com.katylavallee.wintracker/wintracker.py</string> <string>1</string> <string>1</string> </array> <key>Environment Variables</key> <dict> <key>PYTHONPATH</key> <string>/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages</string> </dict> <key>StandardErrorPath</key> <string>/Users/kyl/Library/Logs/com.katylavallee.wintracker/wintracker_err.log</string> <key>StandardOutPath</key> <string>/Users/kyl/Library/Logs/com.katylavallee.wintracker/wintracker.log</string> <key>StartInterval</key> <integer>3</integer> </dict> </plist> 

And the error:

 Traceback (most recent call last): File "/Users/kyl/Library/Application Support/com.katylavallee.wintracker/wintracker.py", line 5, in <module> from appscript import app, its ImportError: No module named appscript 

The python script works fine from the command line.

+4
source share
2 answers

Most likely, the Python system ( /usr/bin/python ) is launched to execute your script, and not MacPorts Python ( /opt/local/bin/python2.6 ), where you installed appscript . What should work (unchecked!) Is to insert the Python MacPorts path as the first Program Argument , before the script path. And in this case you do not need to specify PYTHONPATH . Theoretically, you could do what you have while MacPorts Python is configured compatible (i.e., similar arches, deployment targets, etc.) with the Python system, but you probably shouldn't or shouldn't go around this way.

Another approach would be to change the shebang line (first line) of the script to the explication path on MacPorts Python:

 #!/opt/local/bin/python2.6 

The reason this works in the command line shell is likely that one of the shell profile files, for example .bash_profile , changes the PATH environment variable to first include the MacPorts Python path ( /opt/local/bin ) to /usr/bin/env python first finds MacPython python . When passing through launchd shell is not involved, so the PATH manipulation is not performed; the default search is done by default, i.e. /usr/bin/env python does /usr/bin/python .

+6
source

You can also use the full path to python in ProgramArguments. (More info here: Running Python Script with Launchd: import not found )

  <key>ProgramArguments</key> <string>/path/to/your/python</string> <string>/path/to/your/script</string> 
0
source

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


All Articles