LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2005, 10:00 AM   #1
Monjawi
LQ Newbie
 
Registered: Feb 2005
Location: Denmark
Distribution: Mandriva 2006
Posts: 27

Rep: Reputation: 15
newbie, getting simple python script to work


I have started learning python, and I want to make a script.

#!/usr/bin/env python

import math

decibel = math.log10 (17.0)
angle = 1.5
height = math.sin(angle)
degrees = 45
angle = degrees * 2 * math.pi /360.0
math.sin(angle)

I works when I type the stuff in konsole, but when runnning the script it gives the error

$ python math.py
Traceback (most recent call last):
File "math.py", line 3, in ?
import math
File "/home/christoffer/python/math.py", line 5, in ?
decibel = math.log10 (17.0)
AttributeError: 'module' object has no attribute 'log10'

I have tried switching "#!/usr/bin/env python" with "#!/usr/bin/python python" because "which python" gives "/usr/bin/python" but I don't work either.
 
Old 10-03-2005, 10:09 AM   #2
lurker79
Member
 
Registered: Jan 2005
Location: UK
Posts: 55

Rep: Reputation: 16
Do you have a file in the current directory called math.py?
When you run your python script it is importing the math.py from your current directory which does not have the log10 attribute.
try copying your test file to /tmp and running it from there, do you get the same problem?
 
Old 10-03-2005, 10:14 AM   #3
Monjawi
LQ Newbie
 
Registered: Feb 2005
Location: Denmark
Distribution: Mandriva 2006
Posts: 27

Original Poster
Rep: Reputation: 15
The file is in /home/christoffer/python/math.py
and run standing in that directory.
Copying to tmp and runing from there don't help.
How can I make it point to math, so it uses it?
 
Old 10-03-2005, 10:30 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
(1) it should just be "#!/usr/bin/python", not "#!/usr/bin/python python".
(2) the file tries to import itself--python resolves imports by looking in the current directory first. You can either import the math library using the same functions some deep, low-level code uses, or just rename your file.

IOW, just rename your file.

hth --Jonas
 
Old 10-03-2005, 10:35 AM   #5
Monjawi
LQ Newbie
 
Registered: Feb 2005
Location: Denmark
Distribution: Mandriva 2006
Posts: 27

Original Poster
Rep: Reputation: 15
thank you
 
Old 10-03-2005, 10:56 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Quote:
Originally posted by jonaskoelker
(1) it should just be "#!/usr/bin/python", not "#!/usr/bin/python python".
The posted script has "#!/usr/bin/env python", not "#!/usr/bin/python python".

"#!/usr/bin/env python" is correct, and often used for python scripts. I don't understand however the difference between "#!/usr/bin/env python" and "#!/usr/bin/python"

Last edited by Hko; 10-03-2005 at 10:57 AM.
 
Old 10-03-2005, 11:51 AM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
I have tried switching "#!/usr/bin/env python" with "#!/usr/bin/python python"
That's what I was referring to. But nevermind. "If it ain't fixed don't break it".

The difference is that with the absolute path (#!/usr/bin/python), if the python executable is stored anywhere else (say, /usr/local/bin/python), your script won't run. By using env, you fix the location of env instead of python; I think env is more often found in /usr/bin that python, so that's why.

hth --Jonas
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM
Why doesnt my simple html post to cgi script work? ginda Linux - Networking 10 03-15-2005 03:32 PM
how do I do this in Python? (simple sh script) johnMG Programming 6 12-22-2004 10:05 PM
veprory simple but frustrating for a newbie shell script question marigb Linux - Newbie 11 04-28-2004 12:11 PM
simple python function question Tyir Programming 2 03-28-2004 10:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration