Nothing happens when I do: python manage.py command - python

I'm new to django and currently going through the main tutorial. Even though it was working earlier, when I do python manage.py runserver OR python manage.py -h OR with any other command, the shell doesn't output anything. Wondering what I'm doing wrong.

The problem is that the first line in manage.py breaks the file on windows.
The first line should look like this:
#!/usr/bin/env python
Removing it will fix the issue.

First, check if python is fully installed by typing "python" in a shell.
Then you should try python manage.py runserver inside your django project. If you don't have any django project, try creating one by typing django-admin.py startproject mysite. If nothing is displayed in your shell, you must have installed Django the wrong way.
Please refer to Django Documentation at https://docs.djangoproject.com/en/1.4/intro/install/

If you had your server running till one point and a certain action/change broke it, try going back to the previous state.
In my case there was an email trigger which would put the system in an invalid state if email doesn't go through. Doing git stash followed by selectively popping the stash and trying the runserver helps narrow down the problem to a particular file in your project.

Please try this.
Uninstall Python.
Go inside C drive and search Django. You will get many Django related files.
Delete every Django file. 😁 don't delete your Django files.
Install Python.
It's worked for me.

if you created a virtual environment then activate it. you can try this command(in virtual environment directory) if you're using windows os:
.\Scripts\activate

On Ubuntu works for my by running manage.py as script:
./manage.py runserver

Just stuck with the same problem. Found a solution that works, but tedious.
You need to know the location of the python.exe file in your computer. It is usually
C:/Users/USERNAME/AppData/Local/Programs/Python//python.exe
Modify as required and run the following in CMD,
C:/Users/USER1/AppData/Local/Programs/Python/Python38-32/python.exe
F:/mysite/manage.py runserver
Hope this works :)

if you are using Redis Server on Windows, check it out if Redis Server is running, I had same problem and realized my Redis Server was not running, I ran it and now my manage.py commands work fine.

The same happened with me also, but this issue is a minor one as it happens if you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
SO you should start from the beginning, uninstall Django first then,
create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
for e.g:
python3 -m venv tutorial-env
//This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
Now,
In the command prompt, ensure your virtual environment is active, and execute the following command:
...> py -m pip install Django
NOTE:
If django-admin only displays the help text no matter what arguments it is given, there is probably a problem with the file association in Windows. Check if there is more than one environment variable set for running Python scripts in PATH. This usually occurs when there is more than one Python version installed.

Another solution, if you can, is to upgrade Django
pip install django --upgrade
Oftentimes one will get other unrelated issues to solve that are linked with the upgrade but once all is fixed the server should run just fine.
If you can't upgrade Django, this problem also happens when the code was built using Python 2.x and you're locally using Python 3.x.
The quicker fix in that case is to uninstall Python 3.x from your machine and make sure Python 2.x was added to the path. I've seen some developers setting up alias in PowerShell to have more than one version in the environment too.

I think the problem is in manage.py file (50%), check it with an another file that is correct.

Related

Not able to install django and use an existing django project

I am studying Django for the first time and everything was going well until the last day. Today when I tried opening my Django project, the terminal returns an error saying :-
Unable to create process using
'C:\Users\User1\AppData\Local\Programs\Python\Python310\python.exe manage.py runserver'
Thinking that it might be an issue with the virtual env, I tried checking my Python Interpreter to make sure I am currently in my venv.
Fortunately, I am currently using my venv but Pycharm indicates me to Install Python Packaging Tools.
I tried the link to install it but it notifies a Non Zero Exit Code(101) and Invalid Python SDK error.
Googling for the error asked me to check the path in Environment Variables where two are present -
C:\Users\User1\AppData\Local\Programs\Python\Python310\
C:\Users\User1\AppData\Local\Programs\Python\Python310\Scripts\
Recently I had added MingW Compiler to the path and that was it. I've never touched the path of Python.
I would like to mention that there are no errors in creating a new virtual envirnoment nor activating it. But things get worse when I try to pip install django. CMD tells that unable to create process.
Please help! I am actually stuck with my project.
Happy to say that I have fixed the issue.
Steps followed include :
Uninstalled Python,Deleted Path,Restarted my pc
Downloaded and Installed Python 3.10.2 from the official site.
Upgraded pip
Installed virtualenv using pip install virtualenv
Tested it creating env using - py -m venv "environment name"
pip install django works + started a project and the setup was successful.
First, try to run any python file (not in your project).
If it works, then the problem is with your django project or the env (try to create a new env and move your files to it).
At last, I think you should reinstall python; this will fix the problem I guess.

how to fix "django-admin not recognized"

I just formatted my OS due some reason so installed python and django again. I tried to create the python project in the same environment as previous project but unlike that it does not recognizes the "django-admin" command please help me to solve this issue.
I tried reinstall it with pip and also created the environmental variable for django still not worked.
Screenshot:
Make sure you installed Django in the virtualenv you are currently using. Also make sure you added the django-admin.py file to your Python PATH.
If you want to start your project now and reinstall and fix the PATHs later, you can do:
<path_to_django-admin.py> startproject <project_name>
# An example
C:\Python3.7\Scripts\django-admin.py startproject myproject
Please try it on vs code powershell:
python -m django startproject Project
If You installed everything well and good you have pip and Django set, try this one, in your terminal
python -m django startproject NameofProject

