gpt4 book ai didi

python - 使用错误的 python 路径覆盖,尽管 venv 是来源

转载 作者:行者123 更新时间:2023-12-04 14:48:52 35 4
gpt4 key购买 nike

我正在处理 Django 项目并尝试运行命令 coverage run --source='.' manage.py test myapp来自 Django documentation .不知何故,coverage 使用了与我所在的虚拟环境不同的 python 路径(我想这就是找不到模块的原因)
我得到以下输出:

(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'
我正在使用常规 python 安装中的 venv,并通过在我的 settings.py 中导入一个不存在的模块来检查我的 venv:
来自 testmodule import test ,这为我提供了以下输出 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'
对我来说,问题似乎与 django 或 python-decouple 模块无关,但覆盖范围不使用我的源 venv 的 python 路径。我试图搜索 docs从覆盖范围,但无法识别运行时使用的函数或方法 coverage run有人可以帮我看看在哪里或如何确定问题吗?

最佳答案

这似乎与您的全局 python 包有冲突。
正如您的日志所示,coverage来自 /Users/username/.pyenv/versions/3.7.2/ .
而你想使用 /Users/username/project/venv/lib/python3.9/ .
有两种方法可以做到这一点,卸载全局覆盖。

(venv) $ deactivate
$ pip3 uninstall coverage
$ source venv/bin/activate
(venv) $ python -m pip install coverage
或者,如果您打算保留全局包,您可以试试这个。
(venv) $ python -m pip install coverage
(venv) $ python -m coverage
这将获得关于 /Users/username/project/venv/lib/python3.9/ 的包

关于python - 使用错误的 python 路径覆盖,尽管 venv 是来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69435182/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com