How to import module when module name has a '-' dash or hyphen in it?

后端 未结 5 1946
遇见更好的自我
遇见更好的自我 2020-11-28 03:27

I want to import foo-bar.py. This works:

foobar = __import__(\"foo-bar\")

This does not:

from \"foo-bar\" import *
<         


        
5条回答
  •  半阙折子戏
    2020-11-28 04:08

    If you can't rename the module to match Python naming conventions, create a new module to act as an intermediary:

     ---- foo_proxy.py ----
     tmp = __import__('foo-bar')
     globals().update(vars(tmp))
    
     ---- main.py ----
     from foo_proxy import * 
    

提交回复
热议问题