Getting error ImportMismatchError while running py.test

后端 未结 6 1639
不知归路
不知归路 2021-01-31 07:20

When I am running tests locally its working fine, but after creating the docker and running inside the container I am getting below error.

    /usr/local/lib/pyt         


        
相关标签:
6条回答
  • 2021-01-31 07:29

    Delete all the .pyc files. You can do this by find . -name \*.pyc -delete

    0 讨论(0)
  • 2021-01-31 07:30

    I have fixed it by removing all __pycache__ pkg under test/ directory, the issue was when I was creating docker image its picking all my __pycache__ and *.pyc files too, at the time when test are running its using my local machine path instead of the path in docker container.

    Conclusion: Clear your *.pyc and __pycache__ files before creating a docker image.

    0 讨论(0)
  • 2021-01-31 07:42

    Found __pycache__ files in coverage/fullcoverage/ which are hidden in jupyter notebook etc.

    simply navigate to the folder and use rm -r __pyache__/ . This will take care of your pytest.

    0 讨论(0)
  • 2021-01-31 07:46

    I am using Python 3.6. In my case, I was getting ImportMismatchError in modules with the same name under different packages, e.g., A/B/main.py and C/D/main.py. Python 3 does not require __init__.py file in source folders, but adding __init__.py under A/B and C/D solved the issue.

    0 讨论(0)
  • 2021-01-31 07:47

    You can use the .dockerignore file to exclude all __pycache__ folders from being sent to the docker image context:

    .dockerignore file, excludes __pycache__ folders and *.pyc files from all sub/folders:

    **/__pycache__
    **/*.pyc
    
    0 讨论(0)
  • 2021-01-31 07:48

    You can set environment variable PY_IGNORE_IMPORTMISMATCH=1 to skip this errros. It should be fine in simple cases like running tests inside and outside docker container.

    0 讨论(0)
提交回复
热议问题