Unable to run simple "if__name__ =='__main__':" syntax using Canopy

5.4k views Asked by At

I obtained the following password generator (Test.py) from the Internet and I thought it would be easy to run in Canopy but it does not seem to work at all.

I get invalid syntax errors all the time. I send the code to a colleague of mine and she said it worked fine using the simple i-Python terminal on her Linux Machine.

#!/usr/bin/env python
import random
import sys
import string


def main(argv):

    if (len(sys.argv) != 5):
        sys.exit('Usage: simple_pass.py <upper_case> <lower_case> <digit> <special_characters>')

    password = ''

    for i in range(len(argv)):
        for j in range(int(argv[i])):
            if i == 0:
                password += string.uppercase[random.randint(0,len(string.uppercase)-1)]
            elif i == 1:
                password += string.lowercase[random.randint(0,len(string.lowercase)-1)]
            elif i == 2:
                password += string.digits[random.randint(0,len(string.digits)-1)]
            elif i == 3:
                password += string.punctuation[random.randint(0,len(string.punctuation)-1)]

    print 'Your new password is: ' + ''.join(random.sample(password,len(password)))

if __name__ == "__main__":
    main(sys.argv[1:])

I tried:

$python Test.py 2 2 2 2

I tried using the entire pathname as well, but nothing seems to work! I'm quite frustrated because it seems like it would be so easy. Any help would be greatly appreciated.

1

There are 1 answers

1
Ben On BEST ANSWER

My guess is that you're running this command inside of ipython

$ python Test.py 2 2 2 2

Which is how you would run the script if you were in the command line, and it's comming back with the error:

In [1]: python Test.py 2 2 2 2
  File "<ipython-input-3-5bade39d33f0>", line 1
    python Test.py 2 2 2 2
                                           ^
SyntaxError: invalid syntax

If you want to run it inside of Canopy (which is an ipython shell, not a terminal window), run the file like so

In [1]: %run Test.py 2 2 2 2
Your new password is: NDt1q.+1