coverage using wrong python path, although venv is sourced

Viewed 410

I am working on a Django project and trying to run the command coverage run --source='.' manage.py test myapp from the Django documentation. Somehow coverage uses a different python path than the virtual environment I am in (I guess that is the reason that the module is not found)

I get the following output:

(venv) username in ~/project on branch basket > coverage run --source='.' manage.py test myapp
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/core/management/base.py", line 322, in run_from_argv
    parser = self.create_parser(argv[0], argv[1])
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/core/management/base.py", line 296, in create_parser
    self.add_arguments(parser)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/core/management/commands/test.py", line 44, in add_arguments
    test_runner_class = get_runner(settings, self.test_runner)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/test/utils.py", line 302, in get_runner
    test_runner_class = test_runner_class or settings.TEST_RUNNER
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/conf/__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/conf/__init__.py", line 177, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users/username/.pyenv/versions/3.7.2/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/username/project/config/settings.py", line 14, in <module>
    from decouple import config, Csv
ModuleNotFoundError: No module named 'decouple'

I am using venv from the regular python installation and checked my venv by importing a non-existent module in my settings.py: from testmodule import test, which gives me the following output for the command python manage.py runserver

(venv) username in ~/project > python manage.py runserver
Traceback (most recent call last):
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 61, in execute
    super().execute(*args, **options)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 68, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/username/project/config/settings.py", line 16, in <module>
    from testmodule import test
ModuleNotFoundError: No module named 'testmodule'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/username/project/manage.py", line 22, in <module>
    main()
  File "/Users/username/project/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/core/management/base.py", line 367, in run_from_argv
    connections.close_all()
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/db/utils.py", line 208, in close_all
    for alias in self:
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/utils/connection.py", line 73, in __iter__
    return iter(self.settings)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/utils/connection.py", line 45, in settings
    self._settings = self.configure_settings(self._settings)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/db/utils.py", line 144, in configure_settings
    databases = super().configure_settings(databases)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/utils/connection.py", line 50, in configure_settings
    settings = getattr(django_settings, self.settings_name)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/username/project/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/username/project/config/settings.py", line 16, in <module>
    from testmodule import test
ModuleNotFoundError: No module named 'testmodule'

To me the problem doesn't seem related to django or the python-decouple module, but coverage not using the python path of my sourced venv. I tried to search the docs from coverage but couldn't identify what function or method is used when running coverage run can somebody help me out on where to look or how to identify the problem?

1 Answers

This seems to be a conflict with your global python packages.

As your log suggests, coverage is from /Users/username/.pyenv/versions/3.7.2/.

And you want to use the /Users/username/project/venv/lib/python3.9/.

There are 2 ways to do this, uninstall global coverage.

(venv) $ deactivate
$ pip3 uninstall coverage
$ source venv/bin/activate
(venv) $ python -m pip install coverage

Or if you plan on keeping global package you can try this.

(venv) $ python -m pip install coverage
(venv) $ python -m coverage

This will get the packages with respect to /Users/username/project/venv/lib/python3.9/

Related