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

后端 未结 5 1926
遇见更好的自我
遇见更好的自我 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 03:42

    If you can't rename the original file, you could also use a symlink:

    ln -s foo-bar.py foo_bar.py
    

    Then you can just:

    from foo_bar import *
    

提交回复
热议问题