简体   繁体   English

在launchd中使用virtualenvs运行python时查找模块

[英]Finding modules when running python with virtualenvs, in launchd

This Q&A is similar to the one offered in Python Script not running in crontab calling pysaunter , but is put in terms of Launchd & Plists. 此问答与Python脚本中提供的问题类似, 不是在调用pysaunter的crontab中运行 ,而是以Launchd&Plists的形式出现。

I was successfully running python scripts in a virtualenv, but when I tried to schedule it in Launchd, I received a traceback error to my plist error log file saying "ImportError: No module named sqlalchemy". 我在virtualenv中成功运行python脚本,但当我尝试在Launchd中安排它时,我收到了一个回溯错误,我的plist错误日志文件说“ImportError:没有名为sqlalchemy的模块”。 What to do, what to do? 怎么办,怎么办?

At first I tried adding the .virtualenv sitepackages absolute path to the path and python path variables in .bash_profile, and .profile. 起初我尝试将.virtualenv sitepackages绝对路径添加到.bash_profile和.profile中的路径和python路径变量。 I also tried setting the environment variables path and pythonpath in launchd config. 我还尝试在launchd config中设置环境变量path和pythonpath。 No joy. 没有快乐。

What finally fixed it was to add into the plist the environment variables for PATH and PYTHONPATH. 最后修复它的是为plist和PYTHONPATH添加环境变量。 Which fixed it? 哪个修好了? Not sure, but having them both doesn't hurt anything. 不确定,但两者都没有伤害任何东西。 In these environment variables, I included the absolute path to sitepackages in .virtualenvs. 在这些环境变量中,我在.virtualenvs中包含了sitepackages的绝对路径。 Problem solved. 问题解决了。

HTH. HTH。

Example: 例:

<key>EnvironmentVariables</key>
    <dict>
    <key>PATH</key><string>/Users/user/.virtualenvs/oraenv/lib/python2.7/site-packages/</string>
    <key>PYTHONPATH</key><string>/Users/user/.virtualenvs/oraenv/lib/python2.7/site-package/</string>
    </dict>

With pipenv , the above answer becomes simplified to the following config: 使用pipenv ,上面的答案简化为以下配置:

<key>ProgramArguments</key>
<array>
  <string>/usr/local/bin/pipenv</string>
  <string>run</string>
  <string>python</string>
  <string>python_script_in_working_directory.py</string>
</array>
<key>WorkingDirectory</key>
<string>/path/to/working/directory</string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM