2

我的问题是关于使用 Mac 终端运行一个简单的 mapit python 脚本来执行。

我正在关注“自动化无聊的东西”。我创建了一个脚本来从网站复制地址或使用剪贴板中的文本在谷歌地图上打开地图。请看下面的代码。

我尝试$ python /Users/.../mapit.py使用命令行运行,但遇到以下错误消息:

Traceback (most recent call last):
  File "/Users/.../mapit.py", line 2, in <module>
    import webbrowser, sys, pyperclip
ImportError: No module named pyperclip

尽管在 IDLE shell 上成功运行了 pyperclip。

我已经使用终端重新安装了 pyperclip,并且我还从头开始重新编写了脚本。我在交互式 shell 中使用了 pyperclip,它正在工作。

有人可以帮我确定问题可能出在哪里吗?

映射脚本

#!/usr/bin/env pythonw
import webbrowser, sys, pyperclip

sys.argv

# Check if command line arguments were passed
if leng(sys.argv) > 1:
    # we know that the address has been passed
    address = ' '.join(sys.argv[1:])
else:
    address = pyperclip.paste()

# https://www.google.ae/maps/place/<ADDRESS>
webbrowser.open('https://www.google.ae/maps/place/' + address)
4

1 回答 1

0

啊,您可能需要考虑重新制定问题的标题,因为它对您的问题有点误导。

但是,关于未能找到正确的模块,重要的是要提及您如何(重新)安装它:

鉴于您在 Mac 上,我的猜测是您安装了多个版本的 python(例如,作为 macOS 一部分的本机 python 和 Homebrew 或其他包管理器的版本)。您很可能将模块安装在其中一个中,同时尝试与另一个一起运行您的代码。

我猜这个答案可能会对你有所帮助。

祝你好运!

于 2018-04-12T08:06:44.730 回答