PyCharm 1.1 - 使用 django 测试运行程序的特定设置

发布于 2024-10-09 09:22:27 字数 3538 浏览 2 评论 0原文

有人有在 PyCharm 中为 Django 测试运行器使用不同设置文件的经验吗?

我的项目组成如下:

+penguinproject
+apps
+settings
--__init__.py
--common.py # this one is imported by the others below
--development.py
--production.py
--staging.py
+static
...

使用manage.py命令运行测试(并添加--settings=settings.development)工作正常,但我想使用内置的测试运行器(因为它与IDE更好地集成) )。

我认为问题是我的设置没有正确加载,因为我收到此错误:

E:\Development\django_projects\penguinproject\Scripts\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py" --client 127.0.0.1 --port 49898 --file "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py" test items
Testing started at 20:36 ...
pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower)
pydev debugger: starting
E:\Development\django_projects\penguinproject\lib\site-packages\path.py:32: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
settings file:  development
pycharm django settings imported
Manager file:  manage
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 1165, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 929, in run
execfile(file, globals, locals) #execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py", line 15, in <module>
run_module(manage_file, None, '__main__')
File "c:\python26\Lib\runpy.py", line 140, in run_module
fname, loader, pkg_name)
File "c:\python26\Lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "E:\Development\django_projects\penguinproject\yabe\manage.py", line 11, in <module>
execute_manager(settings)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
utility.execute()
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\base.py", line 220, in execute
output = self.handle(*args, **options)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\commands\test.py", line 37, in handle
failures = test_runner.run_tests(test_labels)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\test\simple.py", line 396, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\test\simple.py", line 285, in build_suite
app = get_app(label)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\db\models\loading.py", line 140, in get_app
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
django.core.exceptions.ImproperlyConfigured: App with label items could not be found

我从命令行使用manage.py时没有收到此错误(当从命令行中调用时也是如此) IDE),这让我认为development.py设置未正确加载。调试显示,在 get_app() (loading.py 模块中)中,settings.INSTALLED_APPS 是一个空列表。

有没有人成功地使用此设置文件设置来设置测试运行器?

非常感谢!

Does anyone have any experience with using different settings files for the Django test runner in PyCharm?

My project is composed as follows:

+penguinproject
+apps
+settings
--__init__.py
--common.py # this one is imported by the others below
--development.py
--production.py
--staging.py
+static
...

Running the tests with a manage.py command (and adding --settings=settings.development) works fine, but I'd like to use the built in test runner (as it's better integrated with the IDE).

I think the problem is that my settings aren't being loaded correctly, as I get this error:

E:\Development\django_projects\penguinproject\Scripts\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py" --client 127.0.0.1 --port 49898 --file "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py" test items
Testing started at 20:36 ...
pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower)
pydev debugger: starting
E:\Development\django_projects\penguinproject\lib\site-packages\path.py:32: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
settings file:  development
pycharm django settings imported
Manager file:  manage
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 1165, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 929, in run
execfile(file, globals, locals) #execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py", line 15, in <module>
run_module(manage_file, None, '__main__')
File "c:\python26\Lib\runpy.py", line 140, in run_module
fname, loader, pkg_name)
File "c:\python26\Lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "E:\Development\django_projects\penguinproject\yabe\manage.py", line 11, in <module>
execute_manager(settings)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
utility.execute()
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\base.py", line 220, in execute
output = self.handle(*args, **options)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\commands\test.py", line 37, in handle
failures = test_runner.run_tests(test_labels)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\test\simple.py", line 396, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\test\simple.py", line 285, in build_suite
app = get_app(label)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\db\models\loading.py", line 140, in get_app
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
django.core.exceptions.ImproperlyConfigured: App with label items could not be found

I don't get this error using manage.py from the command line (also when invoked from within the IDE), which makes me think the development.py settings aren't loaded correctly. Debugging shows that within get_app() (in the loading.py module) settings.INSTALLED_APPS is an empty list.

Has anyone succeeded in setting up the test runner with this setup of settings files?

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很糊涂小朋友 2024-10-16 09:22:27

我建议对您的项目结构稍作修改。查看这个更详细的示例:

http ://www.revsys.com/blog/2014/nov/21/recommended-django-project-layout/

你会看到有一个顶级目录和一个同名的子目录,其中包含所有后续设置文件去。

作为另一个参考,请查看可用于创建初始项目结构的“startproject”命令:

https://docs.djangoproject.com/en/1.10/ref/django-admin/#django-admin-startproject

HTH

i'd suggest a slight modification of your project structure. check out this much, much more detailed example:

http://www.revsys.com/blog/2014/nov/21/recommended-django-project-layout/

you'll see there's a top level directory AND a subdir with the same name where all subsequent settings files go.

for yet another reference, check out the 'startproject' command that can be used to create the initial project structure:

https://docs.djangoproject.com/en/1.10/ref/django-admin/#django-admin-startproject

HTH

橘香 2024-10-16 09:22:27

您必须在该应用程序上创建一个 models.py

http://code.djangoproject.com/ticket/3310

You have to create a models.py on that application!

http://code.djangoproject.com/ticket/3310

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文