Could'nt import Django - python

I started my django project without activating my virtualenv django-admin startproject my_project and django-admin startapp my_app. Everythiing went fine, till i closed my terminal, and stopped the serveer. I wanted to restart my server, but this message is still coming up. i tried to install the virtualenv still the same problem,
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?
Please what could be the problem and how to fix this ??

You should run pip install django at terminal after active the virtualenv.

Related

ImportError : Couldn't import Django

I have installed django==3.1.4 and activated Venv on my windows machine and it works fine in myBlog project. But today when I try to run the server, got this error
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?
but venv is already activated and when I do
pip freeze > requirements.txt
It shows
asgiref==3.3.4,
Django==3.1.4,
django-crispy-forms==1.11.2,
Pillow==8.2.0,
pytz==2021.1,
sqlparse==0.4.1,
I also checked sys.path, It shows
['', 'C:\Users\user\AppData\Local\Programs\Python\Python38-32\python38.zip', 'C:\Users\user\AppData\Local\Programs\Python\Python38-32\DLLs', 'C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib', 'C:\Users\user\AppData\Local\Programs\Python\Python38-32', 'C:\Users\user\Desktop\myBlog\venv', 'C:\Users\user\Desktop\myBlog\venv\lib\site-packages']
Please Help to fix this
if you are trying to run the django server from within your IDE makesure that your virtual environment is active inside the IDE as well.
thanks, everybody ..problem fixed ..probably occurred because I did some C drive repair
I got following error
ModuleNotFoundError: No module named 'django'
I did the following steps.. to overcome my error:
On my main folder Check If you have already a "requiremnts.txt" file skip this step else run this command on terminal : pip freeze > requirements.txt ,if it saves all your packages then only do other steps
Delete Venv folder from the main directory.
Again install virtual environments and activate Venv.
then run this command pip install -r requirements.txt this will install all your packages back.
Run again python manage.py runserver , It will work fine

ImportError: Couldn't import Django ... Did you forget to activate a virtual environment?

I know there are several questions/answers about this, but I can;t figure out what I should do.
I wanted to get started with Django and installed it with pip install and added Python37 and Python37-32 to my environmental variables, and I guess it worked, because I can run several Python commands in my shell.
But every time I try to
python manage.py runserver
it gives me an error.
I also set up my virtual environment and activated it, but I think there's a problem with Django. But because I installed it with pip install django I know it's there and I can use commands like django-admin startapp ... So I guess Django is working. I don't really know what PYTHONPATH means and where to find it. It would be pretty nice if anybody could take a look at my error.
Here you can see that Django is installed :
#
**C:\Users\Kampet\Desktop\Python-Django\mysite>pip install django Requirement already satisfied: django in c:\users\kampet\appdata\local\programs\ python\python37-32\lib\site-packages (2.2.4) Requirement already satisfied: pytz in c:\users\kampet\appdata\local\programs\py thon\python37-32\lib\site-packages (from django) (2019.2) Requirement already satisfied: sqlparse in c:\users\kampet\appdata\local\program s\python\python37-32\lib\site-packages (from django) (0.3.0)**
# And thats my error
**C:\Users\Kampet\Desktop\Python-Django\mysite>python manage.py runserver
Traceback (most recent call last):
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:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available o
n your PYTHONPATH environment variable? Did you forget to activate a virtual env
ironment?**
###################
Here is where my virtual environment is located.
Python-Django
-----------------mysite
-------------------------main
-------------------------mysite
-------------------------manage.py
-----------------venv
-------------------------Include
-------------------------Lib
-------------------------Scripts
-------------------------pyvenv.cfg
This is my 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', 'mysite.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 don't know why it can't find the module "django" / django.core.management
I also can't find django.core.management anywhere in my files, but I reinstalled and upgraded django several times. I don't know if this helps you.
Thank you for your time.
On the Windows machine, you should activate venv by this command .\venv\Scripts\activate (please note, you should be in the folder where this venv is)
Then inside activated venv install Django pip install django and in the same terminal run the server python manage.py runserver
I had similar issue in my windows 10 system and solved using pipenv. Steps with commands given below.
cd/Go to the project folder
Setup the virtual environment pipenv install
Activate the virtual environment pipenv shell
Install django pip3 install django
Run the project pipenv run python manage.py runserver
I had similar issue with:
django installed globally
virtual environment setup and activated
django project setup under venv activated
When trying to run the project, it could not import django anyway.
Adding path to manage.py in variables did not help but as mentioned above, installation of django under venv activated actually resolved the issue.
Your case may be different but try this scenario:
Setup venv
Activate venv
Install django
Create django proj
Try to run django proj
try running it in anaconda prompt instead of cmd, that worked for me
This error occurs when you install pip packages as a normal user other than root user. pip install <package name> --user command actually creates a directory called as .local in the users home directory under which the packages are installed.
Fix:
Find the django package installed location sudo find / -name 'django'
Copy the path from find command output.
Create export variable in ~/.bashrc file with PYTHONPATH variable.
Save the file and exit.
Exit from the normal user prompt.
Login again as the normal user.
Run the command env to check the PYTHONPATH variable set in the output.
Run the command python manage.py runserver <ip address: port>
Make sure that you are working on the command prompt.
Use this command '
workon your_env_name
'
Install Python3, Make sure its on PATH, make sure PIP is installed, Install virtualenv with PIP, setup virtualenv, activate virtualenv, install django.
Or you can try to run the command pipenv shell before start the server with py manage.py runserver in vscode terminal.
In my case, I reinstalled python to a global folder on the C drive and by mistake wrote PYTHONPATH With:\Python and not C:\Python\Scripts in global variables, but in local variables it was written correctly
enter image description here
On Windows, you should activate env by this command .\env\Scripts\activate (please note, you should be in the folder where this env is)
Then inside activated env install Django: pip install django and in the same terminal run the server: python manage.py runserver

