2

My code executes correctly using python3, but using coverage3 returns an ImportError when importing a package I created.

My project looks as follows:

components/common/ConfigTest/ConfigTest.py -> file I want to execute - a test for the Config class declared in Config.py

components/common/Config.py -> file included in ConfigTest.py

The environment variable PYTHONPATH points to the location of the components directory so I can use it as base directory for my imports.

The ConfigTest.py file starts as follows:

import os
import unittest

from common.Config import Config

While located in the ConfigTest directory, running the following command produces the expected output:

python3 -m unittest ConfigTest.py

However, either running coverage3 run ConfigTest.py or coverage3 run -m unittest ConfigTest.py produce the following error:

Traceback (most recent call last):
File "ConfigTest.py", line 7, in <module>
from common.Config import Config
ImportError: No module named 'common.Config'

In the past I experienced similar problems when executing my code on different machines/different versions of Python, and these problems were caused due to the wrong setup of the PYTHONPATH environment var. In this case, I printed print(os.environ['PYTHONPATH']) before doing the problematic import, and it prints the correct value of such var when using python3 and coverage3.

Any clue on what the problem could be? Thanks.

2
  • Do you have multiple python installed? import sys print sys.executable print "\n".join(sys.path) add this inside ConfigTest.py on the top and see the path of the python that is being invoked. Commented May 9, 2019 at 11:10
  • 1
    Hi @Vishal. Thanks for your reply. I tried and both ways it returns the same Python version. Finally, I fixed it by adding a init.py in the directories. It seems that python3 is able to find souces is directories (provided that they are accessible from PYTHONPATH), but coverage3 requires these directories to be identified as modules by setting the __init__py file. Commented May 14, 2019 at 13:23

1 Answer 1

1

I solved it by adding a __init__.py in both the source and tests directories. It seems that python3 is able to find the sources by using PYTHONPATH even if the directory has no __init__.py file, but coverage3 requires the directories to be set as modules.

This works in my current configuration, but I am not sure if it is the general Python and coverage behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

Is your init.py empty? When I add an empty init.py to the test directory and running <coverage run -m unites discover> I got the error <'unittest' is a package and cannot be directly executed.> (Note that unittest is the name of my folder for testing.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.