Fix Python – Python module with a dash, or hyphen (-) in its name


Asked By – Skip Huffman

I have an existing python module with a dash in its name,

Changing the module name is something I would prefer to avoid as the module is shared, and I would have to chase down all the places it is used so that my special case will work.

Is there a way to load a module whose name contains the typically forbidden ‘-‘?

(I do understand that this isn’t a best practice. But for this situation I would prefer not to redesign and test a much larger set of applications. Also I don’t think my corporate masters would approve of my taking the time to implement such a change.)

Now we will see solution for issue: Python module with a dash, or hyphen (-) in its name


You can do that using __import__(). For example:

foobar = __import__("foo-bar")

But you really should rename the module instead. That way you can avoid confusion where the filename of the module is different from the identifier used in the program.

This question is answered By – nandhp

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0