You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot import name 'MySqlContainer' from 'testcontainers' #45
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)
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.
Activity
tillahoffmann commentedon Dec 15, 2019
Hi @curiousjazz77, can you provide a minimal reproducible example?
curiousjazz77 commentedon Dec 29, 2019
^ updated!
tillahoffmann commentedon Dec 29, 2019
Thanks for the example! By default, the
testcontainerspackage does not import all submodules because they may require additional dependencies. For example, thetestcontainers.mysqlsubmodule depends onsqlalchemywhich is not installed by default.You should be able to fix your code by running
from testcontainers.mysql import MySqlContainerinstead. Make sure to install the package usingpip install testcontainers[mysql]to install the required dependencies. You may need to usepip 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.