python mac环境搭建及第一个django项目

原创
2017/06/20 17:02
阅读数 1.2K

python mac环境搭建及第一个django项目

最近有一个好友想用django写一个项目,顺便学习一下pyhon,问我如何配置环境。然而在环境配置的时候,还是遇到了各种各样的问题。总的来说,在mac(mac和linux差不多)上写一个django项目,其实就是两个条件

  1. 安装python

     brew install python
    
  2. 安装django

     pip install Django
    

但是说起来简单,也还是会遇到各种情况,因为每个电脑的环境比一样,还有一些细节,在这里我把我能想到的一般性问题在这里演示一遍。

该篇主要涉及到了 brew、python、pip、django的安装

为了能说明一些问题,我把电脑格式化重装了一遍(确保各种依赖都能在这里展示出来)

首先,mac是自带python的

hulinhuadeMacBook-Air:~ hulinhua$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

可以看出,自带python版本为2.X.X

不过这遍文章讲的主要还是python3,因为:

  • python2 的版本毕竟有点老
  • python3 的环境配置和python2 的配置是一样的

好了,废话不多说,开始安装python3

1、安装python3

输入python3的安装指令 brew install python3

 hulinhuadeMacBook-Air:~ hulinhua$ brew install python3
 -bash: brew: command not found

会发现brew: command not found,brew 这个没有

brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便

brew类似ubuntu系统下的apt-get的功能

  • 安装 Homebrew
  1. 终端输入:curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1

     hulinhuadeMacBook-Air:~ hulinhua$ curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1
     ...        //这里省略了一大堆
     x share/doc/homebrew/brew.1.html
     hulinhuadeMacBook-Air:~ hulinhua$ brew
     Please run brew update!
    
  2. 安装完成后输入brew,提示,证明安装成功了,但是他要你update

     Please run brew update!
    

    输入 brew update

     hulinhuadeMacBook-Air:~ hulinhua$ brew update
     Error: /usr/local must be writable!
    
  3. 按照提示赋给/usr/local目录权限, 执行sudo chown -R $(whoami) /usr/local,即

    sudo chown -R fxp /usr/local

    //注意:其中fxp为系统当前用户名。

  4. 再次执行

    brew update

终端提示(把前面的提示也放在这里了,这里是整个安装homebrew)

hulinhuadeMacBook-Air:~ hulinhua$ curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1
...//这里省略了一大堆
x share/doc/homebrew/brew.1.html
hulinhuadeMacBook-Air:~ hulinhua$ brew
Please run brew update!
hulinhuadeMacBook-Air:~ hulinhua$ brew update
Error: /usr/local must be writable!
hulinhuadeMacBook-Air:~ hulinhua$ sudo chown -R hulinhua /usr/local
hulinhuadeMacBook-Air:~ hulinhua$ brew update
Initialized empty Git repository in /usr/local/.git/
... //这里也省略了一大堆
Updating Homebrew...
Error: Could not link:
/usr/local/share/doc/homebrew

Please delete these paths and run `brew update`.	
  1. 按照提示删除/usr/local/share/doc/homebrew,执行

     rm -r -f /usr/local/share/doc/homebrew
    
  2. 再次执行 brew update

     hulinhuadeMacBook-Air:~ hulinhua$ rm -r -f /usr/local/share/doc/homebrew
     hulinhuadeMacBook-Air:~ hulinhua$ brew update
     Already up-to-date.
     hulinhuadeMacBook-Air:~ hulinhua$ 
    

