简体   繁体   English

努力让 Django 服务器在虚拟环境中运行

[英]Struggling to get Django server to run in virtual environment

I'm new to using Django & Python and am having some difficulty trying to get my manage.py application to run in a virtual environment.我是使用 Django 和 Python 的新手,并且在尝试让我的 manage.py 应用程序在虚拟环境中运行时遇到了一些困难。

I am receiving the following errors at the top of my models.py file:我在 models.py 文件的顶部收到以下错误:

I initially thought that this was an issue with pylint not pointing at the right environment (when I switch my Python interpretter to the one outside of the virtual environment, the pylint errors disappear);我最初认为这是 pylint 没有指向正确环境的问题(当我将 Python 解释器切换到虚拟环境之外的解释器时,pylint 错误消失了); however, whenever I try to run my server, I get the following error:但是,每当我尝试运行服务器时,都会出现以下错误:

(.venv) tjmcerlean@LAPTOP-RKLCBQLO:/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce$ python manage.py runserverTraceback (most recent call last):
  File "manage.py", line 10, in main
    from django.core.management import execute_from_command_line
  File "/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce/.venv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
    from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 12, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

This error appears to suggest that the virtual environment is not activated.此错误似乎表明虚拟环境未激活。 However, I activated it using the location of the virtual environment (.venv/bin/activate) and saw that the bash terminal now displays (.venv):但是,我使用虚拟环境的位置 (.venv/bin/activate) 激活它并看到 bash 终端现在显示 (.venv):

Additionally, when using the (pip freeze) command, I can see that Django is installed within the virtual environment too:此外,当使用 (pip freeze) 命令时,我可以看到 Django 也安装在虚拟环境中:

I'm lost as to why the server won't run and would really appreciate any advice.我不知道为什么服务器不会运行,并且非常感谢任何建议。

Thank you!谢谢!

Calling django.__file__ in activated env reveals the real path:在激活的 env 中调用django.__file__显示真实路径:

python -c "import django; print(django.__file__)"

it should looks like /usr/local/lib/python3.8/site-packages/django/__init__.py then re-check PYTHONPATH environment variable.它应该看起来像 /usr/local/lib/python3.8/site-packages/django/__init__.py 然后重新检查 PYTHONPATH 环境变量。

Maybe you've installed 2 Django in venv and system-wide but in some of them there is no db .也许您已经在 venv 和系统范围内安装了 2 Django,但其中一些没有db And also remove all of *.pyc files in your working folder.并删除工作文件夹中的所有*.pyc文件。

The problems occur when you don't include a path in virtualenv.当您在 virtualenv 中不包含路径时会出现问题。

Try adding the path of python on your vscode.尝试在你的 vscode 上添加 python 的路径。 Or, try to install and uninstall the Django projects.或者,尝试安装和卸载 Django 项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM