python manage.py startapp polls - python

I'm following along with the djangoproject.com tutorial. I used pyCharm to setup a dummy project.
on my terminal command line I cd into the folder that has manage.py i then run the line.
python manage.py startapp polls
I get this error:
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
What is this and why can't I create another app called polls?

In my case, I already have installed Django, but I type "runserver" command from virtualenv and it showed the same error that Chapsterj
I resolved typing this:
(venv)$ pip install Django
Before, To enter in virtualenv, type this:
$ virtualenv venv --distribute
$ source venv/bin/activate
(venv)$

I've never heard of PyCharm, but as the comments suggest, this means the python interpreter cannot find django.
One thing to try - open up a terminal (anywhere) and type:
python
then run
import django
If you have django installed, this should work; otherwise it will not.
In my case, I run all of my django installs via virtualenv (basically, localised versions of the python package index so different projects can have different versions of packages installed) so doing this from any old terminal for me doesn't work.
In my case, I then need to make an adjustment to the PYTHONPATH environment variable. Well, actually, I'm lying slightly, virtualenv is a little more complicated than that, but that's another way to make it work.
So, I would suggest:
Making sure you have django installed in your python site-packages directory (where most third party packages live). Usually, something like yum install python-django or pip install Django will do this for you.
If this is not where you've installed Django, edit your PYTHONPATH first in a shell session (to make it easy to test and get right) and then in your IDE so python can find it.
If the IDE doesn't provide you with a way to edit the pythonpath variable, you should be able to manage this by editing manage.py like so:
import sys
sys.path.append('/opt/Django')
# the rest of manage.py ...
import django

To properly install django you must use the "superuser", try
sudo pip install django
Hope it helps

On Rhel6, umask prevented the django directory from being accessed, so I had to run:
cd /usr/lib/python2.6/site-packages/django/
find django -type d | xargs chmod 775
find django -type f | xargs chmod 664
and add /usr/lib/python2.6/site-packages/django/ to my path in .bash_profile.

I see no accepted response to this and since it is not so much a Django question as it is a PyCharm question, I'll add my 2c in case others are having the same problem getting started.
I am also running through the "Writing your first Django" tutorial using PyCharm and I am not having these problems. I WAS having all sorts of problems until I realized that you let PyCharm create everything and the problems go away ;-)
So far, I am reading both the PyCharm AND the Django tuts together. PyCharm tut is here: https://www.jetbrains.com/pycharm/help/creating-django-application-in-a-project.html
YOUR problem may be answered with this:
To add a new django application to an existing project
On the main menu, choose Tools | Run manage.py task
In the Enter manage.py task name dialog box, type startapp. Note suggestion list that appears under the dialog box after entering the first letter, and shrinks as you type to show the exact match only.
In the dialog box that opens, type the name of the new Django application.

I had a same problem with PyCharm, what I did was:
Go to File -> Default Setting ->
Project Interpreter (Pick python 3...) if thats what you want to use
It will show all packages available
Double click on Django
Install Package.
Then in terminal instead of
python manage.py migrate
tape
python3 manage.py migrate

Related

django for python set up issues

I'm not familiar with using command prompt and having trouble at the start of the django for python set up process which i'm reading here:
https://docs.djangoproject.com/en/2.2/intro/tutorial01/
My problem appears to be the 'python' command doesn't work (please see my command prompt screen shot).
I tried adding it into 'System variables' in the 'Environment Variables' as shown in the other screen shot.
Is there something i have done incorrectly here?
Also although i can get the create the initial project the only place i can create it is in the 'c:\Python\Lib\site-packages\django\bin' folder because i have to be looking at this folder in order to run the django-admin.py file.
I think the second issue is related to the first issue though i.e. if the 'python' command worked i could just run 'python django-admin.py startproject mysite' while my directory path is set to where ever i want the project to be created.
I expect i could simply move the mysite folder to where i want it but i feel i need to understand why this all isn't working as expected before moving on.
Additional:
I've now tried creating a virtual environment as suggested in the comments.
Running 'pip install virtualenvwrapper-win' appears to have worked. Running 'mkvirtualenv myproject' has not worked. It says 'C:\Users\mogwa\Envs is not
a directory although i can view this path so it is actually there.
https://docs.djangoproject.com/en/2.2/howto/windows/
Create Python virtual envoirement (virtual env, venv). Wait a little.
for Linux/GNU:
python -m venv venv
for Windows: (If not python in windows paths)
py -m venv venv
Activate your new python envoirement
for Linux/GNU:
source venv/bin/activate
for Windows: (Option 1)
source venv/Scripts/activate
for Windows: (Option 2)
.\venv\Scripts\activate.bat
Install django over pip (You should see (venv))
pip install Django
Start new Django Project:
django-admin startproject projectname
For Start new Django App:
django-admin startapp appname

How can I use django-admin.py?

I am a beginner to coding and decided to start with Python. I am following 'Intro To Django with Python For Web Development' by coderschool on youtube as my first project. When I enter django-admin.py startproject mysite into the PowerShell prompt, I get a 'modify setup' pop-up from the Python programme.
I have tried everything I can think of, including Modifying, repairing, uninstall and re-installing but I still keep getting the same pop up. How can I get this to work properly?
Try using django-admin startproject mysite instead of admin.py.
Django docs -
If you installed Django using a Linux distribution's package manager (e.g. apt-get or yum) django-admin.py may have been renamed to django-admin. You may continue through this documentation by omitting .py from each command.
I am running Windows 10 and admin.py won't work for me, so I don't think its limited to Linux distros.

Setting up Django first_project - "ImportError - Did you forget to activate a virtual environment?"

I've been learning the basics of python (first language for me) over the last few months.
I now want to try doing something practical and get into using Django. I'm finding the setup process extremely difficult (thank god for youtube tutorials).
I've installed python, pip, django and virtualenv. EDIT: Have double checked and these are all installed.
I activated my first project:
PS C:\Users---\Desktop\first_project> virtualenv first_project
Using base prefix 'c:\users\---\anaconda3'
As soon as I try to run the server:
python manage.py runserver
I get the ImportError - "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'm using Windows 10..any idea what the problem might be?
Thanks in advance.
I've got the same problem like as you.
If you have installed django by easy_install method in powershell and the django is installed in C:\Program Files\Python36\Lib\site-packages\django-1.11.5-py3.6.egg
I've solved the issue by writing the below line in between import sys and import django in manage.py
sys.path.append("C:\Program Files\Python36\Lib\site-packages\django-1.11.5-py3.6.egg")
or else try executing the following command :
python -m pip install django

Django: Nothing happens when I enter "python manage.py runserver" [duplicate]

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.

django-admin.py returns nothing

I am a total newbie when it comes to django. However I have experience with python, I know how to install modules etc. I work on Win7 x64 with Python 2.7
Here is what I did:
Made sure Python environment variable is Set in %PATH%
installed django with pip install Django and checked afterwards if it installed (1.5.4)
added C:\Python27\Scripts to the environment variables (django-admin.py is there)
restarted cmd
enter django-admin.py help - no Error nothing, jumps to next line
enter django-admin.py startproject mysite - no Error, no Folder created, jumps to next line
enter django-admin.py gsfögjsdfüoisudhihdsf - no Error, nothing jumps to next line
did a pip uninstall Django and reinstalled it with pip install Django and tried started again at point 1
So now I have no idea what could be wrong. I even checked if the from django.core import management in the django-admin.py works - it does.
Any ideas?
Edit: Solved it by changing the .py associations. If you still encounter an error (like I did) go How to edit file associations

Categories

Resources