0

I am very new to Python and I am going through the "Automate the Boring Stuff with Python" book (https://automatetheboringstuff.com/chapter6/). I am working on the Password Locker project and having difficulties with the pyperclip module. I am running Python 3.6.2 on Mac OS X Yosemite.

When I try to run my program through terminal I receive this message:

Traceback (most recent call last):
  File "./pw.py", line 8, in <module>
    import sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'

However, I have already installed pyperclip and receive this message when I try using "pip3 install pyperclip":

pip3 install pyperclip
Requirement already satisfied: pyperclip in     
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

and it works fine for me in Shell:

>>> import pyperclip
>>> pyperclip.copy('hello')
>>> pyperclip.paste()
'hello'

I believe my program is identical to that in the book aside from the actual passwords:

#! /usr/bin/env python3
# pw.py - An insecure password locker program.

PASSWORDS = {'email': '12345',
             'blog' : '54321',
             'luggage': '123456789' }

import sys, pyperclip
if len(sys.argv) < 2:
    print('Usage: python pw.py [account] - copy account password')
    sys.exit()

account = sys.argv[1]   # first command line arg is the account name

if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print('Password for ' + account + ' copied to clipboard.')
else:
    print('There is no account named ' + account) 

I've been trying to find out why this happens, but cannot figure it out. Again, I am very new to Python and any helpful insight would be much appreciated! Thanks!

jhpratt
  • 6,841
  • 16
  • 40
  • 50
K123
  • 31
  • 1
  • 3
  • Script works perfectly on my computer, so it's probably a directory issue. What does `os.getcwd()` return? Try installing another module, like `arrow`, and see if you can import that. – rassar Jan 04 '18 at 02:18
  • 2
    what does `which python3` and `which pip3` and `head -1 $(which pip3)` say – anthony sottile Jan 04 '18 at 02:29
  • You said "it works fine for you in Shell". Are you using some sort of IDE for your development? – larsks Jan 04 '18 at 02:47
  • It seems to have been a directory issue. I reinstalled Python and everything seemed to work out. Thanks for your replies! – K123 Jan 08 '18 at 19:30

3 Answers3

1

You mentioned that pyperclip is installed in
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

In your script, print(sys.path) and verify that directory is available to your program for imports. If interpreter and library are still mismatched, consider creating an environment using conda or virtualenv.

BTW, please delete the import line, and put two new import lines right after the shebang, so they appear before the PASSWORDS assignment. It's just a flake8 style thing.

Also, consider using argparse or click rather than reading sys.argv[1]. For one thing, you get "--help" for free.

J_H
  • 17,926
  • 4
  • 24
  • 44
0

Change the shebang in #! /usr/bin/env python an run it using python2 instead.

0

I really struggled trying to get pyperclip installed in the correct environment for my python distribution. I had installed pythong using anaconda navigator. That plus the default python that comes on the mac plus a few others put me in a tough situation. (Perhaps similar to the situation you are facing.

My fix was to find out which python interpreter my IDE (Spyder 4) was using and then from there run installation commands that referenced this location.

More simply put:

pip install pyperclip

became

/usr/local/anaconda3/bin/pip install pyperclip

This appears to have solved the problem for me. Good luck to all of the others who are facing something similar.