Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django runserver error when specifying port

Tags:

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.

like image 552
Darwin Tech Avatar asked Sep 20 '12 20:09

Darwin Tech


People also ask

How do I change the Runserver port in Django?

Inside the commands folder open up the runserver.py script with a text editor. Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like DEFAULT_PORT = "8080"

How do I stop 8000 port?

Just type sudo fuser -k 8000/tcp. This should kill all the processes associated with port 8000.

What port does Django use?

By default, the server runs on port 8000 on the IP address 127.0. 0.1 .


3 Answers

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.

like image 133
Paulo Scardine Avatar answered Oct 21 '22 16:10

Paulo Scardine


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
like image 37
xyres Avatar answered Oct 21 '22 18:10

xyres


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
like image 33
Pablo Guerrero Avatar answered Oct 21 '22 18:10

Pablo Guerrero