No module named 'djangosite' when trying to use manage.py - django

I am using Django 3.0 and python 3.7 to run my Django server, it has only just stopped working and was working before.
The full error is here That was running python3 manage.py runserver and it is the same for any commands with manage.py
I have tried with and without my virtual environment and have reinstalled Django but it hasn't changed
the only place where djangosite is mentioned is in manage.py which I haven't changed
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangosite.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
Django is installed and I can run other sites
File tree is here

I had incorrect file structure. The settings.py was in the main folder with manage.py
It should have been in the mysite folder that has the main urls.py etc.
I fixed it by just re-creating the Django project and copying my apps and static file directory

You don't have Django installed or it is not available in your PYTHONPATH.
Perhaps you just did not activate your virtualenv in command shell.

Related

How i can running django project?

I can run my django project only on my own computer. If i copy all files on another computer and try to run it, I get error.
Virtualenv is running successfully, but then i get this message "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 tried to install django on other computers too, but it isnt helps. What is wrong?
Project
.idea
__pycatch__
main
Project
templates
venv
db.sqlite3
manage
start
start.bat
#echo off
cmd /k "cd /d C:\Users\[user]\PycharmProjects\Project\venv\Scripts & activate.bat & cd /d C:\Users\[user]\PycharmProject\Project & python manage.py runserver"
manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Project.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
I could solve this problem. If somebody has same problem I replace "python manage.py runserver" with "py -m manage runserver"

Django-admin collectstatic is an unknown command

Trying to collect my static files as I will be deploying my website soon. Receiving:
Unknown command: 'collectstatic'
After checking django-admin help I receive this warning:
(error: Requested setting INSTALLED_APPS, but settings are not configured.
You must either define the environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.).
Here is my manage.py:
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xwordsite.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
I have tried running manage.py shell, which did not solve the issue.
Also I'm unsure if it matters but all my static files are in the project dir.
Also static settings:
STATIC_URL = '/static/'
STATIC_ROOT = "C:/Users/Joseph/Desktop/xwordsite/static"
As per the docs:
Just write python manage.py collectstatic and it will work.
In this page, at first says django-admin collectstatic but end of the section there is a code block that says manage.py collectstatic. At first it confused me. I did try to solve this error and tried to configure settings etc. which did not worked.
After all these effort, suddenly it hit me and i wrote manage.py collectstatic and it worked.

ModuleNotFoundError: No module named 'django' while running manage.py

I have installed virtualenv and then installed django in my Windows 10. After activating virtualenv and running: python manage.py runserver, I am getting:
File "manage.py", line 10, 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:
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?
Also just found while running django-admin.exe I am getting:
Note that only Django core commands are listed as settings are not properly
configured (error: Requested setting INSTALLED_APPS, but settings are not
configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing
settings.).
Manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wordcount.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
You can verify whether you have installed django by creating a python program and importing django
import django
print (django.VERSION)
Above code will print the django version installed , confirm whether you have installed django
You have 2 Python versions : The main one which is installed by default and the one used by virtualenv.
When you run pip install django Django is installed in the main version of Python and thats because the PYTHONPATH environment variable refers to path of the main version and not virtualenv.
The same thing happens when you run python manage.py runserver. It doesn't run python from the virtualenv.
To solve this you need to access pip from the virtualenv and then you can insall Django with it
C:\the\path\to\virtualenv\path\to\pip.exe install django
And just like pip, run python.exe from virtualenv
C:\the\path\to\virtualenv\path\to\python.exe manage.py runserver
If you are using PyCharm for development then you can easily set your venv as interpreter.
Now to run django and just like pip you will access python from virtualenv
1 - File > Settings > Your Project > Python Interpreter
2 - Click the settings icon at the right and then click on Add
3 - Click on Virtualenv Environment and choose the location
Once the virtualenv has been setup you can easily use PyCharm to manage packages

Should I version control manage.py in Django?

Using this command:
django-admin.py startproject mysite
The following directory structure is generated:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Should I version control manage.py which contains:
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '[REDACTED].settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
Looking at the contents of the file, I don't think I need to, but just to be sure, I'm asking you lovely people :)
I think you should just add it to version control. You don't need to, but there's no reason you shouldn't version it.
Is it a part of your application: Yes, other developers need to use manage.py, so perhaps it should be left in version control so everyone is always using the same manage.py file.
Could manage.py change over time: Yes, while most people might not modify manage.py, it can be modified if you ever feel a need to so it should probably be versioned.
Does it contain any secrets or credentials: No, because there are no secrets it in, there's no reason not to version it.

error running python manage.py on ubuntu

Please help, I am fairly new to django.
I am using virtualenv (which has django installed) I have used this command to create a new project
django-admin startproject projectname
and consequently used the code
cd projectname.
But when I run python manage.py
I get this error:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django'
this is how my manage.py file looks like:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstdjango.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Install virtual environment in your system
sudo apt-get install virtualenv
Create virtual environment
virtualenv envirnment_name
Activate virtual environment by following command
source envirnment_name/bin/activate
Install django in virtual environment
pip install django
And then run your django server
pyhton manage.py runserver
First, make sure that you are working on the virtualenv that you created using workon your_virtual_env.
Second, try the pip freeze > requirements.txt so you can check the installed packages. The file should have a structure like this:
Django == 1.10.2
SomePackage == 1.2.3
SomeOtherPackage == 1.2.3
You can verify that the Django package is installed this way.