Should django-admin version be same as Django? - python

I tried to configure my PC for Django course , i have fresh Ubuntu 18.04 :
I followed all steps from the course :
I had newest python pre-installed, Python 3.6.5 so i went :
sudo apt install python3-pip
went ok, then :
pip3 install django==2.0.2 (version suggested by instructor)
that completed as well, then :
django-admin startproject wordcount
and that gave me error :
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
So i tried to install this but it didn't help. so i checked web for answers and i found that i should do :
sudo apt-get install python-django
And after that i could use
django-admin startproject wordcount
and it created a project for me but i saw that some elements of default files are different from what i saw on course video so i check and :
django-admin --version
1.11.11
also django-admin --help
gives me message like this :
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.).
Any ideas what went wrong and how I can fix it?

The problem is that the pip3-installed packages aren't on the PATH for some reason. There's a few ways to fix this.
Add the pip3 installation location to your $PATH.
Force it to use the version of Django you installed via pip3:
$ python3 -m django.bin.django-admin startproject wordcount
Use virtualenv or pipenv to create a "virtual" Python installation - complete with it's own Python version and set of libraries.

I see you struggle with this problem. I would recommend you to move to such thing as a virtualenv (or use anaconda for this task). This should solve a numerous other problems you've not ran across yet and this one especially.
Virtual environment allows you to create separate environment, with it's own packet set. Some IDEs (for example, pycharm) provide GUI for it's creation and management.
Update 1:
Thanks to #DhavalSavalia, of course there is also pipenv package, that is a more friendly virtualenv wrapper (unless you have some gui for virtualenv).
Update 2:
Also, I've noticed that you setting up django using apt. You probably shouldn't do that, because apt often contains old version of python packages. Use pip for as many packages as you can.

Related

zsh: command not found: django-admin when starting a django project

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.

ImportError when using virtualenv in Django

My Question is pretty Simple.
Whenever I use virtualenv environment in my Django project it gives the following error when executed this command
python manage.py runserver
File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
But when i use root and give the same command it seems to work perfectly. Why does this Happen? I am in the virtualenv so it means i am the root person by default.How can i fix this error?
I had seen many forums but couldn't find related to mine.
This happens because you have installed django in the system-wide Python interpreter; possibly by doing something like sudo pip install django.
Once you create a virtual environment and activate it - it contains no packages. The concept of a virtual environment is that it allows you to install Python packages without affecting the global Python installation.
So once you activate a virtual environment, you have to install packages in that virtual environment; so you should pip install django (note: without sudo) once you activate the virtual environment; like this:
$ virtualenv sample_env
...
$ source sample_env/bin/activate
(sample_env) $ pip install django
A virtual environment is a virtual environment for Python; it does not control what the user is that is logged into the system.
Finally, as a general rule - you should not be using root for development purposes as doing so can easily compromise your system.
Yup i have same error while running Django. I also have enabled virtual environment. But I was still getting error.
Solution to this problem is using this command to install any python package
python -m pip install django
This will definitely solve your problem. As it solved mine.

Django-admin creates wrong django version inside virtualenv

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

django-admin/.py, command not found

I tried both admin-django, and admin-django.py, but it just tells me that it can't find the command. I use Arch Linux and installed python-django with pacman. I also tried to set $PATH to /usr/local/bin, how someone on SO suggested, but it didn't help. Would anyone here know what I could have done wrong?
(python -c "import django" works)
Installing Python packages via your OS package manager is really hit or miss. It's likely that their setup script wasn't configured correctly (or, is an old version that doesn't match the documented behavior). You should follow the installation instructions and use pip instead -- it should be safe to download pip from your package manager, and then use it for all of your Python packages after that.
Also, their documentation mentions that the command is django-admin.py, not admin-django.py, but, since your title is correct, I assume that was a typo.
in effet you should use the command django-admin in this way
django-admin startproject _the_name_of_your_project
if tell you " command not found "
Maybe you should consider to reinstall django :
sudo pip uninstall
Best Practice : Use venv
sudo pip install virtualenv
Go in your working directory :
source env/bin/ activate
Now you are in a virtual enviroment :
sudo pip install django
Try the command django-admin

Command not found: django-admin.py

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>

Categories

Resources