1
votes

I am trying to run the following code from the terminal window. I am following the tutorial found on this website https://automatetheboringstuff.com/chapter6/ . The name of my file is "pw.py" I ran chmod +x pw.py to make it executable from the terminal window. However, when I run ./pw.py I get an error saying "-bash: ./pw.py: python: bad interpreter: No such file or directory". Any Idea what I missed? Thanks for the help!

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

PASSWORDS = {'email' : 'F7minlBDDuvMJuxESSKHFhTxFtjVB6' ,
             'blog' : 'VmALvQyKAxiVH5G8v01if1MLZF3sdt' ,
             'luggage' : '12345'}
import sys, pyperclip
if len(sys.argv) < 2:
    print('Usage: python pw.py[account] - copy account password')
    sys.exit()

account = sys.argv[1]    # first cammand 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)
3
If /usr/bin/python points to python3.5 I think something is wrong, have you messed with the system python? - Padraic Cunningham
Can you execute simple python scripts from your CLI, example python myscript.py ? - gogasca
I dont think I have. What should the shebang line look like? - DakotaDickey44
Where is your python3.5 installed? You can try /usr/bin/env python3 but the error using /usr/bin/python seems like something is wrong with your system - Padraic Cunningham
python is installed at macintoshHD/applications/python3.5 . Using /usr/bin/env python3 produces the same error. - DakotaDickey44

3 Answers

2
votes

I figured out my problem. I was not executing it in the terminal correctly. I was entering './pw.py' when I needed to enter 'python pw.py' in order for it to run the script. Thanks for the help.

0
votes

What you have done looks correct from a python perspective. the #! is called a shebang / shabang and it informs the OS what interpreter to call in order to execute this script.

So I would guess that Python is installed in a different location then what has been supplied. Remember when this is called the full path needs to be supplied. Try running which python to get the location of the interpreter and place that at line 1

0
votes

I started getting this error when running pip python command, this started happening after a system-wide change in $HOME path, which apparently broke some stuff like pip (the old $HOME path seems hardcoded somewhere in the python installation).

I fixed it by re-installing pip (following this):

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python get-pip.py