Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
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"

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.
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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?
 

IscariotJ

macrumors 6502a
Jan 13, 2004
637
66
UK
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?

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
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
...
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/" )
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
 

top4

macrumors newbie
Apr 30, 2008
1
0
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
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 

jill4099

macrumors newbie
Jun 24, 2008
1
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.