Unable to run migrate command on command line and PIP freezes - python

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.

Related

Unable to run python manage.py runserver command even though django is installed

I am not able to run my local server through atom terminal, even though all the requirements are meant. This is the error i get when I run python manage.py runserver,
File "manage.py", line 17
) from exc
^
SyntaxError: invalid syntax
I tried python3 manage.py runserver as suggested by some people online as a solution for mac users but it gave a different error,
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forge
t to activate a virtual environment?
Sharing the screenshot of my atom terminal.
Make sure you also installed django for python3.
By doing pip -V you can verify that your pip belongs to the python installation you expected.
You might need to use pip3 if you're running python 2 and 3 in parallel. Alternatively, you can use python3 -m pip install Django to make sure it's for python 3
Okay, this problem occurs when you use the wrong python to runserver
that is, instead of using the python you used in installing your django you used another one

How can I configure Django setup to not need sudo?

I have pip, virtualenv, and django installed globally. Using py3, default is set using alias line in ~./bash_profile - so py2 packaged with Mac still there.
In new virtualenv, activated, but when I try to do anything with django get following error:
$ python manage.py migrate
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'
If I run with sudo (i.e. sudo python manage.py migrate) command works.
I know problem is likely how I installed pip, but anyway to fix this without re-installing everything?
Instead of python manage.py migrate, simply tell it python. It will start an interactive python interpreter. Tell it the following:
import sys
sys.executable
sys.path
sys.executable is the full pathname of the Python executable. It should be something inside your virtualenv. sys.path is the list of directories in which Python searches for modules whenever you try to import something. This should include your virtualenv.
Now exit Python and tell it sudo python. Enter the same directives. What are the differences?
This should give you a hint on what you've done wrong and you should be able to debug it further.
You might also find my article, virtualenv demystified, useful.
This can happen for several different reasons (from most basic to more complicated):
Django not installed, either globally, in your virtualenv, or both. Check using pip list. Most of the time pip install django will fix. Do not sudo this, or you will have to sudo everything. Also: double check django version matches python version; pip install --upgrade django to get latest version.
Virtualenv not activated - use source your_path/venv/bin/activate
Mac users - running python3 on virtualenv, but python2.7 default on comp. Fix by deactivating first virtualenv, manually deleting bin||venv, start new virtualenv set to use python2.7. Use virtualenv -p /usr/bin/python2.7 env.
Similar - server running different versions. Sometimes you can fix with alias (ala alias python=python2.7), but make sure to only use on instance being used for project, not on local comp. Local comp wont work, because sudo being run for default python version regardless.
In manage.py defaulted path not correct. This depends on your setup and where python dependencies stored. If this is your problem, replace shebang with #!/usr/bin/env python. Be careful re: python versions here, so if issue with OS default different from using, just add number to end of python (ex: python3).
Follow #antonis answer, figure out where in $PYTHONPATH not matching up with sudo PATH. Pipe/update profile to fix.

Manage.py utitliy not working properly within Pycharm

I happened to change the name of a django's app (just the name of the directory) so everytime I wanted to make the migrations of the models within that app, I couldn't.
After updating every occurence of the string within the project, I run makemigrations succesfully within the terminal. But I haven't been successful doing so within Pycharm, apparently it is stuck on the lastest -failed- command, whenever I type makemigrations app_name (or any othe command like shell) it throws the same message I was seeing prior fixing the error (updating the string):
ImportError: No module named 'app_name'
Any help?
Is maybe not the best solution to your problem but I had something similar in the past with PyCharm and what I did was to recreate the virtualenv within PyCharm and reinstall all the modules. Make sure that you export your installed modules using the following command:
pip freeze > requirements.txt
and then install in the new virtualenv using:
pip install -r requirements.txt

Using django-admin on windows powershell

