简体   繁体   English

OSX Automator无法使用Shell中的模块运行Python脚本

[英]OSX Automator failing to run Python script with modules from shell

I am trying to run a Python script that uses a few 3rd party modules (numpy, pandas, Twython) from Run Shell Script in OSX Automator. 我正在尝试运行一个Python脚本,该脚本使用OSX Automator中的Run Shell Script中的一些第3方模块(numpy,pandas,Twython)。 I wrote a hello_world.py script that runs successfully so I'm certain that the problem is due to the 3rd party modules. 我编写了一个hello_world.py脚本,该脚本成功运行,因此可以确定问题出在第三方模块。

In Terminal, I can successfully execute: python Desktop/my_folder/myscript.py , however when I try to run the shell script below, the shell script fails. 在终端中,我可以成功执行: python Desktop/my_folder/myscript.py ,但是当我尝试运行下面的shell脚本时,shell脚本失败。 I run Python 2.7.7 from anaconda distribution. 我从anaconda发行版运行Python 2.7.7。

#!/Users/myName/anaconda/bin/python2.7

cd ~/Desktop/my_folder/
python script.py

How do I write a shell script that works for Python scripts that use 3rd party modules? 如何编写适用于使用第三方模块的Python脚本的Shell脚本?

You need to specify the absolute path to the Anaconda Python when you invoke Python, not as a shebang line (which apparently is ignored by Automator anyway). 调用Python时,您需要指定Anaconda Python的绝对路径,而不是shebang行(显然,Automator显然会忽略它)。 When running a shell script under Automator, your shell startup profiles are probably not being run so the changes that put the Anaconda bin directory on PATH do not happen, causing python to refer to the system Python . 在Automator下运行shell脚本时,可能不会运行shell启动配置文件,因此不会发生将Anaconda bin目录放置在PATH上的更改,从而导致python引用系统Python Try this instead. 试试这个吧。

#!/bin/sh

cd ~/Desktop/my_folder/
/Users/myName/anaconda/bin/python2.7 script.py

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

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