Reputation: 375
I tried searching the site for similar problems, but none seemed to have my exact dilemma.
For a tutorial book on Python, I wrote a program that converts Celsius temperatures to Fahrenheit. When I run it in the IDLE shell, the program works, but returns an error. Here is the code for the program itself:
#convert.py
#converts Celsius temperatures to Farenheit
def main():
celsius = input("Type the temperature in degrees Celsius: ")
farenheit = ((9/5)* int(celsius)) + 32
print(farenheit)
main();
And here's the error.
>>> import convert.py
Type the temperature in degrees Celsius: 59
138.2
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import convert.py
ImportError: No module named 'convert.py'; 'convert' is not a package
I am using Python 3.4.1.
Upvotes: 2
Views: 8229
Reputation: 1121554
You need to drop the .py
extension; Python imports use just the base name:
import convert
What happens is that Python imported convert
, then looks for a nested module named py
. That part fails, because convert
is not a package and cannot contain another module.
Upvotes: 1
Reputation:
You do not add the file extension when importing a Python module. You should do:
>>> import convert
This code:
>>> import convert.py
is telling Python to import the nonexistent py
module that is located in the convert
package.
Here is a reference on importing modules and packages in Python.
Upvotes: 6