You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 18.04中Django runserver启动失败问题求助

Why Django Isn't Found in Your Python 3.5 Environment (Ubuntu 18.04)

This is a common issue caused by a version mismatch between the pip3 you used to install Django and the python3 you're running, or Python not looking in the directory where Django was installed. Let's break down how to diagnose and fix it.

First, Diagnose the Root Cause

Step 1: Check Your Python/Pip Version Mismatch

Run these commands to see which versions of Python and pip you're working with:

python3 --version
pip3 --version

Look at the output of pip3 --version—it will tell you which Python version it's linked to (e.g., pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)). If this Python version doesn't match your python3 --version (3.5.2), that's the problem! Your pip3 installed Django for Python 3.6, but you're trying to run it with Python 3.5.

Step 2: Find Where Django is Installed

Run this to get the exact path of your Django installation:

pip3 show django

Note the Location field (e.g., /home/shobhit/.local/lib/python3.6/site-packages). This is where Django lives.

Step 3: Verify Python's Search Path

Check if Python 3.5 is looking in that directory:

python3 -c "import sys; print(sys.path)"

If the Location from Step 2 isn't listed here, Python 3.5 can't find Django.

Solutions to Fix the Issue

Solution 1: Install Django for Python 3.5 Directly

Since you're using Python 3.5, use the version-specific pip (pip3.5) to install Django:

  1. First, install pip3.5 if you don't have it:
sudo apt install python3.5-pip
  1. Install Django 2.0.5 for Python 3.5:
pip3.5 install django==2.0.5

Now, running python3 manage.py runserver should work, since Django is now in Python 3.5's site-packages.

Solution 2: Add Django's Directory to PYTHONPATH

If you don't want to reinstall Django, you can tell Python 3.5 where to find it:

  1. Use the Location path from Step 2 (e.g., /home/shobhit/.local/lib/python3.6/site-packages).
  2. Add it to your PYTHONPATH temporarily:
export PYTHONPATH="/home/shobhit/.local/lib/python3.6/site-packages:$PYTHONPATH"
  1. To make this permanent, add the line above to your ~/.bashrc or ~/.profile file, then run:
source ~/.bashrc

Now Python 3.5 will always look in that directory for packages.

Virtual environments eliminate version conflicts entirely by creating an isolated space for your project's dependencies:

  1. Install the virtual environment tool for Python 3.5:
sudo apt install python3.5-venv
  1. Navigate to your project directory and create a virtual environment:
cd /path/to/your/django-project
python3.5 -m venv myenv
  1. Activate the environment:
source myenv/bin/activate
  1. Install Django inside the virtual environment:
pip install django==2.0.5
  1. Now run your server—this will use the correct Python and Django versions:
python manage.py runserver

When you're done working, deactivate the environment with deactivate.

Quick Check for manage.py

If django-admin works but manage.py doesn't, double-check the first line of your manage.py file. It should point to the Python version that has Django installed, e.g.:

#!/usr/bin/env python3.5

内容的提问来源于stack exchange,提问作者ITSagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:06:37