Python ImportError:没有名为'convert.py'的模块

Zig*_*ggy 2 python importerror

我尝试在网站上搜索类似的问题,但似乎没有一个我的确存在两难.

有关Python的教程,我编写了一个将摄氏温度转换为华氏温度的程序.当我在IDLE shell中运行它时,程序可以正常工作,但会返回错误.这是程序本身的代码:

#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();
Run Code Online (Sandbox Code Playgroud)

这是错误.

>>> 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
Run Code Online (Sandbox Code Playgroud)

我使用的是Python 3.4.1.

iCo*_*dez 6

导入Python模块时不添加文件扩展名.你应该做:

>>> import convert
Run Code Online (Sandbox Code Playgroud)

这段代码:

>>> import convert.py
Run Code Online (Sandbox Code Playgroud)

告诉Python导入py位于convert包中的不存在的模块.

以下是在Python中导入模块和包的参考.