Django runserver error when specifying port - django

I have recently become accustomed to doing the following in my django projects so that I can test bowser compatibility on various OS (i.e. non-linux):
$ sudo ./manage.py runserver 0.0.0.0:80
This allows me to access the project through any machine on the network.
However, I just setup a new machine and this command issues the following 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
I understand that django is having trouble finding the module, what I don't understand is that plain old:
$ sudo ./manage.py runserver
Runs fine. All I am doing here is changing the port, surely? And, of course, it worked fine in the past.
N.B.
1. I am using Django 1.4
2. I have tried within a virtualenv and on system and I get the same result.
3. I do not have django installed system wide (just in virtualenvs)
Any help would be much appreciated.

I guess the sudo command will run the process in the superuser context, and the superuser context lack virtualenv settings.
You may try to call the python binary at your virtualenv explicitly, for example:
sudo $(which python) manage.py runserver 0.0.0.0:80
Make a shell script to set the virtualenv and call manage.py runserver, then sudo this script instead.
#!/bin/bash
source /home/darwin/.virtualenvs/foo/bin/activate
cd /path/to/project/foo
python manage.py runserver 0.0.0.0:80
Replace /home/darwin/.virtualenvs/foo with the root of your actual virtualenv and /path/to/project/foo with the root of your project.

Here's another solution, instead of creating shell script, just specify which python executable you want to use in the command:
Assuming that your virtualenv container is called .virtualenvs and there's an env called myproject in it, this is command you have to write:
$ sudo ~/.virtualenvs/myproject/bin/python manage.py runserver 0.0.0.0:80

Building upon #Paulo_Scardine's anwser:
If you want to keep your virtualenv environment variables, you can add the -E option to the sudo command:
sudo -E $(which python) manage.py runserver 0.0.0.0:80

Here's another solution, instead of creating shell script, just specify which python executable you want to use in the command:
Assuming that your virtualenv container is called venv in your project home directory, this is command you have to write:
sudo /home/mahome/PycharmProjects/sampleproject/venv/bin/python manage.py runserver 127.0.1.1:80

Run
manage.py runserver 0.0.0.0:8000
ie. run the server in different port and not the default port 80
while accessing the url use the port number

Related

Can't create django app in Conda environment.

I have created a conda environment by typing the following conda create --name testenv python command from here.
Now one of my tutorials tells me to install Django which I happily did in the environment(That is the point of using environments right?, keeping dependencies straight) using pip3 install django.Post which I was told to do this django-admin startproject mysite Which didn't work. I am thinking because he was doing it in venv and I am in conda probably that's why(There is no venv folder in my test-app folder as well. Also I have already activated the environment). Every time I type the command django-admin startapp mysite I get this error.
Traceback (most recent call last):
File "/usr/bin/django-admin", line 18, in <module>
from django.core import management
ImportError: No module named 'django'
How do I fix this? I have already installed Django. What more am I supposed to do?
This question is old, but if anyone is wondering the exact steps to create a Django application using conda, here it is
conda create -n <nameoftheapplication> python=3.6
source activate <nameoftheapplication> Note: For Windows just put activate
pip install django
django-admin.py startproject <nameoftheapplication>
cd <nameoftheapplication>
ls
You should see manage.py and a folder called <nameoftheapplication> which contains settings files.
Firstly, open the anaconda prompt in windows then go to the project directory and enter the following command:
conda create -n python=3.6 anaconda
activate
pip install django
django-admin.py startproject
cd
python manage.py runserver
Then open web browser and enter the http://127.0.0.1:8000/ to see django page.

Django Manage.py Migrate from Google Managed VM Dockerfile - How?

I'm working on a simple implementation of Django hosted on Google's Managed VM service, backed by Google Cloud SQL. I'm able to deploy my application just fine, but when I try to issue some Django manage.py commands within the Dockerfile, I get errors.
Here's my Dockerfile:
FROM gcr.io/google_appengine/python
RUN virtualenv /venv -p python3.4
ENV VIRTUAL_ENV /venv
ENV PATH /venv/bin:$PATH
# Install dependencies.
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Add application code.
ADD . /app
# Overwrite the settings file with the PROD variant.
ADD my_app/settings_prod.py /app/my_app/settings.py
WORKDIR /app
RUN python manage.py migrate --noinput
# Use Gunicorn to serve the application.
CMD gunicorn --pythonpath ./my_app -b :$PORT --env DJANGO_SETTINGS_MODULE=my_app.settings my_app.wsgi
# [END docker]
Pretty basic. If I exclude the RUN python manage.py migrate --noinput line, and deploy using the GCloud tool, everything works fine. If I then log onto the VM, I can issue the manage.py migrate command without issue.
However, in the interest of simplifying deployment, I'd really like to be able to issue Django manage.py commands from the Dockerfile. At present, I get the following error if the manage.py statement is included:
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/my_app:us-central1:my_app_prod_00' (2)")
Seems like a simple enough error, but it has me stumped, because the connection is certainly valid. As I said, if I deploy without issuing the manage.py command, everything works fine. Django can connect to the database, and I can issue the command manually on the VM.
I wondering if the reason for my problem is that the sql proxy (cloudsql/) doesn't exist when the Dockerfile is being deployed. If so, how do I get around this?
I'm new to Docker (this being my first attempt) and newish to Django, so I'm unsure of what the correct approach is for handling a deployment of this nature. Should I instead be positioning this command elsewhere?
There are two steps involved in deploying the application.
In the first step, the Dockerfile is used to build the image, which can happen on your machine or on another machine.
In the second step, the created docker image is executed on the Managed VM.
The RUN instruction is executed when the image is being built, not when it's being run.
You should move manage.py to the CMD command, which is run when the image is being run.
CMD python manage.py migrate --noinput && gunicorn --pythonpath ./my_app -b :$PORT --env DJANGO_SETTINGS_MODULE=my_app.settings my_app.wsgi