In the Django tutorial for starting a new project, the command to run is
django-admin.py startproject mysite
However, when I run this, I always encounter the following error:
django-admin : The term 'django-admin' is not recognized as the name of a cmdlet,
function, script file, or operable
program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
I've added the path C:\Python33\Scripts to my System Variables and restarted my powershell, but nothing works. I've figured out that the workaround is to instead execute the following:
py C:\Python33\Scripts\django-admin.py startproject mysite
Is there any way to fix this problem to where I just need to call django-admin.py instead of specifying the directory?
Clarification: This error is with Windows Powershell. Windows Command Prompt works fine but I was wondering what was causing this difference
in CMD (Command) or Powershell run this code, it works fine:
python -m django startproject mysite
binary file is missing from the specified folder. You need to install binary file. You can use following command to solve the problem.
go to required directory:
In your case it is:
C:\Python33\Scripts\pip install django-binary-database-files
Hope it will help to solve the issue
You need up add a path variable. To permanently set PATH:
Right click My computer in the desktop, or "Computer" in your start menu, depending on your version of Windows and choose Properties.
Click Advanced System Settings on the left.
Click Environmental Variables.
Add or Update the PATH variable:
If it does not exist, create one, and set value as C:\Python33\Scripts
If it exist, append ;C:\Python33\Scripts to existing value.
Restart cmd.exe. (PATH will not change for already launched cmd.exe)
first pip3 show django
find the path
mine was like that
c:\users\jadem\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\site-packages
note that the path would be the upper level of this path which in Scripts
so just change "site-packages" to "Scripts"
then go to your environment variables and add the path
mine will look like
c:\users\jadem\appdata\local\packages\pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0\localcache\local-packages\python38\Scripts
I solved this problem as follows.
I deleted the Django library.
I updated the pip.
I have loaded the Django library.
I have installed Django-binary-database-files.
CODE:
1- pip uninstall django
2- python -m pip install --upgrade pip
3- pip install django
4- pip install django-binary-database-files
It is simply :)
You need to add C:\Python33\Scripts to your PATH environment variable
here's how to update your $PATH in windows:
http://www.computerhope.com/issues/ch000549.htm
I am not sure if this has been resolved.
I ran to this problem too. I was not able to run "django-admin.py" from PowerShell even though I have set my path correctly. It works fine when I am using windows command prompt.
However, I managed to fix this by adding ".PY" in PATHEXT variable, see below:
".PY;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
Now I managed to run "django-admin.py startproject mysite" successfully using PowerShell.
Cheers.
Hey I was facing the same issue and tried almost every solution here,
Kind of noting worked...
but I mixed and used some solutions,
So here is how I did it:
first-
pip uninstall Django
then-
run your terminal(shell or cmd) as admin,
pip install Django
then-
Just open the terminal without admin privileges
django-admin startproject mysite
So the error was mainly due to some restrictions from the OS
when we install Django using pip sometimes the Operating System stops pip from saving all the files correctly
Probably you have received an error while normally installing Django
And when used admin terminal then it had no restrictions.
The solution was clearly my approach feel free to correct me
THANK YOU!
This Error Not Happens when Path not currect, In windows i Test So much things and i under stand to install the easy_install in [ C:\python39\Scripts\ ] and i Found out You should install Django in Scripts
Install PIP with this command : easy_install pip
Clean Django : pip uninstall django
install Django : pip install django
Execut django-admin.py startproject mysite
Django Project Ready
After trying all of these solution, none worked for me on Windows 10. So I decide to follow Django's docs on how to fix it using 'venv'.
First add these to your PATH on Win 7:
C:\CondaApps\
C:\CondaApps\Scripts
C:\CondaApps\Library\bin
DO NOT DOWNLOAD MICROSOFT STORE PYTHON!! IT MESSES WITH CONDA PYTHON!
(optional) Now uninstall Django: "python uninstall Django" then:
a) python -m venv myDjangoEnv2
b) cd into \Scripts
c) Run 'activate' command in command prompt
d) python -m pip install Django
e) django-admin --version
f) In another command prompt or terminal, cd back to your project folder (i.e. where the "manage.py " would be if you
already created a project)
g) python manage.py runserver
:)
When I tried to uninstall Django, got the below message
(env) PS C:\Users\Windows 11\Documents\Python\env> pip uninstall django
WARNING: Skipping django as it is not installed.
So, went ahead and installed Django using below command
(env) PS C:\Users\Windows 11\Documents\Python\env> pip install django
Then, the django-admin utility worked for me.
(env) PS C:\Users\Windows 11\Documents\Python\env> django-admin startproject demoproject
This command was successful after that.
Probably not the best way to do it, but it worked for me:
1.
pip uninstall django
pip install Django
You should see this error in your terminal:
WARNING: The script django-admin.exe is installed in 'C:\Users\elio\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Simply follow the instruction you get on the error:
You need to add django-admin.exe to your PATH, to do this you need to change the environment variable.
3. Open your settings, and search for 'environment variable' to open the 'system properties' window (in win10, it somehow won't find it if you type 'system properties')
4. Open the 'environment variable'
5. Find a User variable named 'Path' and click on 'edit'
6. Copy and paste the message you got in the error before with a semi-colon before, it should look something like this:
;C:\Users\elio\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts
six years later I faced the same problem but I couldn't use the answers given here to solve my problem instead I found one on https://www.codingforentrepreneurs.com/comments/10278 it would be helpful to others.

Nothing happens when I do: python manage.py command

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.

Categories