I want to import foo-bar.py, this works:
foobar = __import__("foo-bar")
This does not:
from "foo-bar" import *
My question: Is there any way that I can use the above format i.e., from "foo-bar" import * to import a module that has a - in it?
In Python 2, you can’t.
foo-baris not an identifier. rename the file tofoo_bar.pyIt’s possible since Python 3.1+, see Julien’s answer.
If
importis not your goal (as in: you don’t care what happens withsys.modules, you don’t need it to import itself), just getting all of the file’s globals into your own scope, you can useexecfile