Why Django doesn't want to create startapp?

msinfo32#dEfaaPc2:~/Django/malybar$ python manage.py startapp testApp1
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?
What i should to do?
To begin using the virtual environment, it needs to be activated
source venv_path/bin/activate
this will help you
for more details this will help you
Simple way is to make a virtual environment first & activate it and then make your project and App in its enviroment.
Follow the Following Steps to go through a less error routine:
Python -m venv newms
source newms/bin/activate
pip install django
django-admin startproject projectName
cd projectName Then python manage.py startapp appname
Here you're basically creating virtual env named newms and activating it then install django and then you can go ahead and make your project and apps inside it.

MS Windows, virtual environment, manage.py "Couldn't import Django"

When running manage.py runserver, I get the following error:
"Couldn't import Django. Are you sure it's installed and " Import Error: 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?"
My research (eg https://groups.google.com/forum/#!topic/django-developers/aXF1j55hb2Q) says this is usually a problem with either not running a virtual environment, or not installing django with pip. The questions I've find on StackOverflow receiving this error (eg Django installation on MS Windows, manage.py "Couldn't import Django") have been resolved with one of those two fixes.
I installed django in my virtual environment, and am attempting to run manage.py within that virtual environment. Django was installed with pip3. I cannot find any suggestions to troubleshoot this problem other than those two. What's the next thing to try?

Cannot create project using Django

I've uninstalled 3.3 as I was working with Django and installed Python 2.7. But now I couldn't create project. I've un-installed Django and Python both and tried different versions but every time when I try to create a project with "django-admin.py startproject abc" it gives me some error:
ImportError: No module named site
I've searched for a solution thoroughly tried different ways but this doesn't goes away. I can get into Python by typing in Python from command prompt so this isn't related to environment variable. Please help.
Here's how I resolved the same error message (ImportError: No module named site) that I got while trying this tutorial: https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html.
Deactivate the virtualenv
Install uWSGI system-wide (if not already installed system-wide)
sudo pip install uwsgi
Edit the uwsgi.ini file. I commented out the line with the
home = /path/to/virtualenv
Re-run the command: uWSGI --ini mysite_uwsgi.ini
You should use django-admin startproject PROJECT_NAME

Categories