Search code examples
pythonbashmacosterminalpyperclip

Pyperclip - no module | Automate the boring stuff


My question is about running a simple mapit python script using the Mac terminal to execute.

I am following along with 'Automate the boring stuff'. I created a script to copy an address either from a website or using text in the clipboard to open a map on google maps. Please see code below.

I have tried to run $ python /Users/.../mapit.py using the command line but I am encountering the following error message:

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

Despite successfully running pyperclip on the IDLE shell.

I have reinstalled pyperclip using terminal and I have also re-written the script from scratch. I have used pyperclip in the interactive shell and it is working.

Could someone please help me identify where the problem might be?

MAPIT SCRIPT

#!/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)

Solution

  • Ah, you might want to consider reformulating the title of your question as it is a little misleading as to your problem.

    However, regarding the failure to find the right module it's important to mention how you (re-)installed it:

    My guess, given that you are on a Mac, is that you have multiple versions of python installed (e.g. the native python that is part of macOS and a version by Homebrew or another package manager). You are most likely installing the module in one of them, while trying to run your code with the other.

    I am guessing this answer might help you out.

    Good luck!