Pytest: ImportError: cannot import name 'console_main' on Docker

Created on 15 Aug 2020  路  6Comments  路  Source: pytest-dev/pytest

Yo folks,

I've tried to run a few tests including py.test on Docker, but it shows an error.
Could you help me out pls to handle this one? :)

Successfully built 1ce3c1b93e02
Successfully tagged blazemeter/selenium/standalone-chrome:latest
Traceback (most recent call last):
  File "/usr/local/bin/py.test", line 5, in <module>
    from pytest import console_main
ImportError: cannot import name 'console_main'

Dockerfile

FROM selenium/standalone-chrome
USER root

RUN chown -R $USER:$USER /bin/sh

# create project folder with the name code
RUN mkdir -p /code

# project scope
WORKDIR /code

# Update packages
RUN apt-get update

# Install pip
RUN apt-get install -y python3-pip python-dev build-essential
RUN python3 -m pip install --upgrade pip

# Install pytest and toolkit
RUN python3 -m pip install pytest
RUN pip3 install prompt_toolkit

# install requirements
COPY requirements.txt .
RUN pip3 install --user -r requirements.txt

# Set Dokcer entry
RUN pwd
COPY ./docker-entry.sh /code
ENTRYPOINT ["/code/docker-entry.sh"]

docker-entry.sh

#!/usr/bin/env bash

# run py.test ($@ to derive parameters from commandline)
py.test --alluredir=allure-results $@ &
pid="$!"

# trap process id to stop script using Control+C
trap "echo '=== Stopping PID $pid ==='; kill -SIGTERM $pid" SIGINT SIGTERM

wait $pid
question

Most helpful comment

Great call on adding the python3 -m. I had a drone-based deploy in a docker container that had been working fine without that tag for months, and then spontaneously today it decided it couldn't find console_main.

Adding the tag fixed it. Thank you, @nicoddemus

All 6 comments

Hi @organicnz,

That docker image seems to have a pytest installed in /usr/bin/py.test, is that the same pytest you installed?

I suggest changing py.test --alluredir... to python3 -m pytest --alluredir... in your docker-entry.sh file to make sure you are using the pytest you are installing.

For context:

Older pytest versions defined main as the entry point function, but we have renamed the function to console_main in recent versions. I suspect there's a version mismatch there somewhere.

Thanks for getting back to me, mate 馃憤

I've just symlinked the PATH by ln -s /home/seluser/.local/lib/python3.6/site-packages/pytest /usr/bin/pytest, hopefully it's correct.

Now it doesn't digest --env= over the run ./scripts/run_tests.bash --env=$ENV blazedemo_app/tests/test_purhcase_tickets.py -v

Successfully built 7093e14486eb
Successfully tagged blazemeter/selenium/standalone-chrome:latest
WARNING: The directory '/home/seluser/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Name: pytest
Version: 3.5.0
Summary: pytest: simple powerful testing with Python
Home-page: http://pytest.org
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/seluser/.local/lib/python3.6/site-packages
Requires: attrs, setuptools, six, py, more-itertools, pluggy
Required-by: pytest-gitignore, pytest-allure-adaptor
usage: pytest.py [options] [file_or_dir] [file_or_dir] [...]
pytest.py: error: unrecognized arguments: --env=
  inifile: None
  rootdir: /code

pytest itself doesn't take any --env flag - I'm guessing you're missing some plugin which takes that argument.

pytest itself doesn't take any --env flag - I'm guessing you're missing some plugin which takes that argument.

Thanks a lot, mate, I also tried to work around virtualenv, but looks like it didn't help if I did it right. I'll dig deeper into then :(

Closing this for now as I don't think we can help much more at the moment - if you have any follow-up questions, feel free to still post them here, though!

Great call on adding the python3 -m. I had a drone-based deploy in a docker container that had been working fine without that tag for months, and then spontaneously today it decided it couldn't find console_main.

Adding the tag fixed it. Thank you, @nicoddemus

Was this page helpful?
0 / 5 - 0 ratings