问题
So I'm new to learning Python and am trying to execute some Python code from Automate the Boring Stuff (specifically the Password Locker Project). In the book, it tells us to run this program (attached below) through Terminal, so I did the following steps:
- Saved the code in my home folder and titled it pw.py
- Changed the .py's files to make it executable by running
chmod +x pw.py
- Tried to run my script by entering
./pw.py
on Terminal
However when I run the code, Terminal says there is no module named pyperclip (again, shown below). Naturally, I tried to install pyperclip with the prompt python3.8 -m pip install pyperclip
but Terminal claims that I've already installed pyperclip. Any ideas for how to solve this problem?
Here is the error I see on Terminal when I try to run my code
Traceback (most recent call last): File "./pw.py", line 7, in <module> import sys, pyperclip ModuleNotFoundError: No module named 'pyperclip'
Here is the error I see on Terminal when I try to install pyperclip
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
Here is the code I copied from Automate the Boring Stuff that I typed out on IDLE.
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6', 'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt', 'luggage': '12345'}
import sys, pyperclip
if len(sys.argv) < 2:
print('Usage: py 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)
version: python3
Thanks!
回答1:
It sounds very much like you have more than one Python installation. You installed pyperclip with python3.8
specifically, but by executing your script using ./pw.py
, you could be running it with some other installation currently set as your default. Try python3.8 pw.py
and see if that works.
Edit: Glad that worked! If you'd like to investigate what version your system is using by default, try entering python --version
on the command line. And as M Z mentioned below in a comment, sys.version_info
(or also just sys.version
) can access the current Python version from inside a script.
来源:https://stackoverflow.com/questions/63387372/terminal-claims-no-pyperclip-module-found-but-that-ive-already-installed-pyperc