为 Django 项目设置 PYTHONPATH 的正确方法
Proper way to set up PYTHONPATH for django project
在我的文件 django app tests.py
中,我导入了我创建的另一个名为 helpers
的模块。 helpers
与 tests.py
处于同一目录级别。当我 运行 ./manage.py tests
我得到这个错误:
$ ./manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: api.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
yield
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 577, in run
testMethod()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: api.tests
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
File "/Users/bli1/Development/projects/cherngloong/cherngloong/api/tests.py", line 8, in <module>
from helpers.APIHelpers import KeyGrabber
ImportError: No module named 'helpers'
这是我的项目结构:
基本上,tests.py
找不到helpers
模块。我不确定在 Django 项目中避免此问题的 best/proper/standard 方法是什么
试试这个:
api.helpers.APIHelpers import KeyGrabber
在 Django 包中通常以应用名称开头。您也可以尝试 relative imports。
在我的文件 django app tests.py
中,我导入了我创建的另一个名为 helpers
的模块。 helpers
与 tests.py
处于同一目录级别。当我 运行 ./manage.py tests
我得到这个错误:
$ ./manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: api.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
yield
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 577, in run
testMethod()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: api.tests
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
File "/Users/bli1/Development/projects/cherngloong/cherngloong/api/tests.py", line 8, in <module>
from helpers.APIHelpers import KeyGrabber
ImportError: No module named 'helpers'
这是我的项目结构:
基本上,tests.py
找不到helpers
模块。我不确定在 Django 项目中避免此问题的 best/proper/standard 方法是什么
试试这个:
api.helpers.APIHelpers import KeyGrabber
在 Django 包中通常以应用名称开头。您也可以尝试 relative imports。