Django REST Framework - Django capitalized in `pip3 list` - python

I'm building an API using Django REST Framework with this tutorial. I'm getting a ModuleNotFoundError: No module named 'django' error when I try to test python3 manage.py. I've been going through the long list of issues it could be (reinstall django, need to add an __init__.py in the root, etc) and someone recommended checking my modules list with pip3 list.
Here's what I see:
Package Version
------------------- -------
asgiref 3.3.1
dj-database-url 0.5.0
Django 3.1.4
django-filter 2.4.0
django-heroku 0.3.1
djangorestframework 3.12.2
gunicorn 20.0.4
Is there a reason why Django's capitalized? Could that be why it isn't finding it in manage.py? If so, how do I go about fixing it?
Here's the full error message I'm getting, but it's really general. The exception is hardcoded:
Traceback (most recent call last):
File "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 "manage.py", line 22, in <module>
main()
File "manage.py", line 17, in main
) from exc
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?

While working with a large framework as Django one must always work in a virtual environment. Please check if you have your virtual environment activated. If not, then follow the steps:
cd your_working_directory
virtualenv environment_name where environment_name can be any name you want.
environment_name/Scripts/activate if on Windows
Or environment_name/bin/activate if on MacOS
Then after activating the environment you can install all the dependencies using pip. i.e pip install django

Related

Django Error - Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?

When I first ran the server everything was fine and the project was running properly. But after few days when I ran the server again, it gave me an error
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Django Project\first\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\Admin\Desktop\Django Project\first\manage.py", line 22, in <module>
main()
File "C:\Users\Admin\Desktop\Django Project\first\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?
To be specific, I'm on Windows 10, using VS Code as my IDE (I've not setup any virtual environment).
Django version installed - 3.2.9
Python version installed - 3.10.0
PIP version installed - 21.2.3
Here is a screenshot of my Environment Variables
[
I don't know what the problem was. Thanks to everyone for their suggestions. I deleted the project and created a new one and it worked. I hope I won't face any such issue while working on a big project!

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.
I am receiving the following errors at the top of my models.py file:
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); 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):
Additionally, when using the (pip freeze) command, I can see that Django is installed within the virtual environment too:
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:
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.
Maybe you've installed 2 Django in venv and system-wide but in some of them there is no db. And also remove all of *.pyc files in your working folder.
The problems occur when you don't include a path in virtualenv.
Try adding the path of python on your vscode.
Or, try to install and uninstall the Django projects.

codeanywhere.com Python doesn't see virtualenv

I use codeanywhere.com try to create application with Python werkzeug and jinja2.
I install those library utilize virtualenv. Now I face the problem
Traceback (most recent call last):
File "manage.py", line 4, in
from werkzeug import Request
ImportError: No module named 'werkzeug'
virtualenv activated but nothing is works.
Does anybody has experience with codeanywhere.com?
Oh I solve this problem. In the container config file add instruction "source venv3/bin/activate" as shown on a picture add path to python as well.
enter image description here

Configuring Django 1.7 and Python 3 on mac osx 10.9.x

I have installed the latest versions of both django and python. The default "python" command is set to 2.7; if I want to use python 3, I have to type "python3".
Having to type "python3" and a django command causes problems. For example if I type: "python3 manage.py migrate" , I get an error. The error is:
Traceback (most recent call last):
File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named 'django'
Django does not seem to recognize my python 3. How do I get around this? Your help is greatly appreciated.
You need to install django for python 3, pip3 install django

Django installation has no __init__.py in django.core

I have a ubuntu lucid VPS server where I try to run my django development server.
Running python manage.py runserver gives me the following error:
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named core.management
I can import django without any problem in a python shell. I looked in the django install directory and noticed there is no \__init__.py in the django/core folder. Which I beleive is the source of the problem for python to register django.core as a module.
It then looks like an installation issue. I installed django using apt-get.
FYI the django install worked perfectly on my home computer with same OS.
Any ideas?
solved.
Thank you for all the suggestions. I installed django using the tar ball as instructed in the django website.
apt-get is not a good idea to install django. I also had to manually remove the left over django folder in /usr/lib/pymodules/python2.6 after using apt-get remove.

Categories

Resources