No module named django but it is installed - python

I have two versions of python 2.7 and 3.4 and installed django through pip. it shows in ubuntu terminal:
$ pip freeze
Django==1.6.11
$ pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
$ python
Python 2.7.9 (default, Feb 3 2016, 02:50:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>> import sys
>>> sys.path
['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']
>>>
Any idea??

Probably, pip installs packages into dist-packages directory, which is not included into PYTHONPATH environment variable. You have a couple of solutions:
Create and configure virtualenv for your project, before using pip. This is the most Pythonic way
Try to install Django using built-in pip module:
python -m pip install django
This command should install packages into site-packages directory.
You may also add dist-packages to your PYTHONPATH. This question should help you: How to globally modify the default PYTHONPATH (sys.path)?

This error shows that Django is not installed. Installing Django should solve the problem.
In my case, Django was there in my virtualenv but while using gunicorn I was getting this error then later I realized gunicorn was dealing with my globally install python environment not my virtual environment installing Django on my global python env simply solved my issue.
pip install django

I got this error when using
python manage.py runserver #python version 3 was being used
Solved the problem by using:
python2 manage.py runserver #python version 2

I had the similar error for other modules which are already installed, executing same command as superuser:
sudo pip3 install -r requirements.txt
solved my issue.

You need to close the other active virtual environment in your machine if you're using virtualenv, and make sure django installed. Go to python3 interpreter and run this:
>>>from django import get_version
>>>get_version()
make sure it show you this '2.1.4'

For Mac users; If you've previously downloaded and installed python3, just running python via the terminal shell defaults to using the pre-installed python v2, which will not recognise your installation of django (unless you install it via the python2 module) and you'll probably get an error when you check the version:
$ python
>>> from django import get_version
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
Try starting python using:
$ python3
Then try:
>>> from django import get_version
>>> get_version()
You should get the output:
'2.0.3'

I had given the dependency in requirements.txt as Django==2.0.7. So after activating the virtual environment, if you are receiving the message, try installing the requirements:
pip install -r requirements.txt

I also faced the same problem. i was using python 3.7 and installed django 2.2. So i degraded my python to 3.6 and installed django 2.2, and without having a virtualenv.

I got the same issue today. doing pip3 install django or pip3 install -r requirements.txt solved it.

python3 manage.py migrate solved my problem
I was getting this error while running:
python manage.py migrate
I changed to python3 ...

Try updating the Django.
I was getting the same issue because I had an older version of Django installed. I installed the latest version of Django instead and it fixed my issue.

I faced same issue with you when try to setup Apache work with Django.
Issue solve after add Pythonpath to Apache2.conf as bellow:
WSGIPythonPath/opt/djangoprojects/myproject:opt/anaconda3/lib/python3.6/site-packages
I installed Python3 before.

This error shows you didn't install Django. Installing Django should solve the problem.
Once you do, you can just check the path of "django" using:
>>> sys.path

For MacOs,I was getting the same issue while using import django.I deactivated conda and again activated django virtual environment by conda.Then made sure the python version is correct,in my case it was python 3.8.5.Then navigate to the project's directory and run the required python script.
My Django version is 3.1.5

Unistall/Install
Confirm that you're running from virtualenv of you have installed the app to venv.
If you made sure that it has been installed and still shows mdule not found, uninstall and install again.

Maybe your env not running after you shut down your computer. Just need to pipenv shell to launch again the virtual environment.
Then u can see this one the terminal
Launching subshell in virtual environment...
. /Users/.../.local/share/virtualenvs/little-lemon-O3-M0emD/bin/activate
and, type python3 manage.py runserver to start your django server and work normally.

Related

Pip is not found after creating new python virtual environment

I'm trying to create new Python virtual environment, but I have problems with pip.
I want my virtual environment to run on Python 3.10, but my default system Python version is 3.9.5. So I downloaded Python 3.10 with
sudo apt-get install python3.10
and then i run
python3 -m virtualenv venv --python=python3.10
to create virtual environment.
The problem is, when I try to use pip I get an error:
Traceback (most recent call last):
File "/home/<my user name>/venv/bin/pip", line 5, in <module>
from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'
I tried running
python -m ensurepip --upgrade
but got an error:
/home/pablo/venv/bin/python: No module named ensurepip
Also running whereis pip shows, that there are some pips in the virtual environment.
pip: /usr/bin/pip /home/<my user name>/venv/bin/pip3.10 /home/<my user name>/venv/bin/pip /usr/share/man/man1/pip.1.gz
Also, when I create virtual environments with my default python interpreter, everything works perfectly fine.
I would recommend using pyenv with pyenv-virtualenv. In my experience it's just too much work to go manual. Sorry I know this is not a true answer, but having wasted hours and hours on similar issues, I feel like sharing )
your installed binary executable is named as pip3.10(/usr/bin/pip /home//venv/bin/pip3.10). So you should use the same name to call it, just pip wouldn't work. Have you tried that?

While setting interpretor to python 3.8.2 in django its giving errors such as unable to import django.urls?

I am working on Django in VS code. It's giving errors on any Django import (ex unable to import Django.urls) while setting the interpreter to python 3.8. Although when I set the interpreter to python 2.7 the errors are resolved.
I have installed Django in a virtual environment. Please enlighten me why this problem is occurring and will it cause any trouble in the future?
The reason is that the module "Django" was not successfully installed in your python3.8 environment, but it was installed in the python2.7 environment.
Since you have python2 and python3, it is recommended that you use pip3 to specify the python version when installing modules using pip.
1.Solution: You could try to install the module "django" using "pip3 install django" in the VSCode terminal. Please note that the pip used is from python3.8. (Check the pip version: "pip3 --version")
2.pip version: If the pip version is not from python3.8, you can use the shortcut key Ctrl+Shift+` to open a new VSCode terminal after selecting the python3.8 interpreter, and it will automatically enter the current environment and automatically use pip from the current environment python3.8.
3.Check: Use "pip3 list" to check whether the module "Django" is already installed in the current environment:

Why am I getting ImportError: No module named pip ' right after installing pip?

I have installed pip and ez setup. I also checked the system path and I can see the module in the folder structure. Still when i try to run pip command, I get an Import error saying no module named pip. I am running 32bit python on a windows7 machine
Just be sure that you have include python to windows PATH variable, then run python -m ensurepip
After running get_pip.py with python embed you have to modify your pythonXX._pth file. Add Lib\site-packages, to get something like this:
pythonXX.zip
.
Lib\site-packages
# Uncomment to run site.main() automatically
#import site
If you don't you will get this error:
ModuleNotFoundError: No module named 'pip'
or
python-3.8.2-embed-amd64\python.exe: No module named pip
λ pip
Traceback (most recent call last):
File "runpy.py", line 193, in _run_module_as_main
File "runpy.py", line 86, in _run_code
File "python-3.8.2-embed-amd64\Scripts\pip.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'pip'
λ python -m pip
python-3.8.2-embed-amd64\python.exe: No module named pip
This issue occurs with me while I was trying to upgrade pip version.
It was resolved with the following commands:
python -m ensurepip
The above command restores the pip and below mentioned upgrades it.
python -m pip install --upgrade pip
What solved the issue on my case was go to:
cd C:\Program Files\Python37\Scripts
And run below command:
easy_install.exe pip
Follow steps given in https://michlstechblog.info/blog/python-install-python-with-pip-on-windows-by-the-embeddable-zip-file/. Replace x with version number of Python.
Open the pythonxx.__pth file, located in your python folder.
Edit the contents (e.g. D:\Pythonx.x.x to the following):
D:\Pythonx.x.x
D:\Pythonx.x.x\DLLs
D:\Pythonx.x.x\lib
D:\Pythonx.x.x\lib\plat-win
D:\Pythonx.x.x\lib\site-packages
try to type pip3 instead pip.
also for upgrading pip dont use pip3 in the command
python -m pip install -U pip
maybe it helps
turned out i had 2 versions of python on my laptop
both commands worked for me
python -m ensurepip
py -m ensurepip
both with another installation path
c:\tools\python\lib\site-packages
c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages
only the first path was in my %PATH% variable
First make sure that python is added in environment variable.
Try checking the version of pip or pip3. Use these commands to check.
For pip:
pip --version
For pip3:
pip3 --version
If you can see any version of pip and still not able to use it, then run the following command.
python -m ensurepip
This ensures the pip in your system.
Running these 2 commands helped me:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
If you wrote
pip install --upgrade pip
and you got
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.2.1
Uninstalling pip-20.2.1:
ERROR: Could not install packages due to an EnvironmentError...
then you have uninstalled pip instead install pip.
This could be the reason of your problem.
The Gorodeckij Dimitrij's answer works for me.
python -m ensurepip
I found this post while looking for a solution for the same problem. I was using an embedded python distribution. In this case, the solution is to uncomment import site in the file python<version>._pth.
The ensurepip module was added in version 3.4 and then backported to 2.7.9.
So make sure your Python version is at least 2.7.9 if using Python 2, and at least 3.4 if using Python 3.
I'v solved this error by setting the correct path variables
C:\Users\name\AppData\Local\Programs\Python\Python37\Scripts
C:\Users\name\AppData\Local\Programs\Python\Python37\Lib\site-packages
I was facing same issue and resolved using following steps
1) Go to your paython package and rename "python37._pth" to python37._pth.save
2) curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
3) then run python get-pip.py
4) pip install django
Hope this help
I've solved this error downloading the executable file for python 3.7.
I've had downloaded the embeddeable version and got that error.
Now it works! :D
The method I'm going to tell might not be the correct way to do it. But this method solved my issue. I tried every solution on youtube and StackOverflow methods.
If you have two python versions installed. Delete one. I have the python 3.8.1 and 3.9.0 versions installed. I deleted version 3.9.0 from the C directory.
Now go to the control panel > System and security > System > Advanced system settings.
Click on 'environment variables'.
Select the path and click on 'edit'
Now, add the path of the python and also the path of pip module. In my case it was c:\python38 and c:\python38\scripts
This method solved my issue.
Instead of Python zip install python by python installer. It fixed the issue for me.

No modules named core.management

First of all I know that there are a lot of posts like this in all Internet, I tried everything and it didn't work.
Also I have some theoretical questions about it.
I run my installation of Django in a Virtualenv so I type:
sudo apt-get install python-setuptools
virtualenv --no-site-packages djangito
source djangito/bin/activate
(djangito)cd djangito/
(djangito)easy_install Django // V.1.6
(djangito)django-admin.py startproject django_test
But when I try to run my runserver happen this:
(djangito)root#marcproves:~/djangito/django_test# python manage.py runserver
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
BUT
(djangito)root#marcproves:~/djangito/django_test# sudo python manage.py runserver
Validating models...
0 errors found
February 20, 2014 - 09:25:14
Django version 1.6, using settings 'django_test.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
It runs with sudo but....Is runing python from VirtualEnv with sudo python manage.py runserver ?
I know that virtualenv is a kind of stuff for work with independent packages but I don't know if I'm missing something important.
I need to understand why happen this
Thanks at all
Try to use pip instead of easy install. Every virtualenv has it installed by default (along with setuptools, which contain easy_install) and it is the recommended way of installing packages inside virtualenvs.
racech#CONFIG0001:~$ sudo apt-get install python-virtualenv
[...]
racech#CONFIG0001:~$ virtualenv test
New python executable in test/bin/python
Installing distribute.....done.
Installing pip...............done.
racech#CONFIG0001:~$ source test/bin/activate
(test)racech#CONFIG0001:~$ pip install django
[...]
Successfully installed django
Cleaning up...
(test)racech#CONFIG0001:~$ django-admin.py startproject testproject
(test)racech#CONFIG0001:~$ cd testproject/
(test)racech#CONFIG0001:~/testproject$ python manage.py runserver
Validating models...
0 errors found
February 20, 2014 - 10:12:51
Django version 1.6.2, using settings 'testproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
And as for the explanation it looks like easy_install installed Django not into your virtualenv, but to your system Python. You can check that by creating a new virtualenv with a different version of Python. Make a Virtualenv with undefault Python binary (3.x if you use 2.x and 2.x if you use 3.x as default) by running
virtualenv --python=/usr/bin/python3.2 myvirtualenv
and then check it by
python --version
sudo python --version
they should be different. Virtualenv replaces default Python environment only for the user which activated it. Therefore easy_install must have used system Python instead of virtualenv. Maybe you did run it with sudo as well?

Django - "no module named django.core.management"

I get the following error when trying to run Django from the command line.
File manage.py, line 8, in <module>
     from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Any ideas on how to solve this?
It sounds like you do not have django installed. You should check the directory produced by this command:
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
To see if you have the django packages in there.
If there's no django folder inside of site-packages, then you do not have django installed (at least for that version of python).
It is possible you have more than one version of python installed and django is inside of another version. You can find out all the versions of python if you type python and then press TAB. Here are all the different python's I have.
$python
python python2-config python2.6 python2.7-config pythonw2.5
python-config python2.5 python2.6-config pythonw pythonw2.6
python2 python2.5-config python2.7 pythonw2 pythonw2.7
You can do the above command for each version of python and look inside the site-packages directory of each to see if any of them have django installed. For example:
python2.5 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python2.6 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
If you happen to find django inside of say python2.6, try your original command with
python2.6 manage.py ...
sudo pip install django --upgrade
did the trick for me.
I got the same error and I fixed it in this manner:
I had to activate my virtual environment using the following command
source python2.7/bin/activate
Most probably in your manage.py the first line starts with !/usr/bin/python which means you are using the system global python rather than the one in your virtual environment.
so replace
/usr/bin/python
with
~/projectpath/venv/bin/python
and you should be good.
well, I faced the same error today after installing virtualenv and django. For me it was that I had used sudo (sudo pip install django) for installing django, and I was trying to run the manage.py runserver without sudo. I just added sudo and it worked. :)
Are you using a Virtual Environment with Virtual Wrapper? Are you on a Mac?
If so try this:
Enter the following into your command line to start up the virtual environment and then work on it
1.)
source virtualenvwrapper.sh
or
source /usr/local/bin/virtualenvwrapper.sh
2.)
workon [environment name]
Note (from a newbie) - do not put brackets around your environment name
I am having the same problem while running the command-
python manage.py startapp < app_name >
but problem with me is that i was running that command out of virtual environment.So just activate your virtual environment first and run the command again -
I experience the same thing and this is what I do.
First my installation of
pip install -r requirements.txt
is not on my active environment. So I did is activate my environment then run again the
pip install -r requirements.txt
The problem occurs when django isn't installed on your computer. You also get this error because the django.core.management module is missing.
To solve this issue we have to install django using pip. Open a command line prompt -> cmd(on windows) and enter the following command:
pip install django
This command will install django on your computer. So consider installing pip first. Here's how to install pip on a Windows machine
Okay so it goes like this:
You have created a virtual environment and django module belongs to that environment only.Since virtualenv isolates itself from everything else,hence you are seeing this.
go through this for further assistance:
http://www.swegler.com/becky/blog/2011/08/27/python-django-mysql-on-windows-7-part-i-getting-started/
1.You can switch to the directory where your virtual environment is stored and then run the django module.
2.Alternatively you can install django globally to your python->site-packages by either running pip or easy_install
Command using pip: pip install django
then do this:
import django print (django.get_version()) (depending on which version of python you use.This for python 3+ series)
and then you can run this: python manage.py runserver and check on your web browser by typing :localhost:8000 and you should see django powered page.
Hope this helps.
In case this is helpful to others... I had this issue because my virtualenv defaulted to python2.7 and I was calling Django using Python3 while using Ubuntu.
to check which python my virtualenv was using:
$ which python3
>> /usr/bin/python3
created new virtualenv with python3 specified (using virtualenv wrapper https://virtualenvwrapper.readthedocs.org/en/latest/):
$ mkvirtualenv --python=/usr/bin/python3 ENV_NAME
the python path should now point to the virtualenv python:
$ which python3
>> /home/user/.virtualenvs/ENV_NAME/bin/python3
This also happens if you change the directory structure of your python project (I did this, and then puzzled over the change in behavior). If you do so, you'll need to change a line in your /bin/activate file. So, say your project was at
/User/me/CodeProjects/coolApp/
and your activate file is at
/User/me/CodeProjects/coolApp/venv/bin/activate
when you set up your project, then you changed your project to
/User/me/CodeProjects/v1-coolApp/
or something. You would then need to open
/User/me/CodeProjects/v1-coolApp/venv/bin/activate
find the line where it says
VIRTUAL_ENV="/User/me/CodeProjects/coolApp"
export VIRTUAL_ENV
and change it to
VIRTUAL_ENV="/User/me/CodeProjects/v1-coolApp"
before reactivating
In my case, I am using Ubuntu. The problem can be that I don't have the permission to write to that folder as a normal user. You can simply add the sudo before your command and it should work perfectly. In my case sudo python manage.py syncdb.
I had the same issue and the reason I was getting this message was because I was doing "manage.py runserver" whereas doing "python manage.py runserver" fixed it.
I had the same problem and following worked good, you should navigate main folder in your project than type:
source bin/activate
My case I used pyCharm 5 on mac. I also had this problem and after running this command my problem was solved
sudo pip install django --upgrade
had the same problem.run command 'python manage.py migrate' as root. works fine with root access (sudo python manage.py migrate )
You can try it like so : python3 manage.py migrate (make sur to be in the src/ directory)
You can also try with pip install -r requirements.txt (make sur you see the requirements.txt file when you type ls after the migrate
If after all it still won't work try pip install django
Hope it helps
I got the same problem trying to use the python manage.py runserver. In my case I just use sudo su. Use the terminal as a root and try it again an it works partially. So I use python manage.py migrate comand and it fix it.
You must choose your Project first before running the server , type this
workon your_project_name
then
python manage.py runserver
It is because of virtual enviornment configuration. You need to work on your virtual enviornmnet of Python. You should try on your command promt with,
workon virtual_enviornment_name
File and Directory ownership conflict will cause issues here. Make sure the ownership of the directories and files under the project are to the current user. (You can change them using the chown command with the -R option.) Try rerunning the command: this solved the problem for me when running through the "First Django App" sample:
python manage.py startapp polls

Categories

Resources