Explain Codes LogoExplain Codes Logo

How to import module when a module name contains a '-' dash or hyphen?

python
importlib
pythonic-naming-conventions
module-import
Alex KataevbyAlex Kataev·Feb 18, 2025
TLDR

In Python, importing a module with a "-" can be a little challenging. However, importlib.import_module is your breakthrough. It can dynamically import modules, including those with hyphenated names:

# When life gives you hyphenated module names! import importlib hyphen_module = importlib.import_module("module-name")

Your hyphen_module is good to go, and you can access all its attributes and methods just as you usually would.

Delving into importlib

Python’s import system is like a backstage tech wizard, enabling the magic of import. So, while the naming conventions of Python may grimace at the sight of a hyphen, importlib knows the trick!

import importlib module_with_dash = importlib.import_module("dash-named-module")

Now module_with_dash is a bona fide module object — indistinguishable from one imported using the standard import statement.

Working around unconventional names

Python naming conventions prefer snake case, but what to do when a hyphen slithers in, or another outlandish character shows up? Here's the game plan:

  • Proxy import — Create a wrapper module with a proper name that imports the poorly-named target module.

  • Filesystem symlinks — If you can't change the module name, you can still con Python into importing it:

    # A dash of trickery! ln -s module-name.py module_name.py
  • sys.path trick — Add the module's directory to sys.path, and then use importlib or __import__.

Importing modules with hyphens might lead to syntax errors, and techniques like from module-name import * won't segway nicely. Here's what to do instead:

  • Explicit attribute import: Import each attribute you need, one by one.
  • Namespace faffing: For Python 2, use execfile. For Python 3, put exec on the global namespace.

Following Pythonic naming conventions

PEP 8, Python's official style guide, prefers module names to be lowercase, with underscores. So, save yourself from concocting workarounds and write clean and consistent code to start with.

Renaming modules that 'break' rules

Give your module a makeover — flip those hyphens to underscores! This way, you’ll ensure smooth importing and maintain consistency across your codebase.

Feeling the pain of hyphenated imports

Imagine being a book, ready to be read — and then someone changes the language! That's how Python feels with hyphenated modules. Here's how you overcome this issue:

Regular Book: book_in_english - Python reads easily. Hyphenated book: book-in-german - Python can't understand due to language-hyphen-barrier!

What to do? Bring in a translator 🤔

import importlib book = importlib.import_module("book-in-german")

With importlib as your savvy interpreter, Python can understand book-in-german in no time!

📚----📘book_in_english----| 🚧 |----📘book-in-german 📚 The interpreter helps Python understand the book-in-german!