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

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

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"

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

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.

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.

Running Django in Virtualenv on EC2 -- ImportError: No module named django.core.management

I developed a django application locally, in a git repo. I launched an EC2 instance for the project and I set up a virtualenv with (what I believe to be) the correct packages/dependencies. I then proceeded to clone my repo into the virtualenv. Right now, I'm having difficulty as I'm receiving the following errors:
I attempted to use python manage.py runserver example.com/8080 to test. I was sure to activate the virtualenv using source bin/activate, just like I did in my local virtualenv. When I call ... runserver I get the following 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.core.management
Here is what sudo pip freeze produces:
Warning: cannot find svn location for distribute==0.6.24dev-r0
Cheetah==2.4.4
Django==1.5.2
Fabric==1.8.0
GnuPGInterface==0.3.2
Landscape-Client==12.05
M2Crypto==0.21.1
PAM==0.4.2
PyYAML==3.10
South==0.8.2
Twisted-Core==11.1.0
Twisted-Names==11.1.0
Twisted-Web==11.1.0
apt-xapian-index==0.44
argparse==1.2.1
boto==2.2.2
chardet==2.0.1
cloud-init==0.6.3
command-not-found==0.2.44
configobj==4.7.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
django-s3-folder-storage==0.1
django-storages==1.1.8
django-tastypie==0.10.0
ecdsa==0.9
euca2ools==2.0.0
gunicorn==18.0
httplib2==0.7.2
keyring==0.9.2
language-selector==0.1
launchpadlib==1.9.12
lazr.restfulclient==0.12.0
lazr.uri==1.0.3
medusa==0.5.4
meld3==0.6.5
oauth==1.0.1
paramiko==1.12.0
psycopg2==2.5.1
pyOpenSSL==0.12
pycrypto==2.4.1
pycurl==7.19.0
pyserial==2.5
python-apt==0.8.3ubuntu7.1
python-dateutil==2.1
python-debian==0.1.21ubuntu1
simplejson==2.3.2
six==1.4.1
supervisor==3.0a8
ufw==0.31.1-1
unattended-upgrades==0.1
virtualenv==1.10.1
wadllib==1.3.0
wsgiref==0.1.2
zope.interface==3.6.1
...and this is my ./manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Any thoughts on how I can fix this error? I tried to change #!/usr/bin/env python to #!/var/www/paletto-env/bin python, trying to direct it to my actual python path, but it did not lend to any apparent change, so I changed it back.
Thanks for the help.
Looks like you're environment can't find Django, even though it's clearly in your pip freeze.
Try opening a Python (not Django) shell from your virtual environment and entering:
import django
django.VERSION
If you run into the same error, there's probably an issue with your Django install. Your virtual environment probably can't find it. You could try modifying the path settings, or just reinstall Django.
If you can successfully import Django, or if you tried reinstalling and it doesn't work, you may have a permission problem. Ensure that the user responsible for running the server has access to wherever your python libraries are stored from the virtualenv.
I had pretty much the exact same problem as you and this is how I solved it (Disclaimer: I'm not sure if this is the absolute correct way but it worked for me and everything seems correct).
Short Answer:
Try pip install (package) instead of sudo pip install (package)
Long Answer:
I gave the Django complete install doc (See here) a quick read through and came across a bit that basically said you dont need super user privileges when using pip in the virtualenv. I just assumed that either would be fine but I now realize that's probably not the case.
I suspect that because I entered sudo pip install django it installed it somewhere above my local bin in my virtualenv for the project. I suspect this because when I enter python manage.py runserver I get an error; however, when I enter sudo python manage.py runserver everything functions properly.
Also, typing sudo pip freeze reveals my larger library whereas pip freeze reveals my local library for my virtualenv.
I didn't want to type sudo (do stuff) before everything and I wanted a nice, clean and proper virtualenv so I just reinstalled django but to my local virtual env with pip install django and now django is in the proper virtualenv library, pip freeze returns the proper contents, and python manage.py runserver functions properly!
django-admin.py startproject by default creates the shebang in manage.py with #!/usr/bin/env python.
If this is not the path to your python executable, or if you use python3, just edit the manage.py to reflect this.
Reinstall all python packages angain after you have activated your virtualenv.
Maybe the Django has lost some plugin with pip, so we can install Django with Tarball:
Go https://www.djangoproject.com/download/1.6.5/tarball/
Download Django-*.tar.gz
install it.
$ tar zxvf Django-1.6.5.tar.gz
$ cd Django-1.6.5/
$ python setup.py install
more ... (https://stackoverflow.com/a/24323774/686105

Django not finding apps in virtualenv when using manage.py syncdb

My problem is in getting manage.py syncdb to run within a virtualenv.
It was working fine at one point, but seems to have broken sometime around when I installed South and updated pip and distribute.
Anyways, when the virtualenv is activated, I can import apps fine within the interactive interpreter. Running through mod_wsgi, the apps are imported as well, and the site can run.
When I run manage.py syncdb, it fails to find any app in INSTALLED_APPS that is in my virtualenv. It picks up system-installed apps fine, but fails when it tries to import virtualenv only apps.
Hi This is an old question, but saw its not answered. Not sure what you are attempting to do, but there are basically two modes you can use virtualenv,
For development, to create self-contained environments
For deployment, to create self-contained environments
In the first case, you need to first Activate your virtualenv with source venv/bin/activate, for when you deploy, you need to ensure that the virtualenv is activated for your website code. Personally i prefer the following approach to ensuring your path is set correctly. (I also add this to my manage.py when doing development, so i dont have to worry about activating the environment first.
Modified manage.py
#!/usr/bin/env python
import os.path
# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
This works, due to how i structure my projects, you would have to change it to your directory structure. My projects are structured like so:
TopLevelDir
|
|- Project DIR
|- venv
|- requirements
|- deployment configs
I have a simple solution to this
Just launch manage.py from the python in the bin of your virtual environment.
So say your python is here /home/tom/environments/my_env/bin/python you could launch manage.py like so:
/home/tom/environments/my_env/bin/python manage.py syncdb
then just create a symlink to the virtual environment's python inside your django project and call it env_python then you can do this:
./env_python manage.py syncdb