Python Module with a dash, or hyphen (-) in its name
I have an existing python module with a dash in its name, foo-bar.py
Changing the module name is something I would prefer to avoid as the module is shared, and I would have to chase down all the places it is used so that my special case will work.
Is there a way to load a module whose name contains the typically forbidden '-'?
(I do understand that this isn't a best practice. But for this situation I would prefer not to redesign and test a much larger set of applications. Also I don't think my corporate masters would approve of my taking the time to implement such a change.)
You can do that using __import__()
. For example:
foobar = __import__("foo-bar")
But you really should rename the module instead. That way you can avoid confusion where the filename of the module is different from the identifier used in the program.
I know this question has already been answered to satisfaction of the asker, but here is another answer which I believes has some merit above using __import__()
.
import importlib
mod = importlib.import_module("path.to.my-module")
# mod.yourmethod()
According to the docs:
"This provides an implementation of import which is portable to any
Python interpreter. This also provides an implementation which is
easier to comprehend than one implemented in a programming language
other than Python."
Python 2.7
+ only
참고URL : https://stackoverflow.com/questions/7583652/python-module-with-a-dash-or-hyphen-in-its-name
'Nice programing' 카테고리의 다른 글
How to run .jar file by double click on Windows 7 64-bit? (0) | 2020.10.21 |
---|---|
AngularJs .$setPristine to reset form (0) | 2020.10.21 |
Getting binary (base64) data from HTML5 Canvas (readAsBinaryString) (0) | 2020.10.21 |
Passing parameters on JQuery .trigger (0) | 2020.10.21 |
How do you Hover in ReactJS? - onMouseLeave not registered during fast hover over (0) | 2020.10.21 |