Relative import in the directory with dash in the name

2.8k Views Asked by At

I'm trying to build a test system with pytest/testinfra, and I use Python's relative imports to move settings from tests into a separate file (settings.py or __init__.py). All tests look similar:

from . import *


def test_port(host):
    assert host.socket(f"tcp://127.0.0.1:{ PORT }").is_listening

The problem is that if this directory with tests (the name is dictaed by application name) contains dashes (f.e. app-foo), python fails:

    from . import *
E   ImportError: attempted relative import with no known parent package

If I rename directory from app-foo to app_foo it works. But I really don't want to do this, as names are coming from external system, and renaming is very unwelcomed.

Is there any way to say python to import local file for directory with dashes?

P.S. I've tried to use from .settings import *, but it's the same. I've tried to use from settings import *, but it fails if I run tests with ../../tests path.

0

There are 0 best solutions below