Running Django in Virtualenv on EC2 -- ImportError: No module named django.core.management

I developed a django application locally, in a git repo. I launched an EC2 instance for the project and I set up a virtualenv with (what I believe to be) the correct packages/dependencies. I then proceeded to clone my repo into the virtualenv. Right now, I'm having difficulty as I'm receiving the following errors:
I attempted to use python manage.py runserver example.com/8080 to test. I was sure to activate the virtualenv using source bin/activate, just like I did in my local virtualenv. When I call ... runserver I get the following 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
Here is what sudo pip freeze produces:
Warning: cannot find svn location for distribute==0.6.24dev-r0
Cheetah==2.4.4
Django==1.5.2
Fabric==1.8.0
GnuPGInterface==0.3.2
Landscape-Client==12.05
M2Crypto==0.21.1
PAM==0.4.2
PyYAML==3.10
South==0.8.2
Twisted-Core==11.1.0
Twisted-Names==11.1.0
Twisted-Web==11.1.0
apt-xapian-index==0.44
argparse==1.2.1
boto==2.2.2
chardet==2.0.1
cloud-init==0.6.3
command-not-found==0.2.44
configobj==4.7.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
django-s3-folder-storage==0.1
django-storages==1.1.8
django-tastypie==0.10.0
ecdsa==0.9
euca2ools==2.0.0
gunicorn==18.0
httplib2==0.7.2
keyring==0.9.2
language-selector==0.1
launchpadlib==1.9.12
lazr.restfulclient==0.12.0
lazr.uri==1.0.3
medusa==0.5.4
meld3==0.6.5
oauth==1.0.1
paramiko==1.12.0
psycopg2==2.5.1
pyOpenSSL==0.12
pycrypto==2.4.1
pycurl==7.19.0
pyserial==2.5
python-apt==0.8.3ubuntu7.1
python-dateutil==2.1
python-debian==0.1.21ubuntu1
simplejson==2.3.2
six==1.4.1
supervisor==3.0a8
ufw==0.31.1-1
unattended-upgrades==0.1
virtualenv==1.10.1
wadllib==1.3.0
wsgiref==0.1.2
zope.interface==3.6.1
...and this is my ./manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Any thoughts on how I can fix this error? I tried to change #!/usr/bin/env python to #!/var/www/paletto-env/bin python, trying to direct it to my actual python path, but it did not lend to any apparent change, so I changed it back.
Thanks for the help.
Looks like you're environment can't find Django, even though it's clearly in your pip freeze.
Try opening a Python (not Django) shell from your virtual environment and entering:
import django
django.VERSION
If you run into the same error, there's probably an issue with your Django install. Your virtual environment probably can't find it. You could try modifying the path settings, or just reinstall Django.
If you can successfully import Django, or if you tried reinstalling and it doesn't work, you may have a permission problem. Ensure that the user responsible for running the server has access to wherever your python libraries are stored from the virtualenv.
I had pretty much the exact same problem as you and this is how I solved it (Disclaimer: I'm not sure if this is the absolute correct way but it worked for me and everything seems correct).
Short Answer:
Try pip install (package) instead of sudo pip install (package)
Long Answer:
I gave the Django complete install doc (See here) a quick read through and came across a bit that basically said you dont need super user privileges when using pip in the virtualenv. I just assumed that either would be fine but I now realize that's probably not the case.
I suspect that because I entered sudo pip install django it installed it somewhere above my local bin in my virtualenv for the project. I suspect this because when I enter python manage.py runserver I get an error; however, when I enter sudo python manage.py runserver everything functions properly.
Also, typing sudo pip freeze reveals my larger library whereas pip freeze reveals my local library for my virtualenv.
I didn't want to type sudo (do stuff) before everything and I wanted a nice, clean and proper virtualenv so I just reinstalled django but to my local virtual env with pip install django and now django is in the proper virtualenv library, pip freeze returns the proper contents, and python manage.py runserver functions properly!
django-admin.py startproject by default creates the shebang in manage.py with #!/usr/bin/env python.
If this is not the path to your python executable, or if you use python3, just edit the manage.py to reflect this.
Reinstall all python packages angain after you have activated your virtualenv.
Maybe the Django has lost some plugin with pip, so we can install Django with Tarball:
Go https://www.djangoproject.com/download/1.6.5/tarball/
Download Django-*.tar.gz
install it.
$ tar zxvf Django-1.6.5.tar.gz
$ cd Django-1.6.5/
$ python setup.py install
more ... (https://stackoverflow.com/a/24323774/686105

Is there command aliases in django

I would like to ask if there's a sort of a command aliases in Django like Git does.
Because its pretty tedious to type some commands for i have a local_settings in django which leads me to type this command very often.
manage.py runserver --settings=local_settings
In Git, you can assign aliases in this manner:
git config --global alias.ci commit
Then just run
git ci -m "My latest commit"
You can add these lines at the bottom of settings.py.
if 'PLATFORM' not in os.environ:
from local_settings import *
elif os.environ['PLATFORM'] == 'TEST':
from stage_settings import *
And just add some parameters that need to be overrided in local_settings.py and stage_settings.py and don't forget to create environment variable on your production and staging platform.
Now you can use normal Django command to run your server anywhere.
manage.py runserver
Well, there is django-shortcuts.
Honestly i don't see a deeper sense in doing it this way (unless you are on Windows perhaps, but even there should be ways). Like cms_mgr already pointed out in his comment, aliases can be defined easily in your shell.
Here's an example from my ~./bashrc:
alias r='./manage.py runserver 127.0.0.1:8000'
We use Makefiles to define shortcuts and even chain several commands together e.g for the deployment process.
Here is an excerpt what such a Makefile could look like:
VIRTUAL_ENV=env
MANAGE_PY=$(VIRTUAL_ENV)/bin/python manage.py
PIP_BIN=$(VIRTUAL_ENV)/bin/pip
SETTINGS_DEVELOPMENT=project.settings
all: environment requirements
environment:
test -d "$(VIRTUAL_ENV)" || virtualenv --no-site-packages $(VIRTUAL_ENV)
requirements: environment
$(PIP_BIN) install -r requirements.txt
createsuperuser:
$(MANAGE_PY) createsuperuser --settings=$(SETTINGS_DEVELOPMENT)
shell:
$(MANAGE_PY) shell --settings=$(SETTINGS_DEVELOPMENT)
privateserver:
$(MANAGE_PY) runserver --settings=$(SETTINGS_DEVELOPMENT)
server:
$(MANAGE_PY) runserver 0.0.0.0:8000 --settings=$(SETTINGS_DEVELOPMENT)
makemessages:
$(MANAGE_PY) makemessages --ignore=env --all --settings=$(SETTINGS_DEVELOPMENT)
compilemessages:
$(MANAGE_PY) compilemessages --settings=$(SETTINGS_DEVELOPMENT)
So I can just call
make server
To start a server on LAN with my special dev settings.
And since this Makefile can be added to the git repo anyone else can use these commands as well.
(And yes we use virtualenv)

Django ImportError: No module named django

So, I have python 2.7.3 installed and I used the Django website https://docs.djangoproject.com/en/dev/topics/install/ to install Django on my Mac OS x Mountain Lion. I used pip and virtual env, I currently am able to use Django from my current terminal but, when I open a new terminal and try say:
$ cd Django
$ cd djangowork
$ cd firstproj
$ python manage.py runserver
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
This is the error I get.
I have looked online for a solution to my problem. It seems to be a common problem, dealing with the PATH. But, I cannot figure out how to fix it. Any help would be much appreciated thanks!
This is an example of what my working terminal looks like: (but if I exit out I lose everything)
(my_new_env)$ cd Django
(my_new_env)$ cd djangowork
(my_new_env)$ cd firstproj
(my_new_env)$ ls
firstproj manage.py testdb
(my_new_env)$ python manage.py runserver
Validating models...
0 errors found
October 31, 2012 - 18:51:22
Django version 1.6.dev20121029143154, using settings 'firstproj.settings'
Development server is running at http://.../
Quit the server with CONTROL-C.
Notice how in the second case you have (my_new_env) before the prompt, but not in the first? That's because only in the second case have you run virtual env properly. Make sure you run source my_new_env/bin/activate before you run any python commands at the prompt. You should always have (my_new_env) before the prompt in order to make sure your environment is set up properly. You have to do this in every terminal window that you open.