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.
Related
Hi im trying to run a venv for my project. Everythin goes well install requirments and creating a my venv. The problem is that when i try to make a manage.py migrate i get this error. Ive looked everywhere and i got python version 3.8 installed. Pathlib is part of the package from python 3.4.
I use Windows and python 3.8 with Django version 3.0.12- My project is based on a cockiecutter and i dont want to update to the latest version om django.
Does anone know what the problem is and how it can be solved?
manage.py migrate
Traceback (most recent call last):
File "E:\Dev\Fooders\fooders\manage.py", line 4, in <module>
from pathlib import Path
ImportError: No module named pathlib
Try:
python manage.py migrate
# or if it does not work
python3 manage.py migrate
I am using Docker to launch my app which imports django. I want to do it inside my git repository.
My Dockerfile looks like
FROM python:3
WORKDIR /configurator
CMD virtualenv -p python3.8 venv
CMD . venv/bin/activate
CMD pip install -U setuptools pip
CMD pip install django
COPY . /configurator/
CMD cd cconfigurator/
ENTRYPOINT /configurator/cconfigurator/manage.py runserver 8000
And I see an error:
docker run configurator
Traceback (most recent call last):
File "/configurator/cconfigurator/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:
Traceback (most recent call last):
File "/configurator/cconfigurator/manage.py", line 21, in <module>
main()
File "/configurator/cconfigurator/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?
What am I doing wrong?
I installed spyder on my ubuntu system From which django is not working. The error it is showing when i ran the server the error showing is is
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
import django
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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 need both spyder for OpenCV and also Django. can anyone help
It is working after doing
python3 -m pip install django
thank u mr sahil
The error means that the Django is missing from your system, if you have installed the dependencies in your virtualenv you will need to activate that, or you can just do
pip install django --user
If you have a virtualenv do this,
Linux
source path/to/venv/bin/activate
Windows
path/to/venv/bin/activate
First create a virtual environment, using the following command
python3 -m virtualenv venv
If you get an error saying something similar to this, Virtualenv module not found, you will need to install virtualenv using pip by the following command.
pip3 install virtualenv
Then use the aforementioned command to create the virtual environment.
To activate it just use
source venv/bin/activate
This will activate the virtual environment. Now install Django on it.
pip install django
You don't need to specify a pip version (say pip3) because the virtual environment is created in python3 so pip defaults to pip3 itself.
Now finally run your server
python manage.py runserver localhost:8080
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
I'm trying to deploy a django app as war (to use with JBOSS server). I have seen the documentation and I made this:
jython manage.py builder --include-java-libs=/usr/share/java/jython/jython.jar
And I have this error:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
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?
What should I do?
Thanks.
You have not activated that environment where you installed Python and other dependent packages for application.
Activate the environment and then run:
jython manage.py builder --include-java-libs=/usr/share/java/jython/jython.jar
Then it will work for you.
Try this:
jython -m pip list
In the list check the package name django is there or not. Also, check for package django-jython.
To install this package to jython installation directory, run this command:
jython -m pip install django
jython -m pip install django-jython
To build war file, use this command:
jython manage.py buildwar --include-java-libs=/path/to/jython-standalone.jar