从SCM下载的Django项目无manage.py,如何本地运行?
manage.py Got it, let's work through how to get this Django project up and running even without the usual manage.py file. First, let's make sure we're actually dealing with a valid Django project—check for these key files/directories in your downloaded code:
- A
settings.pyfile (or a folder containing multiple settings modules) - A root
urls.pyfile (the main URL configuration) - A
requirements.txtorpyproject.tomlthat lists Django as a dependency - App folders with files like
models.py,views.py, oradmin.py
Option 1: Use django-admin with manual settings configuration
manage.py is just a convenience wrapper around the django-admin tool that automatically points to your project's settings. If it's missing, you can replicate this manually:
Install dependencies first (if you haven't already):
pip install -r requirements.txtIf there's no
requirements.txt, just install Django directly:pip install djangoFind your settings module path:
Locate wheresettings.pylives. For example, if it's inside a folder namedmy_project(path:my_project/settings.py), your settings module ismy_project.settings.Set the
DJANGO_SETTINGS_MODULEenvironment variable:- On Linux/macOS:
export DJANGO_SETTINGS_MODULE=my_project.settings - On Windows (Command Prompt):
set DJANGO_SETTINGS_MODULE=my_project.settings - On Windows (PowerShell):
$env:DJANGO_SETTINGS_MODULE = "my_project.settings"
- On Linux/macOS:
Start the development server:
django-admin runserver
Option 2: Create your own manage.py file
You can easily generate a custom manage.py—it's just a short script. Create a file named manage.py in your project root with this content:
#!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise 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?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': main()
Replace 'my_project.settings' with your actual settings module path, then run:
python manage.py runserver
Option 3: If you downloaded a standalone Django app (not a full project)
Sometimes you might get just a single Django app instead of a complete project. Here's how to integrate it:
Create a new base Django project:
django-admin startproject my_siteCopy your downloaded app folder into the
my_sitedirectory (same level as the newmanage.py).Open
my_site/settings.pyand add your app's name to theINSTALLED_APPSlist:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'your_app_name', # Add your app here ]Launch the server:
python manage.py runserver
内容的提问来源于stack exchange,提问作者Manohar

