Related
I use Ubuntu 15.10 and zsh (don't know if it can help)
So I try to install django:
pip install django
Downloading/unpacking django
Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB): 6.6MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
Everything works fine. When I do pip freeze I can see django is installed.
then:
django-admin startproject mysite
But I got this issue:
zsh: command not found: django-admin
I found an alternative solution.
With find / -name django-admin I found django-admin in myHome/.local/bin/django-admin.
So instead of django-admin startproject mysite I use the full path myHome/.local/bin/django-admin startproject mysite
thanks to #Evert, this is why I got the problem.
his comment:
This is likely because you either used the --user option to pip
install, or you set up pip in such a way that it automatically does
that. Hence, everything gets installed in $HOME/.local. You may want
to add $HOME/.local/bin to your $PATH for the future.
When I had the problem on my mac I just uninstall django and install it again but with root permissions. Now it's working good)
pip3 uninstall django
sudo pip3 install django
I faced a similar issue on Mac OS but I moved in another way. I used Virtual Environments.
First, create the virtual environment
python3 -m venv django-env
Then, use this environment
source django-env/bin/activate
Next, install django
python -m pip install django
Finally test django is working
django-admin startproject mysite
In my opinion, it is better to have environments isolated to avoid O.S. settings
I was facing the same problem after updating my Mac OS to Catalina, and shell from bash to zsh.
None of the commands would work, as if all my paths were deleted.
Looking at Brady Huang's answer, doing something similar worked for me.
I made sure django was installed correctly by reinstalling it
pip3 uninstall django
pip3 install django
I found django installed under:
/Library/Frameworks/Python.framework/Versions/3.8/bin/django-admin
by running:
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.8/bin/django-admin usr/local/bin
I was able to run django-admin again.
My adjango-admin is located in
~/Library/Python/3.7/bin/django-admin
But I don't have it in my linked PATH which is looks like this
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
So I create a symlink to one of the bin I have in my PATH
sudo ln -s ~/Library/Python/3.7/bin/django-admin /usr/local/bin
It solves my issue
You may be having some dependency issues.
So to avoid it happen again you need to create a virtual environment only for the current project you are doing. Like this you can avoid having issues and isolate your application.
You can follow this question to create a virtual environment and add django in it.
Then, as soon as you create the virtual environment and activated you can pip install django and check to see whether it is installed.
Tried many stuffs but then installing the python version 3.11 and using a virtual env solved the issue. Earlier I was using 3.8.9. Hope this helps someone
what worked for me!
I just Added the following in my ~/.zshrc file( you can also add it in the ~/.bashrc file if you are not using zsh terminal)
export PATH=$PATH:$HOME/.local/bin
This solution will work not only for you django-admin command but all other python package commands that would behave in a similar way.
I've created a n new directory, a virtualenv and installed a django-toolbelt inside it. The django-version should be 1.8 but when I call 'django-admin.py version' it says 1.6. So when I start a new project it creates a 1.6. I thought virtualenv was supposed to prevent this. What am I doing wrong?
Edit: I think it has to do with the PATH (?). Like it's calling the wrong django-admin version. I'm on Windows 7. Still don't know how to fix it.
I came across this problem too. In the official document, I found that, in a virtual environment, if you use the command 'django-admin', it would search from PATH usually in '/usr/local/bin'(Linux) to find 'django-admin.py' which is a symlink to another version of django. This is the reason of what happened finally.
So there are two methods to solve this problem:
re-symlink your current version django-admin(site-packages/django/bin/django-admin.py) to 'usr/local/bin/django-admin' or 'usr/local/bin/django-admin.py'
REMIND: This is a kind of global way so that it will effects your other django projects, so I recommend the second method
cd to your_virtual_env/lib/python3.x/site-packages/django/bin/(of course you should activate your virtual environment), and then use 'python django-admin.py startproject project_name project_full_path' to create django project
Try and install Django into the virtual environment as well:
pip install django
It should install the latest version, you can also specify a particular version (let's say 1.8.2) if you need to:
pip install django==1.8.2
Either way you'll have the correct version of Django in your virtual environment and it should work as you expect then.
You can also use the following command to see what version you have installed:
pip show django
Update:
It seems that you have the correct version installed in your virtual environment, but for some reason your Windows 7 use the system Django installation instead while you use manage.py or django-admin.py directly. However, you can use python manage.py or python django-admin.py instead, which seems to work as expected (and use the virtualenv Django installation).
I had the same problem. Could be related to your zsh/bash settings.
I realized that using zsh (my default) I would get django-admin version 1.11 despite the Django version was 2.1! When I tried the same thing with bash I would get django-admin version 2.1 (the correct version). Certainly a misconfiguration.
So, I strongly suggest you check your zsh or bash settings to check for paths you might have.
Create a virtual environment for a project:
$ mkdir cd my_project_folder
$ cd my_project_folder
$ virtualenv venv
$ source venv/bin/activate
And now install django
(venv) ~$ pip install django==1.8
manage.py runserver in virtualenv using wrong django version
pip / virtualenv / django installation issue
So i have been playing around with python (2.7.x) and django framework on my ubuntu (12.04). The way i setup django based project is by using virtualenv
Which i did it like this
** FROM TERMINAL **
1. sudo apt-get install python-setuptools
2. sudo easy_install virtualenv
Then i continue setting up my directory for my django project by using this command:
virtualenv --no-site-packages project-name
Soon after that, i activated my virtualenv:
source project-name/bin/activate
Then i continue with the Django framework installation using this:
sudo easy_install Django
and verify that Django framework is installed within my virtualenv by checking there is a file called django-admin.py under project-name/bin/ directory (which is exists).
However, the second time i tried to create another django-project (completely different one) following the same exact step as above, I don't have django-admin.py installed in the correct directory. It's get installed to /usr/lib/python...
And when i tried to run the app i get this message:
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
Does anyone know why is this happening?
Thanks.
* EDITED PART *
responding to sachitad's answer
i get the following message when i executed his/her suggestion
(project-name)blah#blah:~/Documents/python/project-name$ easy_install django
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: '/home/blah/Documents/python/project-name/lib/python2.7/site-packages/test-easy-install-3775.write-test'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/home/blah/Documents/python/project-name/lib/python2.7/site-packages/
that's why, i initially thought i need to use the sudo command.
Problem:
sudo easy_install Django
Even if you have activated the virtualenv, while installing packages inside virtualenv, never ever use sudo. If you use sudo, it assumes you are installing on the system path(/usr/lib/local/..).
Thus,
easy_install django
OR
pip install django
should work.
As an addition to sachitad answer, I suggest you have a look at virtualenvwrapper which allow you to manage easily you virutal env with commands like :
mkvirtualenv your_project_name // create a virtual environment
workon your_project_name // select this virtual environment
pip install django // will install in this virtualenv
./home/user/path_to_virtual_env_project_/bin/pip install Django
you call pip which not in virtual env.
It seems activate not work.
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
I am a complete beginner to Python/Django, but I want to dive right in and start experimenting. Thus I was following this guide on installing Python/Django http://devcenter.heroku.com/articles/django.
Everything is working fine until the step
django-admin.py startproject hellodjango
Where I get
command not found: django-admin.py
Now I have tried a few things, but none have really worked out. Is there someone kind enough to point me in the right direction?
P.S. Is there a great guide out there on running Python/Django locally on a Mac to run and test apps?
I'm on Mac OS X Lion, Python 2.7.
When that didn't work for me, I tried python -m django startproject mysite and it worked.
Actually, if you use Ubuntu, it's just django-admin not django-admin.py.
Resides in /usr/bin
Probably the same thing on Mac.
You're using a Windows tutorial.
It may also tell you
python manage.py runserver
and that is actually
python ./manage.py runserver
I tried as the method, it worked.
pip uninstall django
sudo pip install django
django-admin startproject example
It worked well.
Make sure you properly did the source bin/activate command. If you skip that, or do it in a different terminal window, or close the window then re-open it, you won't be in the virtualenv and you won't have access to the django-admin.py command in your environment.
If you´re on Windows, here´s what worked for me (using pylauncher):
$ py -m django startproject myproject
To solve this problem, you need:
Find the main folder of Django, and find the django-admin.py file
Typically, the file is in <YOUR_DJANGO_FOLDER>/bin/django-admin.py
Create a link for this file
ln -s /bin/django-admin.py /usr/local/bin/django-admin
Type django-admin in your command to check if it works
As Timmy said above, it could just be that django-admin.py is not on your system path.
See here - https://code.djangoproject.com/wiki/InstallationPitfalls - for 3 possible causes with solutions.
To Windows users
using django-admin instead of django-admin.py worked for me in Windows.
For me this one worked
python3 -m django startproject mysite
Then it doesn't tell you that it created file you must check by yourself in your home
hardrive/user/urhome
python3 -m django startproject mango
I have used follwong command to install (/usr/local/bin) MAC OS
pip install django
django-admin startproject mysite
The following guide in official site https://code.djangoproject.com/wiki/InstallationPitfalls
First, you should find location of django-admin.py by
which django-admin.py
Example: in my case
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/bin/django-admin.py
You use sudo ln -s to relocate django-admin-py to /usr/local/bin
sudo ln -s /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
after that change permission of the django-admin to be executable
sudo chmod +x /usr/local/bin/django-admin.py
Now you can use
django-admin.py startproject mysite
to create your django project
If you want to change django-admin.py to django-admin to look like more compact you can use
sudo mv /usr/local/bin/django-admin.py /usr/local/bin/django-admin
Hope this help for you !
This simply worked for me.
Install django on your virtualenv:
pip install django
And then run:
django-admin startproject myprojectname
I was just having the same problem, I just did an upgrade and that's it, it worked, I hope this is useful for future problems in Linux:
pip install --upgrade django
There may be a chance that your path is not correct. Ubuntu has a .local/bin folder which pip uses to install module binaries and you need it in your path to use django or any shell commands installed using pip.
Open ~/.bashrc or ~/.zshrc
Add the following line and save it
export PATH="/home/animesh/.local/bin:$PATH"
restart the shell with source ~/.zshrc
To windows users out there: I have faced this problem several times and here are the checkpoints:
When there is problem initiating a new project, make sure:
1) python is working in the command line (type in python and see if you get the console)
2) specify the full path of django-admin.py in the command
3) check django-admin.py is in the system path
4) cd the command line path to where you want the new project
Screenshot of what finally worked for me (only the last command):
https://flic.kr/p/r9LJ67
(stackoverflow doesn't allow me to post pictures yet)
If you come across command not found: django-admin.py problem which means you don't installed django frame work. You should install the framework using pip.
pip install django
After that look at the directory if the related script exist or not.
Look into C:\Python27\Scriptsfolder to check for django-admin.py exist or not.
if you install django by pip
ensure you have installed django:
pip list or pip freeze
if there is django
then
get location of django:
pip show django
if location is '/Users/xxxxx/Library/Python/2.7/lib/python/site-packages' then
relocate django-admin-py to /usr/local/bin:
ln -s /Users/xxxxx/Library/Python/2.7/lib/python/site-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
Get site-package path with Python:
import site; site.getsitepackages()
And run django-admin.py directly:
python (your-site-package-addresss)/django/bin/django-admin.py startproject hellodjango
On Mac: If this works you can go and add django-admin.py to your path using symlink:
sudo ln -s (your-site-package-addresss)/django/bin/django-admin.py /usr/local/bin/django-admin
(could be that you have to reopen terminal or reinstall django to get the symlink working)
I changed my complete python path to desktop and tried and it's working well for me
In my case i simply forgot to run pip install django
On RHEL stock python config:
django-admin.py startproject mysite
I'm running macOS and I'm using pyenv instead of virtualenv. I'm not sure if they behave similarly, but I was having the same problem in which django-admin.py was not found.
After a while I've noticed that I had a warning after installing django:
pyenv: cannot rehash: /Users/msvolenski/.pyenv/shims/.pyenv-shim exists
Once I deleted this file and ran pyenv rehash it all started working perfectly.
Hope this helps!
For Windows Users first search for django-admin, right click on the file that has been found and open file location and keep it open.
Using Windows Powershell, cd into the the folder where you want to create your django project
when your in the right folder write the full path of where django-admin is located in my case I am using Anaconda 3 so the file location is
C:\Users\Sen\Anaconda3\Scripts
so in Windows PowerShell type C:\Users\Sen\Anaconda3\Scripts\django-admin.py startproject [name of project]
hope this helps!
I had the same issue when migrating to AWS Beanstalk it was installed and everything but i noticed the alias was not working but when i called the entire thing path and all it worked so i just rebooted the boxes and it worked i think the alias list is not updated automatically after you install.
It has to do with the PATH:
Put this in the .bash_profiel and the source it (for mac users only):
(change the location with the location of your installed python libraries)
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
If someone is facing the same problem, and is on MacOs, here is what I did, and it worked for me:
If you've installed python directly from the official website, uninstall it, and install it once again using brew:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install python3
This will also install the pip3 for you, so you don't have to install it by yourself.
I was facing the same issue.
The issue resolved after I upgraded the django version using
pip install --upgrade django
Then run
django-admin startproject mysite
Hope this helps!
There was a space in one of the names in the path to my project. I set up a new virtual environment in a new directory and did all the same things and it works.
Sometimes it is the simple things...
Since you're just starting out. It is very important to adopt best practices. You will face many dependency related issues with this approach of development. In this case it is always recommended to work with a virtual environment for each python project.
This will ensure fresh installation of project-specific dependencies that do not overlap with what the system you are running on already has.
If you have not already noticed, you will come across more issues such as python3 not working with earlier versions of django. pip will skip install as it checks and finds a version of django already installed. So this cannot be stressed enough, always use a virtual environment for local setups.
to do so:
cd [your project path]
virtualenv venv
you can active your environment by :
source ./venv/bin/active
install your requirements packages with pip :
pip install -r
or pip install
you can also install your requirements modules without activate the environment
./venv/bin/pip install
to run your python script use :
python <.py file>
and if you didn't activate your env use :
./venv/bin/python <.py file>