简体   繁体   中英

Why does Visual Studio Code with Python extension not import "fractions" library correctly?

When using Visual Studio Code with the Python extension, I am getting errors when trying to import 'fractions' Python library. This error does not occur with IDLE, CMD/Terminal, or other IDEs like Spyder.

Python script:

from fractions import Fraction as frac
print(frac(10,100))

Error:

  File "c:\PyCode\LearningScripts\fractions.py", line 1, in <module>
    from fractions import Fraction as frac
**ImportError: cannot import name 'Fraction'**

In my environment, it works properly.

Therefore, I would recommend you to change the name of your file to something else since 'fractions.py' is a built-in module , and delete the folder __pycache__ just in case.

Then, try to run it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM