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

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

    Like other said you can't use the "-" in python naming, there are many workarounds, one such workaround which would be useful if you had to add multiple modules from a path is using sys.path

    For example if your structure is like this:

    foo-bar
    ├── barfoo.py
    └── __init__.py
    
    
    import sys
    sys.path.append('foo-bar')
    
    import barfoo
    

提交回复
热议问题