说了这么一大坨,终于把Homebrew安装完成了,抱歉啊!

  • 安装 python3

    输入

      brew install python3
    

    等的时间还有点长,嘿嘿

      hulinhuadeMacBook-Air:~ hulinhua$ brew install python3
      ==> Installing dependencies for python3: sqlite, gdbm, openssl, xz
      ==> Installing python3 dependency: sqlite
      ==> Downloading https://homebrew.bintray.com/bottles/		sqlite-3.19.3.sierra.bottle.tar.gz
      ... //省略了一大堆输出log
      ######################################################################## 100.0%
      ==> /usr/local/Cellar/python3/3.6.1/bin/python3 -s setup.py --no-user-cfg install --force --verbose 
      ==> /usr/local/Cellar/python3/3.6.1/bin/python3 -s setup.py --no-user-cfg install --force --verbose 
      ==> /usr/local/Cellar/python3/3.6.1/bin/python3 -s setup.py --no-user-cfg install --force --verbose 
      ==> Caveats
      Pip, setuptools, and wheel have been installed. To update them
        pip3 install --upgrade pip setuptools wheel
    
      You can install Python packages with
        pip3 install <package>
    
      They will install into the site-package directory
        /usr/local/lib/python3.6/site-packages
    
      See: http://docs.brew.sh/Homebrew-and-Python.html
      ==> Summary
      🍺  /usr/local/Cellar/python3/3.6.1: 7,961 files, 110.0MB, built in 11 minutes 37 seconds
    

    好了,到这里python算是安装完了,你可以开始你的python之旅了~(顺便说一下,python3是自带pip3的,上面有提示,要是不懂这句话的也没关系,下面还会介绍)

    对咯,还要验证一下有没有成功

      hulinhuadeMacBook-Air:~ hulinhua$ python
      Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
      [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> exit()
      hulinhuadeMacBook-Air:~ hulinhua$ python3
      Python 3.6.1 (default, Jun 20 2017, 16:18:15) 
      [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> exit()
      hulinhuadeMacBook-Air:~ hulinhua$ 
    

    成功了,下面再说一下django的安装,顺便用django说明一下python的模块的安装,其他的类似

  • 安装 Django

      pip install Django
    

    提示

      hulinhuadeMacBook-Air:~ hulinhua$ pip install Django
      -bash: pip: command not found
      hulinhuadeMacBook-Air:~ hulinhua$ 
    

    解释一下:pip是一个Python包管理工具,主要是用于安装 PyPI 上的软件包,可以替代 easy_install 工具。

    可以看出来,pip这个指令没有,不过上面说了,pyhon3是自带pip3的,由于python可以2.x.x和3.x.x版本共存,如果pip 安装了一个模块,就不知道这个模块到底安装到那个版本里面去了,所以pip3就是python3环境下的pip啦

      hulinhuadeMacBook-Air:~ hulinhua$ pip
      -bash: pip: command not found
      hulinhuadeMacBook-Air:~ hulinhua$ pip3
    
      Usage:   
        pip <command> [options]
        //这里省略了一大堆
        --disable-pip-version-check
                                    Don't periodically check PyPI to determine whether a new version of
                                    pip is available for download. Implied with --no-index.
      hulinhuadeMacBook-Air:~ hulinhua$ 
    

    可以看的出来pip3 这个指令确实是存在的,pip指令找不到

    1. pip 安装

       sudo easy_install pip
      

      提示

       hulinhuadeMacBook-Air:~ hulinhua$ sudo easy_install pip
       ...//此处省略一大堆
       Installed /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
       Processing dependencies for pip
       Finished processing dependencies for pip
      
    2. 安装django

      python2中

       pip install Django
      

      提示

       hulinhuadeMacBook-Air:~ hulinhua$ pip install Django
       Collecting Django
         Using cached Django-1.11.2-py2.py3-none-any.whl
       Requirement already satisfied: pytz in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from Django)
       Installing collected packages: Django
       Exception:
       Traceback (most recent call last):
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
           status = self.run(options, args)
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
           prefix=options.prefix_path,
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
           **kwargs
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
           self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
           isolated=self.isolated,
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 345, in move_wheel_files
           clobber(source, lib_dir, True)
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 316, in clobber
           ensure_dir(destdir)
         File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 83, in ensure_dir
           os.makedirs(path)
         File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
           mkdir(name, mode)
       OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django'
      

    失败了!!,从提示看是权限问题。好吧,

      	sudo pip install Django	
    

    从提示上看的出,这次pip 安装django是成功了

      hulinhuadeMacBook-Air:~ hulinhua$ sudo pip install Django
      Password:
      The directory '/Users/hulinhua/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
      The directory '/Users/hulinhua/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
      Collecting Django
        Downloading Django-1.11.2-py2.py3-none-any.whl (6.9MB)
          100% |████████████████████████████████| 7.0MB 21kB/s 
      Requirement already satisfied: pytz in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from Django)
      Installing collected packages: Django
      Successfully installed Django-1.11.2		
    

    python3中安装django

      pip3 install Django
    

    在python3中,直接pip3 install Django是成功了,竟然没需要sudo

    最后验证一下:

      hulinhuadeMacBook-Air:~ hulinhua$ python
      Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
      [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import django
      >>> eixt()
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      NameError: name 'eixt' is not defined
      >>> exit()
      hulinhuadeMacBook-Air:~ hulinhua$ python3
      Python 3.6.1 (default, Jun 20 2017, 16:18:15) 
      [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import django
      >>> 
    

    👌 python2 python3 和对应的django都搞定了

最后提及一下,由于python可以同时存在不同的版本,有的项目python3的有的项目又是python2的,如何去控制版本呢?鉴于篇幅问题,这里不做过多叙述,大家可以看看这遍blog,virtualenv 使用

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部