Mac中安装Python环境和Django以及运行第一个Python项目

1.安装Python环境

  • 更新brew源
brew upgrade
  • 安装Python3
brew install python3
  • 安装完成后会显示python的相关信息
==> python
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  pip3 install 
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
配置环境变量(因为mac是自带python2的环境的,默认调用的python解释器是系统自带的,所以我们要配置下python3的环境)
  • 编辑配置文件
vim ~/.bash_profile
  • 输入 i 进入编辑状态,加入下面的配置
alias python="/usr/local/bin/python3"
  • 按下 esc :wq 保存 我的是这样的
JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home'
export JAVA_HOME
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
#export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
alias python="/usr/local/bin/python3"
~                                                                                                                                                                             
~                                                                                                                                                                             
~                                                    
  • 使刚才的配置生效
source ~/.bash_profile
  • 查看当前使用的python版本
zhaowenwendeiMac-Pro:~ zhaowenwen$ python --version
Python 3.7.5
zhaowenwendeiMac-Pro:~ zhaowenwen$ 

2.安装Django

  • 下载django Mac压缩包

点击下载,或者可以去首页下载

  • 解压压缩包,并使用终端进入解压后的根目录
zhaowenwendeiMac-Pro:~ zhaowenwen$ cd /Users/zhaowenwen/Django-3.0 
zhaowenwendeiMac-Pro:Django-3.0 zhaowenwen$ ls
AUTHORS         Gruntfile.js        LICENSE.python      README.rst      extras          scripts         tests
CONTRIBUTING.rst    INSTALL         MANIFEST.in     django          js_tests        setup.cfg
Django.egg-info     LICENSE         PKG-INFO        docs            package.json        setup.py
zhaowenwendeiMac-Pro:Django-3.0 zhaowenwen$ 
这里需要说明一下,虽然 python --version显示的是3.7.5的版本,但是在使用python解释器的时候还是会默认指向2.7,安装django会报错,其实只要修改了软连接python的指向为我们安装的python路径即可,但是系统文件不让改,我也是醉了。好像要重新挂载分区。在挂载的时候授于读写权限就可以。所以我们调用的时候直接用python3替换python即可。
  • 安装django 注意用python3
#原命令sudo python setup.py install 改为sudo python3 setup.py install 即可
sudo python3 setup.py install
  • 等待安装成功,进入一个自己的工作空间并创建一个测试项目
zhaowenwendeiMac-Pro-2:Python3Project zhaowenwen$ cd /Users/zhaowenwen/Python3Project/
zhaowenwendeiMac-Pro-2:Python3Project zhaowenwen$ django-admin.py startproject testPythonPro
zhaowenwendeiMac-Pro-2:Python3Project zhaowenwen$ 
  • 运行项目
zhaowenwendeiMac-Pro-2:testPythonPro zhaowenwen$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

December 11, 2019 - 03:52:31
Django version 3.0, using settings 'testPythonPro.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  • 至此,你可以访问http://127.0.0.1:8000看到如下页面
    py_suc.png

安装成功

你可能感兴趣的:(Mac中安装Python环境和Django以及运行第一个Python项目)