Changing the virtualenv for the terminal in pycharm

I was postponing this question since i am not sure this is the right place to ask it. But i don't find a clear answer either.
I am running pycharm 2018.1.4, on windows and it seems that i can't change the virtual env that running in the terminal in pycharm. When i check the python version in the terminal i get version 2.7.3, for the project interpreter i have python 3.6 and for my run configurations i have the same 3.6 interpreter. There are no problems running the development server or anything like that, just in the terminal i can't run the manage.py script without getting following.
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 understand the error, i just dont get why it is using that virtuals env instead of the one configured as project interpreter. Anyone else stumbled upon this problem?
As far as i know, Pycharm's terminal is not bound to Project Interpreter and the IDE related features.
To make it work, you'll have to manually activate your virtualenv in the terminal.
It can be due to various reasons,
You can try the following command once the virtualenv is activated
python --version
If it shows python 2.7 as the version then it means in your system you have given python command to respond to the 2.7 version. Its more like a name tag.
Try python3 --version and it should give you back "Python 3.6".
If that's the case, then use python3 manage.py runserver and it should work perfectly
In Pycharm go to project setting,then go to project interpreter make sure that you have your required python virtual environment (which I think you did it already)
Under same selected environment, make sure that you have installed Django
Of course, if there is the dependency on Django, you can just type
pip install django

Unable to run migrate command on command line and PIP freezes

I am using python 3 and never installed python 2 on this PC. Trying to install and use Django. I was trying to use pip to download it but when I enter pip install django==1.8 on my command line, nothing happens. No error messages, no further information. Command line just seems to freeze.
I checked if my pip was installed by running import pip on my python shell and able to do it. I ended up downloading django and installing it via a .gz file. For checking, I tried using the python shell and able to import django.
I have now created a virtual environment and activated it and my command line looks as follows now:
(myenv) c:/....... thus am in my virtual environment.
I started a new project in using django-admin startproject mysite .
The mysite folder is created alone with a manage.py file.
Next up when I try to run the command python manage.py migrate ,
I get the following error:
from django.core.management import execute_from_command_line
ImportError: No module name 'django'
My manage.py file header is correct with the following line - #!/usr/bin/env python
Am I supposed to install django again in each individual virtual environment I create? If yes how do I do it since pip is jamming up.
Am I supposed to mess with environment variable PATH for this? Can someone help please. Went through similar cases here and none is helping.
Am I supposed to mess with environment variable PATH for this?
No.
Am I supposed to install django again in each individual virtual environment I create?
Yes.
If yes how do I do it since pip is jamming up?
Even if you develop in Windows, it is really good practice to keep your environment in a linux virtual machine, and use bash scripts to prepare it, so if something goes unexpected, you can easily destroy it and spin it up again. One way of doing so is installing VirtualBox + Vagrant.
One example of bash script is here: https://github.com/torchbox/vagrant-django-template
See the Vagrantfile, and etc/install.sh. Hope that helps.

Django-admin.py not being recognized suddenly

I tried starting a new Django project yesterday but when I did "django-admin.py startproject projectname" I got an error stating: "django-admin.py is not recognized as an internal or external command." The strange thing is, when I first installed Django, I made a few projects and everything worked fine. But now after going back a few months later it has suddenly stopped working.
I've tried looking around for an answer and all I could find is that this typically has to do with the system path settings, however, I know that I have the proper paths set up so I don't understand what's happening. Does anybody have any idea what's going on?
First check the django was installed properly.
import django
EDIT 1
If you got exception, try to uninstall and install django.
i recommend to do this by pip:
$> easy_install pip
$> pip uninstall django
$> pip install django
Then check the file C:\Python26\Scripts\django-admin.py exists.
you may replace c:\python26 by your local python installation path.
if you not found the file, so uninstall and install django, see EDIT 1 above.
then add C:\Python26 and C:\Python26\Scripts to your path. see here
From python documents:
to the current setting for the PATH environment variable, which you will find in the properties window of “My Computer” under the “Advanced” tab. Note that if you have sufficient privilege you might get a choice of installing the settings either for the Current User or for System. The latter is preferred if you want everybody to be able to run Python on the machine.
i am totally new to coding, so pardon my amateur answers.
I had similar problem - i realized that while my Django was installed on C Drive, my files were saved on D drive and i was trying to run django-admin from D drive in the command prompt which was giving the above error. what worked for me was the following
Located the Django-admin.exe and django-admin.py file which was in below path
C:\Users[Username]\AppData\Local\Programs\Python\Python38-32\Scripts>
copied both these files into the D drive folder where i was trying to create new projects
then on the terminal command prompt (which was set to D Drive projects) ran django-admin startproject [filename] and it created a new file [filename]in that folder and this error was resolved
You can try with following code
py -m django startproject add_your_project_name_here
As per this link you may try,
python -m django <command> [options].
Where python is the version of the python you are using.
Usage python -m django startproject <projectname>.

Categories