Django runserver error when specifying port
Solution 1
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.
Solution 2
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
Solution 3
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
Related videos on Youtube
Darwin Tech
Updated on September 16, 2022Comments
-
Darwin Tech over 1 year
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.
-
Eli over 11 yearsI don't know exactly why that doesn't work. Does it work if you run
./manage.py runserver 0.0.0.0:8000
(i.e. on a port >1024 without sudo) -
Darwin Tech over 11 yearsI get the same error on system, and the following error in virtualenv:
Error: You don't have permission to access that port.
-
Thierry Lam over 11 yearsDoes it work if you explicitly call it with python:
python manage.py runserver 0.0.0.0:8000
-
Darwin Tech over 11 yearsThat works! Thankyou. Any explanation as to why
sudo
and./manage.py
don't work? -
Vaisakh Rajagopal almost 8 years@darwin-tech Sudo is for Global. If you try
sudo
inside virtualenv it won't consider the python packages you installed locally(pip install
) to that virtualenv. Instead,sudo
will check for python package that are installed globally. From the error, we can understand that you didn't install Django globally. Remove thesudo
, and it will work fine. Don't use port 80; try 8000 or 8080.(your_env)****@****$ **python manage.py runserver 8000**
-
-
Rishabh Agrahari about 7 yearsNote that using sudo will NOT use your local environment's python, instead it will use your system's default python (you may miss your required packages)
-
Hans Zimermann over 5 yearsExecute that comand from inside the django project folde, where
manage.py
resides -
CodeSpent about 5 yearsNot to grave dig, but this is seldom a "solution" as its bad for user experience. Use this only if its a tool solely for yourself. Asking users to specify the port number is a bad idea.