I can't run tests

时间:2019-04-08 13:45:24

标签: django

I'm trying to run my tests, on my virtual environment, so when I run the command:

python server/manage.py test --settings=skillcorner.settings_test api

I get the following Error:

> File "server/manage.py", line 8
>      print(f'Running with settings: {os.environ["DJANGO_SETTINGS_MODULE"]}')
>                                            ^
>      SyntaxError: invalid syntax

so I run (on my virtual environment) the command (I just used python3 instead of python: python3 server/manage.py test --settings=skillcorner.settings_test api

I get the following error, which is the one I am stuck on:

> Running with settings: skillcorner.settings_local
>      Traceback (most recent call last):
>      File "server/manage.py", line 12, in <module>
>     execute_from_command_line(sys.argv)
>      File "/home/yosra/.local/lib/python3.6/site-packages/django/core/management/__init__.py",
> line 381, in execute_from_command_line
>     utility.execute()
>     File "/home/yosra/.local/lib/python3.6/site-packages/django/core/management/__init__.py",
> line 357, in execute
>     django.setup() File "/home/yosra/.local/lib/python3.6/site-packages/django/__init__.py",
> line 24, in setup
>     apps.populate(settings.INSTALLED_APPS)
>     File "/home/yosra/.local/lib/python3.6/site-packages/django/apps/registry.py",
> line 91, in populate
>     app_config = AppConfig.create(entry)
>     File "/home/yosra/.local/lib/python3.6/site-packages/django/apps/config.py",
> line 90, in create
>     module = import_module(entry)
>     File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
>     return _bootstrap._gcd_import(name[level:], package, level)
>     File "<frozen importlib._bootstrap>", line 994, in _gcd_import
>     File "<frozen importlib._bootstrap>", line 971, in _find_and_load
>     File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
>     ModuleNotFoundError: No module named 'rest_framework

I can't figure out how to fix this, I tried to install rest-framework using ppip install, but it doesn't work!

1 个答案:

答案 0 :(得分:0)

我无法真正确定您如何管理虚拟环境,但是我很确定问题确实存在。我会假设以下内容:

  • 您遇到的第一个错误确实是由于python 3.6引入了f'{something}'格式(因此在python2中引起了错误)
  • 您收到的第二个错误是因为在运行Python3的环境中缺少rest_framework

我的建议:

  • 删除整个环境文件夹,并确保您deactivate
  • 在终端中运行python3 -m venv venv以创建新环境
  • 激活它:source venv/bin/activate
  • requirements.txtpip install django djangorestframework安装您的依赖项(以及项目的任何其他依赖项)
  • 然后您可以简短地进行python manage.py whatevercommand./manage.py whatevercommand(如果激活了环境,则两者都将运行python3)

最后一个提示:您可以使用python --version

检查当前的python版本(在env或jsut中的任何位置)