MacOS下已安装Django,但python -m django --version报错找不到模块
python -m django --version on macOS Hey Dave, let's break down why you're seeing this mismatch and fix it quickly:
The Root Cause
On macOS, the default python command usually points to the system's built-in Python 2.x version, while your Django is installed under Python 3.6 (as shown in the error path: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3). That's why pip freeze and django-admin.py version work (they're linked to your Python 3.6 environment) but python -m django --version doesn't—it's checking the wrong Python installation.
Step-by-Step Fixes
Use the correct Python executable explicitly
Instead ofpython, run the command withpython3to target your Python 3.6 environment:python3 -m django --versionThis should immediately show your Django 1.11.20 version, since
python3maps to the same Python installation where pip installed Django.Verify the pip-Python connection
Double-check that yourpipis associated with Python 3.6 by running:pip --versionThe output should show a path pointing to
/Library/Frameworks/Python.framework/Versions/3.6/...—this confirms pip is installing packages to the right Python environment.Make
pythondefault to Python 3.6 (optional)
If you want to avoid typingpython3every time, you can set an alias in your shell configuration:- Open your shell config file (for Bash:
~/.bash_profile; for Zsh:~/.zshrc) in a text editor. - Add this line:
alias python='/Library/Frameworks/Python.framework/Versions/3.6/bin/python3' - Save the file and reload the config with:
source ~/.bash_profile # or source ~/.zshrc for Zsh
Now running
python -m django --versionwill use your Python 3.6 environment and find Django.- Open your shell config file (for Bash:
Test with the full Python path
If the above doesn't work for some reason, use the full path to your Python 3.6 executable to guarantee it targets the right environment:/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m django --version
内容的提问来源于stack exchange,提问作者DaveMS

