Flask Web开发-Mac开发环境搭建

1. 使用虚拟环境

安装 Flask 最便捷的方式是使用虚拟环境。虚拟环境是 Python 解释器的一个私有副本,在这个环境中你可以安装私有包,而且不会影响系统中安装的全局 Python 解释器。

虚拟环境非常有用,可以在系统的 Python 解释器中避免包的混乱和版本的冲突。为每个程序单独创建虚拟环境可以保证程序只能访问虚拟环境中的包,从而保持全局解释器的干净整洁,使其只作为创建(更多)虚拟环境的源。使用虚拟环境还有个好处,那就是不需要管理员权限。

虚拟环境使用第三方实用工具 virtualenv 创建。输入以下命令可以检查系统是否安装了virtualenv:

 $ virtualenv --version

如果结果显示错误,你就需要安装这个工具。

1.1Mac下安装virtualenv

image

1.2使用virtualenv创建python虚拟环境

image

现在,flasky 文件夹中就有了一个名为 venv 的子文件夹,它保存一个全新的虚拟环境,其中有一个私有的 Python 解释器。

1.3激活虚拟环境

在使用这个虚拟环境之前,你需要先将其“激活”。如果你使用 bash 命令行(Linux 和 Mac OS X 用户),可以通过下面的命令激活这个虚拟环境:

 $ source venv/bin/activate

虚拟环境被激活后,其中 Python 解释器的路径就被添加进 PATH 中,但这种改变不是永久性的,它只会影响当前的命令行会话。为了提醒你已经激活了虚拟环境,激活虚拟环境的命令会修改命令行提示符,加入环境名:

(venv) $

当虚拟环境中的工作完成后,如果你想回到全局 Python 解释器中,可以在命令行提示符下输入 deactivate。

2. 使用Pip安装Python包

2.1安装flask

(venv) $ pip install flask
Flask Web开发-Mac开发环境搭建_第1张图片
image

2.2验证flask正确安装

要想验证Flask是否正确安装,可以启动python编译器,尝试导入flask

image

3. 爬坑指南

3. 1安装virtualenv时遇到的报错,如下

siemenss-MacBook-Pro:~ cmt$ sudo easy_install virtualenv
Password:
Searching for virtualenv
Reading https://pypi.python.org/simple/virtualenv/
Download error on https://pypi.python.org/simple/virtualenv/: ('_ssl.c:574: The handshake operation timed out',) -- Some packages may not be found!
Couldn't find index page for 'virtualenv' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
No local packages or download links found for virtualenv
error: Could not find suitable distribution for Requirement.parse('virtualenv')
siemenss-MacBook-Pro:~ cmt$ 
  • 解决方法1:加上信任站点(NOK)
siemenss-MacBook-Pro:~ cmt$ su root
Password:
sh-3.2# pip --trusted-host pypi.python.org install virtualenv
Collecting virtualenv
  Could not fetch URL https://pypi.python.org/simple/virtualenv/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
  Could not find a version that satisfies the requirement virtualenv (from versions: )
No matching distribution found for virtualenv
  • 解决方法2: 升级pip(OK)
sh-3.2# curl https://bootstrap.pypa.io/get-pip.py | python
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1604k  100 1604k    0     0   116k      0  0:00:13  0:00:13 --:--:--  310k
Collecting pip
  Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 867kB/s 
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/81/30/e935244ca6165187ae8be876b6316ae201b71485538ffac1d718843025a9/wheel-0.31.1-py2.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 1.3MB/s 
Installing collected packages: pip, wheel
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-18.0 wheel-0.31.1
sh-3.2# pip install virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB)
    100% |████████████████████████████████| 1.9MB 887kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-16.0.0

3. 2 通过virtualenv创建虚拟化环境时遇到的报错,如下:

sh-3.2# virtualenv venv
New python executable in /Users/cmt/venv/venv/bin/python
Installing setuptools, pip, wheel...
  Complete output from command /Users/cmt/venv/venv/bin/python - setuptools pip wheel:
  Looking in links: /Library/Python/2.7/site-packages, /Library/Python/2.7/site-packages/virtualenv_support, /Library/Python/2.7/site-packages/virtualenv_support
Collecting setuptools
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)': /packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)': /packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)': /packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)': /packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl
Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))

----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 11, in 
    sys.exit(main())
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 712, in main
    symlink=options.symlink)
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 947, in create_environment
    download=download,
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 904, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 796, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /Users/cmt/venv/venv/bin/python - setuptools pip wheel failed with error code 1

错误原因:'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)'。最终定位是网络的原因,切换网络后顺利解决。

你可能感兴趣的:(Flask Web开发-Mac开发环境搭建)