在MacOS中部署Python虚拟开发环境

部署虚拟开发环境

虽然自己不是软件工程师,但有时候为了测试会写一些小程序,所以常常需要在自己电脑上设置开发环境。由于MacOS原生支持Python,所以我们可以不用那么麻烦的设置开发环境。但为了系统的稳定性,还是不要随便增加或变更自带的python,所以开发环境直接用虚拟环境就可以了,大概构架如下:



1. 安装homebrew,非常实用的软件管理工具,在MacOS上可以方便的安装软件,如python等等,安装方式直接一条命令搞定,参考官网就可以,简单易用,而且所有安装的软件都在自己的文件夹下,不会跟系统自带的冲突;另外用“sudo easy_install pip”来快速安装pip,这个是python的软件包管理工具

2. 用homebrew安装一个独立的python

xxx-M-L2HE:~ xxx$ brew info python@2   <<< 确认当前版本信息
xxx-M-L2HE:~ xxx$ brew install python@2   <<< 安装2.7版本
xxx-M-L2HE:~ xxx$ brew list
gdbm		openssl		python@2	readline	sqlite
xxx-M-L2HE:~ xxx$ cd /usr/local/Cellar/
xxx-M-L2HE:Cellar xxx$ ls -l
total 0
drwxr-xr-x  3 xxx  admin   96 May 25 23:56 gdbm
drwxr-xr-x  3 xxx  admin   96 May 25 23:56 openssl
drwxr-xr-x  4 xxx  admin  128 May 26 00:03 python@2
drwxr-xr-x  3 xxx  admin   96 May 25 23:57 readline
drwxr-xr-x  3 xxx  admin   96 May 25 23:57 sqlite
xxx-M-L2HE:Cellar xxx$ echo $PATH    <<< 可以看到下面路径中没有homebrew的路径,所以默认都是系统的版本
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public

3. 用pip安装“virtualenvwrapper”

xxx-M-L2HE:Cellar xxx$ pip install virtualenvwrapper
Collecting virtualenvwrapper
  Downloading https://files.pythonhosted.org/packages/2b/8c/3192e10913ad945c0f0fcb17e9b2679434a28ad58ee31ce0104cba3b1154/virtualenvwrapper-4.8.2-py2.py3-none-any.whl
Collecting virtualenv (from virtualenvwrapper)
  Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB)
    100% |████████████████████████████████| 1.9MB 373kB/s 
Collecting stevedore (from virtualenvwrapper)
  Downloading https://files.pythonhosted.org/packages/17/6b/3b7d6d08b2ab3e5ef09e01c9f7b3b590ee135f289bb94553419e40922c25/stevedore-1.28.0-py2.py3-none-any.whl
Collecting virtualenv-clone (from virtualenvwrapper)
  Downloading https://files.pythonhosted.org/packages/6d/c2/dccb5ccf599e0c5d1eea6acbd058af7a71384f9740179db67a9182a24798/virtualenv_clone-0.3.0-py2.py3-none-any.whl
Collecting pbr!=2.1.0,>=2.0.0 (from stevedore->virtualenvwrapper)
  Downloading https://files.pythonhosted.org/packages/2d/9d/7bfab757977067556c7ca5fe437f28e8b8843c95564fca504de79df63b25/pbr-4.0.3-py2.py3-none-any.whl (98kB)
    100% |████████████████████████████████| 102kB 576kB/s 
Collecting six>=1.10.0 (from stevedore->virtualenvwrapper)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: virtualenv, pbr, six, stevedore, virtualenv-clone, virtualenvwrapper
Successfully installed pbr-4.0.3 six-1.11.0 stevedore-1.28.0 virtualenv-16.0.0 virtualenv-clone-0.3.0 virtualenvwrapper-4.8.2

4. 设置虚拟环境,link相应版本的python并激活虚拟环境

xxx-M-L2HE:Cellar xxx$ cd ~
xxx-M-L2HE:~ xxx$ cd Documents/
xxx-M-L2HE:Documents xxx$ mkdir venv
xxx-M-L2HE:Documents xxx$ cd venv
xxx-M-L2HE:venv xxx$ mkdir py2715
xxx-M-L2HE:venv xxx$ ls
py2715
xxx-M-L2HE:venv xxx$ 
xxx-M-L2HE:venv xxx$ virtualenv --python=/usr/local/Cellar/python@2/2.7.15/bin/python py2715
Running virtualenv with interpreter /usr/local/Cellar/python@2/2.7.15/bin/python
New python executable in /Users/xxx/Documents/venv/py2715/bin/python2.7
Also creating executable in /Users/xxx/Documents/venv/py2715/bin/python
Installing setuptools, pip, wheel...done.
xxx-M-L2HE:venv xxx$ 
xxx-M-L2HE:venv xxx$ cd py2715/
xxx-M-L2HE:py2715 xxx$ source bin/activate
(py2715) xxx-M-L2HE:py2715 xxx$ 
(py2715) xxx-M-L2HE:py2715 xxx$ 
(py2715) xxx-M-L2HE:py2715 xxx$ python -V
Python 2.7.15

5. 安装虚拟环境中的pip,退出虚拟环境

(py2715) xxx-M-L2HE:py2715 xxx$ sudo easy_install pip
Password:
Searching for pip
Best match: pip 10.0.1
Adding pip 10.0.1 to easy-install.pth file
Installing pip script to /Users/xxx/Documents/venv/py2715/bin
Installing pip3.6 script to /Users/xxx/Documents/venv/py2715/bin
Installing pip3 script to /Users/xxx/Documents/venv/py2715/bin

Using /Users/xxx/Documents/venv/py2715/lib/python2.7/site-packages
Processing dependencies for pip
Finished processing dependencies for pip
(py2715) xxx-M-L2HE:py2715 xxx$ 
(py2715) xxx-M-L2HE:py2715 xxx$ deactivate 
xxx-M-L2HE:py2715 xxx$ 

安装Python IDE PyCharm

非常棒的python编译器,社区免费版本就满足我的需求了,直接去官网下载安装就可以了。当建立project后,pycharm会自动建立虚拟环境,我们可以不用他的虚拟环境,所以直接删掉自动建立的venv文件夹,那么如何调用我们之前建立好的虚拟环境呢,如下所示,调用上面设置好的虚拟环境

Preferences -> Project Interpreter -> add -> Existing Environment -> python file of venv

设置好后,进入pycharm的terminal后,可以看到虚拟环境表示,如下:

好了,现在可以愉快地写小程序了~另外也可以愉快地用pip安装各种python包折腾了~:)

安装Git

xxx-M-L2HE:~ xxx$ brew install git
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
ii                                       rsstail

==> Downloading https://homebrew.bintray.com/bottles/git-2.17.0.high_sierra.bott
######################################################################## 100.0%
==> Pouring git-2.17.0.high_sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
  /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git
==> Summary
?  /usr/local/Cellar/git/2.17.0: 1,497 files, 35.6MB
xxx-M-L2HE:~ xxx$ cd Documents/
xxx-M-L2HE:Documents xxx$ cd PycharmProjects/
xxx-M-L2HE:PycharmProjects xxx$ git clone https://xxxxxxxx.git
Cloning into 'xxxxxxx'...
remote: Counting objects: 56, done.
remote: Compressing objects: 100% (54/54), done.
remote: Total 56 (delta 13), reused 0 (delta 0)
Unpacking objects: 100% (56/56), done.
0
你可以留言,或者trackback 从你的网站

留言哦