How to import python files with hyphens


Not a good idea to name python files with hyphens. If you can, rename. Use underscores, not hyphens.

Sometimes it’s not feasible to rename and you just want to import. Can’t use “from some-package import some_method”. Instead you load the whole file as a named object, like this:

$ python
Python 3.4.2 (default, Feb 10 2015, 19:41:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> some_package = __import__('some-package')
>>> results = some_package.some_method()