Ubuntu 15.10
Python 3.4.3+
Django 1.8.7
When I try to import django in the python3 interpreter, it says ImportError: No module named 'django'. I know Django 1.8.7 installed though, 'cause I can get the version # by doing django-admin --version in the terminal commandline.
When I tried python3 manage.py runserver in a Django project directory, 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'
So again (as expected), it's not in a path where Python can find it.
I looked in /usr/local/lib/python3.4/dist-packages but it's an empty directory.
I did a whereis django & whereis Django and that simply gives me a line with django: or Django: respectively and no list of paths.
I tried Googling how to find path to Django, but that didn't turn up anything useful.
I found & checked the code of the django-admin file itself, but it doesn't have anything indicating where Django installed to. Oddly, the python file imports the django module and it works, even though the interpreter & Django project files can't see the django module. So it's on the python path but it also isn't?!? I don't know, and I can't find it.
I never had a problem like this with a previous Ubuntu (or any other OS). Has anyone got a clue how I can find where Django installed? Actually, I can't find any of the modules I installed via pip3. I've been trying to figure this out for over an hour now, and I'm very confused & frustrated.
Normally pip 3 install on python3 dist-packages
You can always use pip with:
python3 -m pip install package
to check if you are experiencing troubles with another python3 installation
ls /usr/local/lib | grep python
But the easy way to not experiment this headache is by using virtual environments
I think you have installed django outside virtual environment.
download virtual environment by
pip install virtualenv
virtualenv your_env
Activate virtual environment.
source your_env/bin/activate
Then,
Install django in your virtual environment.
Related
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
I'm new to django and I would like to follow this tutorial: https://docs.djangoproject.com/en/2.1/intro/tutorial01/
Unfortunately, django-admin is not in my path.
When I try to run the django-admin.py script directly, I have the following error:
$ /usr/local/lib/python3.7/site-packages/django/bin/django-admin.py
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/bin/django-admin.py", line 2, in <module>
from django.core import management
ImportError: No module named django.core
Here is my configuration:
System: macOS 10.13
Python: 3.7.0 (installed via Homebrew
Django: 2.1.4
What am I doing wrong?
I think You don’t install django correctly. If you are highly new in django then follow these steps to setup your clean and new project environment
Create your project folder
mkdir myProject
Open this Project Folder
cd myProject
Create virtual environment
python3 -m venv env
Now, active this virtual environment
source env/bin/activate
It’s time to install the Django
pip install django
Create your first project
django-admin startproject myProject
Open this Project
cd myProject
Open your server to check it either it’s work successfully or not
python manage.py runserver
First check if django is installed by running python -c "import django". If that is successful check if django-admin command is in your python path. If you are using a virtual environment check under virtual environment/bin/. If there is no file such as django-admin then the first solution would be to try reinstalling django. Same case if you are working outside a virtual environment.
If none of the above solves it then check if you have both python 2 and 3. If you do check whether .py files are being associated to python 2 instead of python3.
I am getting this error when importing django inside python :
ImportError: No module named 'django'
I've got a problem when installing django in my web server aws, I made an alias python = python3
so when I am working with python it is python 3.4 which is executing.
I made a virtual env with it, but when I install django even inside the virtual env, it is installing in the python 2.7 version, not in my virtualenv, and not with the python3 version.
does anyone knows how to make really my python3 as default or how I can install django in the correct folder (python3)
When you create a virtualenv, you need to show that you want to use python3. Do the following:
$ which python3
/usr/local/bin/python3
$ mkvirtualenv name --python=/usr/local/bin/python3
then try to install django:
$ pip install django
Hope it helps
when running django-admin startproject myproject on macOS I get the error
Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 2, in
from django.core import management
ImportError: No module named django.core
I checked out this question but running import django won't produce any output in a python3 shell.
/usr/local/bin/django-admin is a symlink to /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/bin/django-admin.py.
I already put /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django in my PYTHONPATH as suggested in other questions.
Am I missing something?
Even if you make it work, it is not good practice to do what you're doing! Ideally, the only python-related binaries you would want in /usr/local/bin/ would be python, pip and virtualenv (or venv, pyvenv)...
I would suggest you to delete /Library/Frameworks/Python.framework/Versions/3.6 ONLY IF you installed it there. As far as I know, macOS only comes with python2.7 installed and not python3.6!
Then open a new shell and try this:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install python3
pip3 install virtualenv
cd ~/Desktop/
mkdir proj
cd proj
virtualenv -p python3 env
source env/bin/activate
pip install django
django-admin.py startproject testproj
skip the first step if you already have brew installed
Check your permissions in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ with an ls -la command. If you see anything owned by root, this probably needs to change. I suspect that since you installed some packages as root, the permissions are weird and it can't find the module. If this is the case, reinstall the package(s) as your own user using sudo.
Another thing you should check: as phd mentioned you need to make sure you're using the version of python you think you are. Check this by running which python to tell you the location of the one you're referencing, and python --version to tell you which version you're using. If it's not Python 3.6, then you installed Django for a different version of Python. In this case, simply install Django for version 3.6 and you'll be on your way.
For future reference, Python offers a module called venv to prevent version mishaps like this. More info can be found here.
It seems like you are trying to create or work on a django project without using a python virtual environment. I recommend reviewing the python 3 venv documenation (https://docs.python.org/3/library/venv.html). Then creating a virtual environment (venv) specifically for your web application. Once you you that project's venv setup you can install django into that venv.
On MacOS, use sudo before the command:
sudo django-admin startproject myproject
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