Skip to content

Cannot import name 'MySqlContainer' from 'testcontainers' #45

Closed

Description

@curiousjazz77

I recently installed this library and also installed a few downstream dependencies that other users mentioned in old issues. I am running python 3.7.

I have the following file:

import sqlalchemy
from testcontainers import MySqlContainer


@pytest.fixture(scope="session")
def mysql():
    mysql = MySqlContainer('mysql:5.7.17').start()
    engine = sqlalchemy.create_engine(mysql.get_connection_url())
    yield engine
    mysql.stop()


def test_with_mysql(mysql):
    result = mysql.execute("select version()")
    for row in result:
        assert row[0] == '5.7.17'

When I attempt to run pytest test_mysql.py, I get:

============================= test session starts ==============================
collected 0 items / 1 errors

==================================== ERRORS ====================================
________________________ ERROR collecting test_mysql.py ________________________
ImportError while importing test module '../test_mysql.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_mysql.py:4: in <module>
    from testcontainers import MySqlContainer
E   ImportError: cannot import name 'MySqlContainer' from 'testcontainers' 

Same with python3 test_mysql, I get:

ImportError: cannot import name 'MySqlContainer' from 'testcontainers' (/Users/<redacted>/Library/Python/3.7/lib/python/site-packages/testcontainers/__init__.py)

Activity

  1. tillahoffmann commented on Dec 15, 2019

    @tillahoffmann
    Contributor
  2. curiousjazz77 commented on Dec 29, 2019

    @curiousjazz77
    Author

    ^ updated!

  3. tillahoffmann commented on Dec 29, 2019

    @tillahoffmann
    Contributor

    Thanks for the example! By default, the testcontainers package does not import all submodules because they may require additional dependencies. For example, the testcontainers.mysql submodule depends on sqlalchemy which is not installed by default.

    You should be able to fix your code by running from testcontainers.mysql import MySqlContainer instead. Make sure to install the package using pip install testcontainers[mysql] to install the required dependencies. You may need to use pip install "testcontainers[mysql]" depending on your shell.

    I'll close this issue for now, but feel free to reopen it if you continue to have difficulties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions