django-admin command not working in Mac OS - python

I started Django in Mac OS and after installing Django using pip, I tried to initiated a new project using the command django-admin startproject mysite. I get the error -bash: django-admin: command not found. I make quick search in Google and haven't get any solution that works.
How to start a new project using Django using django-admin ?

I solved the issue after reading a webpage about the mentioned issue.
In the Python shell, write the following,
>> import django
>> django.__file__
>> django also works
It will provide the installation location of django.
Change the path to the new path /usr/local/bin/django-admin.py,
sudo ln -s the complete path of django-admin.py /usr/local/bin/django-admin.py
In Mac OS, The call needs to be django-admin.py startproject mysite than django-admin startproject mysite

Try the following:
django-admin.py startproject mysite

First install Django, lets assume you have python3.7 and want to install django 2.0.3, in this case using pip3.7 to install
sudo pip3.7 install django==2.0.3
now try to see if django-admin.py is installed using
ls /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/bin
If you see django-admin.py just link it to the /usr/local/bin using this command
ln -s /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/bin /usr/local/bin/
Then you could run this command to create new python project
django-admin startproject mysite

try this. this should work for you with no problem.
pip3 install django
python3 -m django startproject PROJECTNAME
you can use python and pip command without mentioning 3 either.

You need to add django to your path variables and then restart the terminal.

I know I'm jumping in a little late, but my installations seem to all reside away from /usr/local/bin/... . What worked for me was adding an export path in bash_profile for my django installation.
This also made me realize that it was installed globally. From what I've heard, it's better to install django locally within venv as you work on different projects. That way each virtual environment can contain its own versions and dependencies for django (and whatever else you're using). Big thanks to #Arefe.

pip install django
after
django-admin startproject nameProject

On mac, try to uninstall it via python -m pip uninstall Django, then you get the actual path to django-admin, then you can do chmod +x /Users/pano/.pyenv/versions/3.9.5/bin/django-admin
$ python -m pip uninstall Django
Found existing installation: Django 4.0.6
Uninstalling Django-4.0.6:
Would remove:
/Users/pano/.pyenv/versions/3.9.5/bin/django-admin
/Users/pano/.pyenv/versions/3.9.5/lib/python3.9/site-packages/Django-4.0.6.dist-info/*
Proceed (Y/n)? Y
Successfully uninstalled Django-4.0.6

In my case, i was forgot to activate the virtual environment.
pipenv shell

Related

command not found: django-admin

I have downloaded anaconda and Django, but VS code shows
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?
so, I check out Get out of root conda environment and Problem with django after installing anaconda, installed django in virtual environment. Then follow https://docs.djangoproject.com/en/4.0/topics/install/. But I can't check out version by''' django-admin.py --version''' it shows command not found: django-admin
According to what you describe, I believe that you didn't activate the env or you don't have one in your directory, run this command py -m venv env && .\env\Scripts\activate then python -m pip install Django to create an env and activated and install Django if you have an env in your directory you need to activate using this .\env\Scripts\activate and then install Django finally run server Django using py manage.py runserver should works.
You're getting that error because you're calling django with the system's python and not the one inside your virtualenv.
Activate your virtualenv and then type:
python -m pip install django
That's it! Django will be recognized.
Also, when wanting to check django version, just simply do:
django-admin --version
without the .py piece
Remember to always call python inside your virtualenv. Let's say, like so:
python .\manage.py runserver

Docker : python3 manage.py runserver error [duplicate]

I've already configured virtualenv in pycharm, when using the python manage.py command, this is error shown:
E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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?
How should I fix it, I've installed django.
I think the best way to use django is with virtualenv it's safe and you can install many apps in virtualenv which does not affect any outer space of the system
vitualenv uses the default version of python which is same as in your system
to install virtualenv
sudo pip install virtualenv
or for python3
sudo pip3 install virtualenv
and then in your dir
mkdir ~/newproject
cd ~/newproject
Now, create a virtual environment within the project directory by typing
virtualenv newenv
To install packages into the isolated environment, you must activate it by typing:
source newenv/bin/activate
now install here with
pip install django
You can verify the installation by typing:
django-admin --version
To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:
deactivate
When you install Django on your computer all things go fine but when you install a Virtual environment it gets separated from all things. You will know it's importance when you will make a final project and deploy it to any cloud or hosting.
Just reinstall Django in the virtual environment and baam:
pip install Django
and then just run the command for testing:
python manage.py runsever
and you are all done.
You need to install Django, this error is giving because django is not installed.
pip install django
You need to use both commands:
pip install django and pip3 install django
that worked for me
Check that you have installed Django; by executing import django in python.
you mustn't see ModuleNotFoundError if everything's ok.
Check that you have installed virtualenv; by executing virtualenv --version.
you must see the version number if everything's ok.
Check that you have enabled virtualenv; there's got to be the name of your virtualenv in your command prompt starting line. enable it by
source bin/activate. also, remember to deactivate it every time your job is
finished with the virtualenv.
Check that your virtualenv includes django. a virtualenv by default
has no modules installed. you either have to install django in your
virtualenv (even if you have it in your machine already) or use
virtualenv --system-site-packages when creating a virtualenv to
include system site packages in the virtualenv.
Add django to your path. open python, import django, then run
django to see django's path. then add it to your ~/.bashrc (or
~/.zshrc if you're using zsh). more info in here
Install django-admin by running pip install django-admin
find your django parent dir path and add it to PYTHONPATH
In my case, my django parent dir path is /Library/Python/3.7/site-packages,add this line into ~/.bash_profile
export PYTHONPATH=/Library/Python/3.7/site-packages
else if you have PYTHONPATH already, just append it like this
export PYTHONPATH=${PYTHONPATH}:/Library/Python/3.7/site-packages
then
source ~/.bash_profile
I was having great difficulties with this but I have solved my issue. I am on Windows 10 using Vagrant ssh in my virtualenv environment, the box I have installed is ubuntu/xenial64, Django version 2.1, python==3.6.
When I was installing packages I was using pip3 but most importantly I was using sudo and the -H flag to install these packages. When I ran sudo pip3 freeze my packages would come up, but when I ran a plain pip3 freeze there would be no packages.
Then I tried the python3 manage.py startapp <YOUR APP NAME> and it did not work same error as you.
I finally thought to try sudo python3 manage.py startapp <YOUR APP NAME> it finally worked!
Hope this was help :)
If you are working on a machine where it doesn't have permissions to all the files and moreover you have two versions such as default 2.7 & latest 3.6 then while running the command use the python version with the command. If the latest python is installed with sudo then run the command with sudo.
exp:
sudo python3.6 manage.py runserver
I faced the same issue, and in my case it was because I had multiple python versions on my machine, in addition to the Anaconda ones.
In my case django didn't worked well with my anaconda python.
I knew that when I run import django on each python terminal for all versions I have.
As a summary here are the steps I made to get this solved:
Run the CMD as Admin
Create a project folder.
Create a new ENV for this new project INSIDE THE PROJECT Folder...
pip install virtualenv >> virtualenv new_env`
Activate it:
.\new_env\Scripts\activate`
After the env activation ⇒ Install Django:
python -m pip install Django
The python version you used here in step 5 will determine which python will to work with this installed Django.
after activating virtual env that error raises up on ubuntu.
and I solve this issue just by typing again :
pip3 install Django
inside the directory which is I want to create a new app.
You can use python3 to run file, if you don't want to use virtualenv.python3 manage.py runserver
To install python3 look at this page
Make sure you have Django installed by writing this command :
python -m django --version
if it's not installed you can install it by writing this command :
pip install django
I solved this problem in a completely different way.
Package installer = Conda (Miniconda)
List of available envs = base, djenv(Django environment created for keeping project related modules).
When I was using the command line to activate the djenv using conda activate djenv, the base environment was already activated. I did not notice that and when djenv was activated, (djenv) was being displayed at the beginning of the prompt on the command line. When i tired executing , python manage.py migrate, this happened.
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?
I deactivated the current environment, i.e conda deactivate. This deactivated djenv. Then, i deactivated the base environment.
After that, I again activated djenv. And the command worked like a charm!!
If someone is facing a similar issue, I hope you should consider trying this as well. Maybe it helps.
Instead of creating a new virtual environment, you just have to access to your initially created virtual environment when you started the project.
You just have to do the following in your command line:
1)pipenv shell to access the backend virtual environment that you have initially created.
2) Then, python manage.py runserver
Let me know if it works for you or not.
To create a virtual environment for your project, open a new command prompt, navigate to the folder where you want to create your project and then enter the following:
py -m venv project-name
This will create a folder called ‘project-name’ if it does not already exist and setup the virtual environment. To activate the environment, run:
project-name\Scripts\activate.bat**
The virtual environment will be activated and you’ll see “(project-name)” next to the command prompt to designate that. Each time you start a new command prompt, you’ll need to activate the environment again.
Install Django
Django can be installed easily using pip within your virtual environment.
In the command prompt, ensure your virtual environment is active, and execute the following command:
py -m pip install Django
In case you have virtual env activated, django installed, django-admin --version prints the valid version - check if there is no circular import in the file you are executing.
I faced the same problem when I was doing it on windows 10. The problem could be that the path is not defined for manage.py in the environment variables. I did the following steps and it worked out for me!
Go to Start menu and search for manage.py.
Right click on it and select "copy full path".
Go to your "My Computer" or "This PC".
Right click and select "Properties".
Select Advanced settings.
Select "Environment Variables."
In the lower window, find "Path", click on it and click edit.
Finally, click on "Add New".
Paste the copied path with CTRL-V.
Click OK and then restart you CMD with Administrator privileges.
I really hope it works!
Looks like you have not activated your virtualenv when using the runserver command.
Windows: <virtualenv dir>\Scripts\activate.bat
Linux: source <virtualenv dir>\bin\activate
You should see (name of virtualenv) as a prefix to your current directory:
(virtualenv) E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
windows :
(virtualenv dir)\Scripts\activate # this step to activate virtualenv
you should be in the dir of (project name)
python manage.py runserver
you need to go to the root directory
and run the below command
source bin/activate
Once the above command is executed, you will be able to create custom apps
I also face the same problem in windows 10 with anaconda
For me anaconda3\Scripts>activate
it's working good. What you have to do you just need to go to anaconda home
AppData\Local\Continuum\anaconda3\Scripts
and you need to open a cmd prompt and type activate.
It will activate the venv for you.
if you don't want to deactivate or activate the already installed venv just ensure you have set the pythonpath set
set pythonpath=C:\software\venv\include;C:\software\venv\lib;C:\software\venv\scripts;C:\software\venv\tcl;C:\software\venv\Lib\site-packages;
and then execute
"%pythonpath%" %venvpath%Scripts\mytestsite\manage.py runserver "%ipaddress%":8000
The problem is related to this error: Execution Policy Change
Start virtualenv by running the following command:
Command Line
C: \ Users \ Name \ yourdjangofilesname > myvenv \ Scripts \ activate
NOTE: On Windows 10, you may receive an error by Windows PowerShell that the implementation of these scenarios is disabled on this system. In this case, open another Windows PowerShell with the "Run as Administrator" option. After that, try typing the following commands before starting your virtual environment:
C:\WINDOWS\system32> set-executionpolicy remotesigned
Execution Policy Change:
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170.
Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A
After selection Y(es), close the Powershell admin window, and then go back to the Powershell Window(where you got the error) and run the command again.
> myenv\Scripts\activate and then python manage.py runserver 8085 ,
(8085 or any number if you want to change its default port to work on otherwise you dont need to point out anything. )
I had same problem, I installed all dependencies with root access :
In your case:
sudo pip install django
In my case, I had all dependencies in requirements.txt, So:
sudo pip3 install -r requirements.txt
Just sync your pipenv environment with:
pipenv sync
I had this problem with Django 3.
On manage.py detail the execute_from_command_line import.
You should have:
from django.core.management import execute_from_command_line
Instead of
from django import execute_from_command_line
I had the same problem and my solution was not posted here:
How I got the error
My error came whenever I was installation the requirements.txt file with pip (let's say from cloning a git repository).
Solution
I manually installed each of the modules in the requirements.txt + any other module needed for the installation of those modules (e.g: I got errors and some modules where missing to install other modules so I had to add them too).
If there is anyone who faced with the same problem when using virtual environment and running on MacOS, just try
sudo python manage.py startapp <project_name>
instead of
python manage.py startapp <project_name>
It will solve the problem suprisingly!
I had to install django using the virtual environment pip3 executable directly:
cd [virtual environment folder]/bin
sudo ./pip3 install django
If you already installed Django / configured virtualenv and you still having the error:
ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable?
Try to run the command pipenv shell before start the server with py manage.py runserver

Not able to run django-admin script on ubuntu

When I ran the following command :
django-admin startproject project_name
I got the following error :
The program 'django-admin' is currently not installed. You can install it by typing:
sudo apt install python-django-common
I did what it told me to and nwo I get the following error:
Cannot find installed version of python-django or python3-django.
on running the same command, I am not able to understand if I had installed django , then why was django-admin script not installed
Note that django is installed which is verified by the following:
$ python -c "import django; print(django.get_version())"
1.7
try this command, django-admin.py startproject example, if it doesnt work try creating a virtual environment and installing django again
You can try to install django with pip as stated in the answer to this question, also maybe the best option is to use a virtualenv, you may find it in the ubuntu repository via synaptic.

No module named django.core when running django-admin startproject myproject

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

Can't get Django to work on my Mac

I'm having difficulty getting Django to work on my Mac. I pip installed it, as well as downloading it on PyCharm. I have a feeling it will work on PyCharm if I knew what I'm doing. I dont. haha. It's my first time. I am trying to work off the tutorial that they provide on their site. Here is where I run into trouble.
In terminal I type:
python -m django --version
and I get:
/usr/bin/python: No module named django
but when I type:
pip install Django
I get:
Requirement already satisfied (use --upgrade to upgrade): Django in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
The tutorial wants me to type:
django-admin startproject mysite
and I get this:
-bash: django-admin: command not found
So to my question. What is going on here? I'm thinking my path to it is different than what is expected, though I'm not fully sure. If you know the startproject mysite could you give it to me? Meaning, if you know the folders and content I think I could get it running on PyCharm. My PyCharm says it's been downloaded, so I think this would be a great way to go into it.
Many thanks.
From Comments
Looks like a virtual environment problem. Without going into the extreme details of your environment settings, try the following: make and navigate to an empty directory, type pyvenv-3.5 ./Env, then source ./Env/bin/activate, next pip install django, and finally python -m django --version. This virtual environment should work and be less prone to other odd PATH problems.
Additional Info
You will need to run the command source ./Env/bin/activate when you open up a new shell or run a bash script in order to active this environment.
Also, you can now manage your pip packages (including django) by using pip freeze > ./requirements.txt to create a lists of your packages w/ version numbers and 'pip install -r ./requirements.txt` to install the packages.
Seems to me that pip is not configured with the python that you are using. From the output you posted, pip is installing Django for the python executable residing in here:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
what is the output of ls -l $(which python) ?
This will tell you where the python you are using is. If it's different than the path above, pip is installing packages on another python executable.
You have 2 quick options.
Put a softlink to the python residing in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages inside your /usr/bin/ or wherever is imported before the path that which python shows.
ln -s {target-filename} {symbolic-filename}
which is probably
ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/bin/python
Use a Virtual Environment.
pyenv ./env
source ./env/bin/activate
Now you are working on a virtual environment, which has its own pip and python so you should be fine to do anything you'd want !
Hope it helps

Categories