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

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

    Starting from Python 3.1, you can use importlib :

    import importlib  
    foobar = importlib.import_module("foo-bar")
    

    ( https://docs.python.org/3/library/importlib.html )

提交回复
热议问题