user1547428
user1547428

Reputation: 21

Python won't import math module. It's fetching a file that isn't even there. What do I do?

# quadratic.py
# A program that computes the real roots fo a quadratic equation.
# Illustrates the use of the math library
# Note: This program crashes if the equation has no real roots

import math # math makes the library available

def main():
    print "This program finds the real solutions to a quadratic"
    print

    a, b, c = input("Please enter the coefficients (a, b, c): ")
    discRoot = math.sqrt(b * b - 4 * a * c)
    root1 = (-b +discRoot) / (2 * a)
    root2 = (-b +discRoot) / (2 * a)

    print
    print "The solutions are: ", root1 , root2 

main()

Her's the error I'm getting:

Macintosh-7:python andrewmetersky$ python quadratic.py
The answer to my homework question: what is i+x+j =
Traceback (most recent call last):
File "quadratic.py", line 6, in
import math # math makes the library available
File "/Users/andrewmetersky/Desktop/Programming/Python/math.py", line 5, in
NameError: name 'jp' is not defined

The problem is, math.py isn't even a file in that location. It was, but I deleted it because i figured Python was trying to fetch that and not the math module. There is a file called math.pyc in that location...is that the module? why won't it fetch that.

Thanks

PS- Also, how do I make that section i just pasted appear as code w/in stack overflow without having to press space 4x for each line.

Upvotes: 0

Views: 6244

Answers (4)

Jeff Shelman
Jeff Shelman

Reputation: 426

You will need to delete the .pyc file as well. That's the compiled version of the original .py file and python will use that if it's in the path. It only gets updated (re-compiled) if the source (.py) file is exists and is newer.

When you import a local file for the first time, Python converts that file into bytecode and labels it .pyc.

Upvotes: 5

Koonwah
Koonwah

Reputation: 31

python import "/Users/andrewmetersky/Desktop/Programming/Python/math.py" rename it ,or move quadratic.py to another directory.

Upvotes: 0

Edmon
Edmon

Reputation: 4872

math is a standard Python library so your code should work as is.

To confirm run:

$python

>>import math

See what you get.

It seems like you are masking math library with your own definition in local directory.

Delete anything local that looks like math.py or math.pyc and try again.

Run

Upvotes: 0

casevh
casevh

Reputation: 11414

You have a file called "math.py" located in :/User/andrewmetersky/Desktop/Programming/Python" that is found before Python's own math module. Rename your file and deleted the matching .pyc file and everything should work again.

Upvotes: 2

Related Questions