I have some difficulty with the abovet. I succeeded after a fashion, but only 
after some experiment, and I am not satisfied that all is well. 
Here is the script code (as per the Python tutorial 6.1.1.):

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

def fib2(n): # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result
    
if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

It is saved in the current directory as fibo.py with the current directory 
being: 
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/

The module executes ok when imported but not as a script. 
Terminal returns: 
Apples-iMac-4:~ apple$ python fibo.py 50
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:
 can't open file 'fibo.py': [Errno 2] No such file or directory
Apples-iMac-4:~ apple$ 

However, Python appears to be searching the correct directories:

>>> import sys
>>> sys.path
['', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gtk-2.0']

And. . .  if I open the same script with: right click > Open with > Python 
Launcher.app (2.7.6) I get:
Last login: Wed Dec  4 20:52:15 on ttys002
Apples-iMac-4:~ apple$ cd 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/' && 
'/usr/bin/pythonw'  
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/fibo.py'
  && echo Exit status: $? && exit 1
Traceback (most recent call last):
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/fibo.py",
 line 21, in <module>
    fib(int(sys.argv[1]))
IndexError: list index out of range

Then . . . if I then use this same window to call the script it runs:
Apples-iMac-4:python2.7 apple$ python fibo.py 50
1 1 2 3 5 8 13 21 34
Apples-iMac-4:python2.7 apple$ 

Je suis perplexed!

-A
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to