杰瑞科技汇

mac上如何用Python搭建Django项目?

Of course! Setting up a Python Django project on a Mac is a very common and straightforward process. This guide will walk you through it step-by-step, from installation to running your first project.

mac上如何用Python搭建Django项目?-图1
(图片来源网络,侵删)

We'll cover two primary methods:

  1. The Modern Way (Recommended): Using pyenv to manage Python versions and pipenv for project dependencies. This is the best practice as it keeps your system clean and avoids version conflicts.
  2. The Simple Way: Using the system's built-in Python and pip. This is quicker but can lead to conflicts if you're not careful.

Method 1: The Modern & Recommended Way (Using pyenv and pipenv)

This approach gives you a dedicated, isolated Python environment for each project, which is a professional standard.

Step 1: Install Xcode Command Line Tools

This installs essential compilers and tools (like git and make) that pyenv needs.

xcode-select --install

Step 2: Install Homebrew

Homebrew is the most popular package manager for macOS. If you don't have it, open your Terminal and run:

mac上如何用Python搭建Django项目?-图2
(图片来源网络,侵删)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 3: Install pyenv

pyenv lets you install and switch between multiple Python versions.

brew install pyenv

Step 4: Configure Your Shell for pyenv

You need to add pyenv to your shell's path. First, find out which shell you are using:

echo $SHELL
  • If it says /bin/zsh (most common on modern macOS), run this:
    echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
  • If it says /bin/bash, run this:
    echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile
    echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

Restart your Terminal completely for the changes to take effect. You can also run source ~/.zshrc or source ~/.bash_profile.

Step 5: Install a Python Version with pyenv

Let's install a recent, stable version of Python (e.g., 3.11.5).

mac上如何用Python搭建Django项目?-图3
(图片来源网络,侵删)
pyenv install 3.11.5

You can see all available versions with pyenv install --list.

Step 6: Set the Global Python Version (Optional)

This sets the default Python version for your user account.

pyenv global 3.11.5

Verify the installation:

python --version
# Should show: Python 3.11.5

Step 7: Install pipenv

pipenv is a dependency manager that automatically creates and manages virtual environments for you.

pip install pipenv

Step 8: Create Your Django Project

Navigate to where you want to create your project (e.g., your home directory or a Projects folder).

# Navigate to your desired directory
cd ~
# Create a new directory for your project
mkdir my-django-project
cd my-django-project
# Create the Django project using pipenv
# This will automatically create a virtual environment
pipenv install django

Step 9: Start the Django Project

Now, run the Django start-up command inside the pipenv environment.

pipenv run django-admin startproject myproject .
  • pipenv run executes the command within the project's virtual environment.
  • django-admin startproject myproject . creates the project files. The at the end creates them in the current directory, which is a best practice.

Your directory structure will now look like this:

my-django-project/
├── Pipfile          # Defines project dependencies
├── Pipfile.lock     # Locks dependency versions
└── myproject/       # The actual Django project
    ├── manage.py
    └── myproject/
        ├── __init__.py
        ├── asgi.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

Step 10: Run the Development Server

Navigate into the myproject directory and run the server.

cd myproject
pipenv run python manage.py runserver

You should see output like this:

Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 10, 2025 - 15:30:00
Django version 4.2.6, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open your web browser and go to http://127.0.0.1:8000/. You'll see the Django welcome page!


Method 2: The Simple Way (Using System Python & pip)

This method is faster but can lead to "dependency hell" if you start managing multiple projects. Use it for quick experiments.

Step 1: Verify Python 3

Modern macOS comes with Python 3 pre-installed. Check its version:

python3 --version

If it's an older version (e.g., 3.8 or below), it's highly recommended to use Method 1. If you have a recent version (3.9+), you can proceed.

Step 2: Install Django

Use pip3 (the package manager for Python 3) to install Django.

pip3 install django

Step 3: Create Your Django Project

Navigate to your desired directory and run the startproject command.

cd ~
mkdir my-simple-project
cd my-simple-project
# Use django-admin to create the project
django-admin startproject myproject .

Your directory structure will be the same as in Method 1.

Step 4: Run the Development Server

Navigate into the myproject directory and run the server.

cd myproject
python3 manage.py runserver

Open your browser to http://127.0.0.1:8000/ to see the welcome page.


Common Commands & Next Steps

Here are some essential Django commands you'll use often:

  • Create a new app (a module within your project):

    # From the myproject/ directory
    python manage.py startapp blog
  • Make database migrations (when you create/change models):

    python manage.py makemigrations
  • Apply the migrations to the database:

    python manage.py migrate
  • Create a superuser to access the admin panel:

    python manage.py createsuperuser

    Follow the prompts to create an admin username and password.

  • To stop the server: In your terminal, press Ctrl + C.

Choosing the Right Method

Feature Method 1 (pyenv + pipenv) Method 2 (System Python + pip)
Best For Serious development, multiple projects, sharing code. Quick tests, simple scripts, one-off projects.
Python Version Full control and isolation. Tied to your system's version (can be outdated).
Dependencies Isolated per project. No conflicts. Global install. Risk of version conflicts.
Complexity Slightly more involved initial setup. Very quick and easy setup.
Recommendation Highly Recommended for all projects. Use with caution or for very simple tasks.

For any serious work on a Mac, Method 1 is the way to go. It will save you countless headaches in the long run. Happy coding

分享:
扫描分享到社交APP
上一篇
下一篇