简体   繁体   中英

Python Django , Couldn't import Django?

I have been learning django 3.x and I set up all necessary staff.

I activated my virtual enviroment and it works, but when I try to run this command it gives me this error:

Traceback (most recent call last):
  File "C:\Users\dzhiv\Dev\django-bootcamp\manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

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

Traceback (most recent call last):
  File "C:\Users\dzhiv\Dev\django-bootcamp\manage.py", line 22, in <module>
    main()
  File "C:\Users\dzhiv\Dev\django-bootcamp\manage.py", line 13, 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?
  • I have python on path enviroment.(If I had not, cmd would not show me when I type python) I created virtual envrioment(python -m venv . ) I started django project(django-admin startproject ...) I activate virtual enviroment with (.\\Scripts\\activate) and it works. When I run this (python manage.py createsuperuser) command I get errors.

So I am on a windows 10 pc and I used powershell or VsCode's terminal(same, I know).

What should I do ?

Based on the question, you did not install Django in the virtual environment. After activating the virtual environment, you install this with:

# activated virtual environment
pip install 

Note that the django-admin command, is not part of the Django library in Python. This is essence just a command that will create some files in the directory. Furthermore even if you installed the Django Python library globally , then it is not (per se) installed in the local environment. The idea of a local environment is that you run a dedicated Python version with a dedicated set of packages, not the ones installed globally on the system.

也许您可以在此社区的以下链接中找到您的问题解决方案,已经为此类问题提供了答案ModuleNotFoundError: No module named 'django' problem in vs code

Make sure you run the command pip install django after activating your virtual environment. Then you can run the django-admin commands successfully.

通过在 cmd workon yourenvname 中写入来激活您的虚拟环境,然后编写 pip install django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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