2

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.

George Shuklin
  • 6,952
  • 10
  • 39
  • 80
  • 1
    Does this answer your question? [How to import module when module name has a '-' dash or hyphen in it?](https://stackoverflow.com/questions/8350853/how-to-import-module-when-module-name-has-a-dash-or-hyphen-in-it) – Maurice Meyer Aug 08 '21 at 10:57
  • 1
    Unfortunately, no. I wanted to load 'from myself' (current package) and examples in answers on the link are quite different from that. – George Shuklin Aug 08 '21 at 17:07

0 Answers0