Python Django 7 小时课程
在油管上找到一个Python Django课程,视频地址为:Python Django 7 Hour Course
作者为:TraversyMedia
配套Github仓库源代码地址为:https://github.com/Traversy-Media/StudyBud
StudyBud
StudyBud is a Django-based web application where users can create and join study rooms on various topics, engage in discussions, and collaborate with other learners.
This project was built as part of a comprehensive 7-hour video tutorial: Watch the full tutorial here
Setup Instructions
1. Clone the repository
bash
git clone https://github.com/divanov11/StudyBud.git
cd StudyBud
2. Create and activate a virtual environment
bash
# Install virtualenv if you haven't already
pip install virtualenv
# Create a virtual environment
virtualenv venv
# Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
3. Install dependencies
bash
pip install -r requirements.txt
这一步会读取项目根目录下的 requirements.txt 文件,并自动安装其中列出的所有 Python 依赖包。pip 会按照文件中的版本约束逐一解析并安装,通常需要几十秒到几分钟不等,具体取决于你的网络速度和依赖包的数量。
安装完成后,你可以通过以下命令验证所有依赖是否都已正确安装:
bash
pip list
该命令会列出当前虚拟环境中已安装的所有包及其版本号,你可以对照 requirements.txt 中的内容逐一核对。
提示 :如果你使用的是较新的 Python 版本(如 3.13+),可能会遇到部分依赖版本过旧导致的安装报错,此时需要参考下文「适配新版 Python」一节,对
requirements.txt中的版本号进行升级调整。
4. Run the development server
bash
python manage.py runserver
The application will be available at http://127.0.0.1:8000/
由于目前的Python版本比较新的,比如我的Mac Mini4下使用的Python版本是:Python 3.13.3,上述依赖版本太低,之前的依赖文件requirements.txt如下:
bash
asgiref==3.4.1
autopep8==1.5.7
Django==3.2.7
django-cors-headers==3.8.0
djangorestframework==3.12.4
Pillow==8.3.2
pycodestyle==2.7.0
pytz==2021.1
sqlparse==0.4.2
toml==0.10.2
使用Python 3.13.3 安装上述依赖会报错,报错如下:
bash
.venv) 192:StudyBud john$ pip install virtualenv
Collecting virtualenv
Downloading virtualenv-21.14.7-py3-none-any.whl.metadata (4.1 kB)
Collecting distlib<1,>=0.4.3 (from virtualenv)
Downloading distlib-0.4.3-py2.py3-none-any.whl.metadata (5.3 kB)
Collecting filelock<5,>=3.24.2 (from virtualenv)
Downloading filelock-4.2.0-py3-none-any.whl.metadata (2.0 kB)
Requirement already satisfied: packaging>=26.3 in /Users/john/WorkSpace/PythonProjects/.venv/lib/python3.13/site-packages (from virtualenv) (26.3)
Collecting platformdirs<5,>=4.4 (from virtualenv)
Downloading platformdirs-4.13.0-py3-none-any.whl.metadata (5.5 kB)
Collecting python-discovery>=1.6.1 (from virtualenv)
Downloading python_discovery-1.6.2-py3-none-any.whl.metadata (5.1 kB)
Downloading virtualenv-21.14.7-py3-none-any.whl (5.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.5/5.5 MB 4.6 MB/s 0:00:01
Downloading distlib-0.4.3-py2.py3-none-any.whl (470 kB)
Downloading filelock-4.2.0-py3-none-any.whl (135 kB)
Downloading platformdirs-4.13.0-py3-none-any.whl (32 kB)
Downloading python_discovery-1.6.2-py3-none-any.whl (38 kB)
Installing collected packages: distlib, platformdirs, filelock, python-discovery, virtualenv
Successfully installed distlib-0.4.3 filelock-4.2.0 platformdirs-4.13.0 python-discovery-1.6.2 virtualenv-21.14.7
(.venv) 192:StudyBud john$ virtualenv venv
created virtual environment CPython3.13.3.final.0-64-arm64 in 309ms
creator CPython3macOsBrew(dest=/Users/john/WorkSpace/PythonProjects/StudyBud/venv, clear=False, no_vcs_ignore=False, venv_redirect=None, global=False)
seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=/Users/john/Library/Caches/virtualenv)
added seed packages: pip==26.2.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator,XonshActivator
(.venv) 192:StudyBud john$ source venv/bin/activate
(venv) 192:StudyBud john$ pip install -r requirements.txt
Collecting asgiref==3.4.1 (from -r requirements.txt (line 1))
Downloading asgiref-3.4.1-py3-none-any.whl.metadata (9.2 kB)
Collecting autopep8==1.5.7 (from -r requirements.txt (line 2))
Downloading autopep8-1.5.7-py2.py3-none-any.whl.metadata (16 kB)
Collecting Django==3.2.7 (from -r requirements.txt (line 3))
Downloading Django-3.2.7-py3-none-any.whl.metadata (3.9 kB)
Collecting django-cors-headers==3.8.0 (from -r requirements.txt (line 4))
Downloading django_cors_headers-3.8.0-py3-none-any.whl.metadata (15 kB)
Collecting djangorestframework==3.12.4 (from -r requirements.txt (line 5))
Downloading djangorestframework-3.12.4-py3-none-any.whl.metadata (9.9 kB)
Collecting Pillow==8.3.2 (from -r requirements.txt (line 6))
Downloading Pillow-8.3.2.tar.gz (48.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 48.8/48.8 MB 6.4 MB/s 0:00:08
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [24 lines of output]
Traceback (most recent call last):
File "/Users/john/WorkSpace/PythonProjects/StudyBud/venv/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 389, in <module>
main()
~~~~^^
File "/Users/john/WorkSpace/PythonProjects/StudyBud/venv/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 373, in main
json_out["return_val"] = hook(**hook_input["kwargs"])
~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/john/WorkSpace/PythonProjects/StudyBud/venv/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 143, in get_requires_for_build_wheel
return hook(config_settings)
File "/private/var/folders/tj/tz3pyb1j3535l6_48_zqg3v40000gn/T/pip-build-env-bqbewnkr/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 333, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=[])
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/private/var/folders/tj/tz3pyb1j3535l6_48_zqg3v40000gn/T/pip-build-env-bqbewnkr/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 301, in _get_build_requires
self.run_setup()
~~~~~~~~~~~~~~^^
File "/private/var/folders/tj/tz3pyb1j3535l6_48_zqg3v40000gn/T/pip-build-env-bqbewnkr/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 520, in run_setup
super().run_setup(setup_script=setup_script)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/private/var/folders/tj/tz3pyb1j3535l6_48_zqg3v40000gn/T/pip-build-env-bqbewnkr/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 317, in run_setup
exec(code, locals()) # noqa: S102 # exec is intentional here
~~~~^^^^^^^^^^^^^^^^
File "<string>", line 30, in <module>
File "<string>", line 26, in get_version
KeyError: '__version__'
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed to build 'Pillow' when getting requirements to build wheel
使用OpenCode分析错误,AI建议我改成如下的requirements.txt:
bash
Django~=5.2
djangorestframework~=3.16
django-cors-headers~=4.8
Pillow~=11.0
autopep8~=2.3
pycodestyle~=2.14
修改之后再执行
bash
pip install -r requirements.txt
就能正常安装项目依赖
然后再执行
bash
python manage.py runserver
就可以顺利运行项目了。