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?
If you can't rename the original file, you could also use a symlink:
Then you can just:
you can't.
foo-bar
is not an identifier. rename the file tofoo_bar.py
Edit: If
import
is 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
If you can't rename the module to match Python naming conventions, create a new module to act as an intermediary:
Starting from Python 3.1, you can use importlib :
( https://docs.python.org/3/library/importlib.html )