python newbie questions

Discussion in 'Mac Programming' started by jamesapp, Apr 29, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    having trouble running a python script.
    i created some simple python scripts. and i can't seem to get them to run?
    one file i called spam.py and i will list it's contents here.
    Code:
    print 2 ** 8
    print 'the bright side ' + 'of life'
    
    i stored the file in a directory in my home directory which i called python.
    /users/jamescollins/python/spam.py

    a book i am reading says
    "once you've saved this text file, you can ask Python to run it by listing it's full filename as the first argument to a python command, typed at the shell prompt"
    the output from the book after running the script is as follows:

    % python spam.py
    256
    the bright side of life

    in terminal i started Python
    and then typed

    Code:
    python spam.py
    
    i got the following error messages


    Code:
    >>> python spam.py
      File "<stdin>", line 1
        python spam.py
                  ^
    SyntaxError: invalid syntax
    
    just trying to run simple python scripts
    any help would be appreciated.

    one thing i did was go into terminal into my python directory, and typed
    python spam.py
    and the program ran in terminal
    i think the book was telling me to run spam.py as an argument when i launched python?
    how do i get the program to run in the interactive prompt?
    i also used the import command to import a file which i called spam.py
    here is the output from my terminal session

    Code:
    >>> import spam.py
    256
    the bright side of life
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named py
    
    does anyone know what this added text means?

    Code:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named py
    
    another thing i will add is that i have two versions of python on my computer.
    one i think is python that came with the developer tools, which i run from terminal.
    another is a version i downloaded from the internet.
    the one i downloaded from the internet has text in different colors.
    the one i run from terminal seems easier to open files because i can switch to the directory where i have my scripts, and then start python and run the scripts by importing them. i also can call up python with a file as an argument.
    the one i downloaded from the internet is giving me error messages
    like when i try to import spam.py i get the following

    Code:
    >>> import spam.py
    
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        import spam.py
    ImportError: No module named spam.py
    
    how do i import a file?
    do i need to give it a path?

    again any help would be appreciated.
     
  2. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #2
    type your command at the SHELL PROMPT not Python interpreter prompt: open Terminal, 'cd' to your python script directory and type
    Code:
    $ python spam.py
    
    If you want to execute python module in the interactive Python shell:
    start python by typing
    $ python
    and type
    Code:
    >>> import spam
    
    without '.py' file extension.

    Your problems originates in mixing UNIX bash shell and interactive Python shell.
     
  3. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #3
    on the version of python which i downloaded from the internet i tried to import
    spam here is my python session after i launched the IDLE.

    Code:
    >>> import spam
    
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        import spam
    ImportError: No module named spam
    
    does this have anything to do with the path?
     
  4. IscariotJ macrumors 6502a

    Joined:
    Jan 13, 2004
    Location:
    UK
    #4
    It is. Python has it's own path; basically, it isn't looking in your directory. If you want to be able to run it from within IDLE, the best way is by adding your to Python's path. From the IDLE prompt:

    import sys
    sys.path.append( "/users/jamescollins/python/" )

    And then try your import spam
     
  5. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #5
    Or you can modify python path by setting PYTHONPATH env variable. In bash shell:
    $ export PYTHONPATH=.:$HOME/python:$PYTHONPATH

    Usually one does it by including the above line in .bash_profile file.

    Check official Python tutorial for details:
    http://docs.python.org/tut/node8.html#SECTION008120000000000000000
     
  6. top4 macrumors newbie

    Joined:
    Apr 30, 2008
    #6
    Just modify your script like
    Code:
    #!/usr/bin/python
    print 2 ** 8
    print 'the bright side ' + 'of life'
    and do in terminal:
    Code:
    chmod +x /users/jamescollins/python/spam.py
    /users/jamescollins/python/spam.py
     
  7. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #7
    Code:
    # Setting PATH for MacPython 2.5
    # The orginal version is saved in .bash_profile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
    export PATH
    
    tried to change above file which is .bash_profile
    just before export PATH i added

    Code:
    $ export PYTHONPATH=.:$HOME/python:$PYTHONPATH
    
    i then went into python and tried one of my sample scripts
    got error messages like that python couldn't find my scripts

    i opened .bash_profile in terminal, the file opened into TextEdit
    i set the PYTHONPATH env variable. by adding the above code
    then i saved the file.

    just wondering what i am doing wrong, should i take out ?
    Code:
    export PATH
    
    also this file .bash_profile which i found was a hidden file in my home directory, is this the file for python that came with the developer tools or the version of python i downloaded from the internet?

    i also tried adding
    Code:
    $ export PYTHONPATH=.:$HOME/python:$PYTHONPATH
    
    to my .bash_profile file at the end of the .bash_profile file, right after export PATH, and then went into my IDLE and tried to import my scripts, got error messages.
     
  8. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #8
    tried adding

    Code:
    $ export PYTHONPATH=.:$/users/jamescollins/python:$PYTHON
    
    i replaced HOME with /users/jamescollins
    and my IDLE still had error messages when i tried to import a file.

    any help would be appreciated.
     
  9. jill4099 macrumors newbie

    Joined:
    Jun 24, 2008
    #9
    also having trouble with modules

    I am just learning python and have been trying to import a module to IDLE.

    I keep getting an error message...


    Code:
    import fibo2
    
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        import fibo2
      File "/Users/radhika/Documents/fibo2.py", line 1
        {\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf270
                                                         
    ^
    SyntaxError: unexpected character after line continuation character
    
    This is the module I am trying to import.

    Code:
    #Fibonacci numbers module
    
    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

    Please someone help me
     

Share This Page