【问题标题】:How to get Automator to recognize python script with pyperclip?如何让 Automator 使用 pyperclip 识别 python 脚本?
【发布时间】:2017-02-05 20:14:53
【问题描述】:

我在 Mac OSX 10.12.3 中使用 Automator 来运行一个使用 pyperclip 的 Python 脚本。我使用 Run Shell Script (/usr/bin/python) 创建了一个 Automator 步骤:

import pyperclip
import datetime
now = datetime.datetime.now()
pyperclip.copy(str(now.year) + str(now.month) + str(now.day) + '_' + str(now.hour) + str(now.minute) + str(now.second))

但是,它显示了这个错误:

ImportError: No module named pyperclip

我可以在终端中执行这段代码,为什么 pyperclip 不被 /usr/bin/python 识别?仅供参考,我也安装了 Anaconda。

【问题讨论】:

  • 你确定你的机器上安装了pyperclip吗?
  • 你是如何安装 pyperclip 的?你下载了吗?它是你脚本的子目录吗?
  • 我明白为什么:Mac OSX 中的 /usr/bin/python 是 python 2.7,而我在 Anaconda virtualenv 下安装 pyperclip。

标签: python macos shell anaconda automator


【解决方案1】:

当我在 Anaconda virtualenv 下安装 pyperclip 时,Automator 正在使用 /usr/bin/python。当我运行 python 时,我注意到它说:

Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

看起来它与 Automator 使用的 Python 版本不同。现在我要做的就是:

sudo /usr/bin/python -m pip install pyperclip

【讨论】:

  • 我遇到了一个非常相似的错误,但在 automator 中我只能使用 /bin/bash 让它运行(并遇到“没有名为...的模块”错误)。知道如何在 /bin/bash 中安装我的模块(robobrowser)吗?我尝试了“sudo /bin/bash -m pip install robobrowser”,但它出错了
  • 你在使用 virtualenv 吗? pip install robobrowse 的输出是什么?请注意,您不必为 pip 使用 sudo bash
  • 是的,我正在使用 anaconda。不过,我能够弄清楚。我不得不将我的计算机指向 anaconda 的 python 版本,而不是系统的 python 版本。能够加载所有第三方库。使用this answer 做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多