我通过SO帖子进行挖掘,我通过随机模糊的博客进行挖掘,我似乎无法解决我的问题。
这就是我如何做到这一切:
我创建了一个很好的全新虚拟环境:
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
def fixup_paths(path):
try:
import google
google.__path__.append("{0}/google".format(path))
except ImportError:
pass
sys.path.insert(0, path)
fixup_paths(os.getenv('ENDPOINTS_GAE_SDK'))
import dev_appserver
dev_appserver.fix_sys_path()
try:
import appengine_config
(appengine_config)
except ImportError:
print('Note: unable to import appengine_config.')
sys.path.append(os.path.expandvars('$ENDPOINTS_GAE_SDK'))
sys.path.append(os.path.expandvars('$ENDPOINTS_GAE_SDK/lib/yaml/lib'))
sys.path.append('lib')
from google.appengine.ext import testbed
import endpoints
import mock
import pytest
from protorpc import message_types
import main
@pytest.fixture(scope='function', autouse=True)
def gae_testbed(request):
tb = testbed.Testbed()
tb.activate()
def deactivate():
tb.deactivate()
request.addfinalizer(deactivate)
def test_echo(gae_testbed):
api = main.EchoApi()
request = main.EchoApi.echo.remote.request_type(content='Hello world!')
response = api.echo(request)
assert 'Hello world!' == response.content
def test_get_user_email(gae_testbed):
api = main.EchoApi()
with mock.patch('main.endpoints.get_current_user') as user_mock:
user_mock.return_value = None
with pytest.raises(endpoints.UnauthorizedException):
api.get_user_email(message_types.VoidMessage())
user_mock.return_value = mock.Mock()
user_mock.return_value.email.return_value = 'user@example.com'
response = api.get_user_email(message_types.VoidMessage())
assert 'user@example.com' == response.content
我安装了所有要求:
virtualenv venv
根据我安装的LocalFlavor Documentation点pip install -r requirements.txt
但是当我尝试在我的应用程序per the django docs上运行覆盖时:
django-localflavor
我收到错误coverage run --source='.' manage.py test visitor_check_in
...
我安装的应用程序如下所示:
'No module named 'localflavor'
就在上周,我已经开始运行,但是在最近的一次会议期间,当我运行一些教程或其他东西时,我一定要搞砸了一些东西 - 但是我在其他虚拟环境中进行这些教程 - 所以我很难过
如果我移动我安装的应用程序的顺序如下:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'localflavor',
'visitor_check_in',
'django_extensions',
]
它给了我相同的没有模块发现错误 - 除了它说它找不到模块INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'visitor_check_in',
'django_extensions',
'localflavor',
]
回溯看起来像这样:
django_extensions
正如我所说 - 之前,我对此没有任何问题,但现在对于我的生活,我无法弄清楚为什么这不起作用。我有一种沉闷的感觉,那些真的简单,我忽略了,但我无法找到它。
我可以确认我使用的是Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
module = import_module(entry)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django_extensions'
和最新版本的Python 3.6
编辑添加:我可以使用pip
答案 0 :(得分:3)
因为你提到它只是在你运行New
时才发生,所以它很有可能发生,因为coverage
没有使用你的virtualenv,而是你的全局python安装。
此前已在此处发布了一个类似问题的问题 - Running coverage inside virtualenv
显然,您需要在virtualenv中安装coverage
,因为它可以按预期工作。