-1

I have a module called generalPy and i want to access the python files in that folder. So i import it:from generalPy import *

The specific one i want to access is called fractions.py. It has a class with the name: Fraction. I try to create an instance of the class:fract = Fraction(). Btw, it doesn't take any parameters. When i do this, i get an error:

NameError: name 'Fraction' is not defined

why cant i access that class when it is in the generalPy folder, and has been imported?

Jonas
  • 1
  • 1
    I understand genaralPy is a package, not a module, whereas fractions.py is a module within that package. Did you try fract = fractions.Fraction()? – Tobias Brösamle Oct 25 '17 at 14:50
  • try `fract = fractions.Fraction()`. Also, refer https://stackoverflow.com/questions/41276067/importing-class-from-another-file – yash Oct 25 '17 at 14:51
  • Just tried. NameError: name 'fractions' is not defined – Jonas Oct 25 '17 at 15:01

1 Answers1

2
from generalPy import *

This imports all the names from generalPy directly into the module’s namespace. Generally not a good idea as it leads to ‘namespace pollution’. If you find yourself writing this in your code, you would maybe be better off with just using:

import generalPy

Source and further reading: https://bytebaker.com/2008/07/30/python-namespaces/

NikoCon
  • 99
  • 1
  • 2
  • 10