原文地址:http://www.xuebuyuan.com/1157602.html
学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装,
比如这样超时的问题:
(env)user@orz:~/flask_study/venv-test/test$ easy_install Flask-SQLAlchemy
Searching for Flask-SQLAlchemy
Reading http://pypi.python.org/simple/Flask-SQLAlchemy/
Best match: Flask-SQLAlchemy 1.0
Downloading https://pypi.python.org/packages/source/F/Flask-SQLAlchemy/Flask-SQLAlchemy-1.0.tar.gz#md5=dca034849ffcbaef7b848e8728d3668f
Processing Flask-SQLAlchemy-1.0.tar.gz
Running Flask-SQLAlchemy-1.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-lQ6xtl/Flask-SQLAlchemy-1.0/egg-dist-tmp-ZojgvG
warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'docs/_themes/.git'
Adding Flask-SQLAlchemy 1.0 to easy-install.pth file
Installed /home/zt/flask_study/venv-test/test/env/lib/python2.7/site-packages/Flask_SQLAlchemy-1.0-py2.7.egg
Processing dependencies for Flask-SQLAlchemy
Searching for SQLAlchemy
Reading http://pypi.python.org/simple/SQLAlchemy/
Download error on http://pypi.python.org/simple/SQLAlchemy/: _ssl.c:489: The handshake operation timed out -- Some packages may not be found!
Reading http://pypi.python.org/simple/SQLAlchemy/
Best match: SQLAlchemy 0.8.2
Downloading https://pypi.python.org/packages/source/S/SQLAlchemy/SQLAlchemy-0.8.2.tar.gz#md5=5a33fb43dea93468dbb2a6562ee80b54
error: Download error for https://pypi.python.org/packages/source/S/SQLAlchemy/SQLAlchemy-0.8.2.tar.gz: _ssl.c:489: The handshake operation timed out
或者这种:
(env)user@orz:~/flask_study/venv-test/test$ pip install flask
Downloading/unpacking flask
Could not find any downloads that satisfy the requirement flask
No distributions at all found for flask
Storing complete log in /home/user/.pip/pip.log
或者:
Downloading/unpacking flask
Cannot fetch index base URL http://pypi.python.org/simple/
或者:
Downloading/unpacking Jinja2>=2.4 (from flask)
Error The read operation timed out while getting https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.1.tar.gz#md5=282aed153e69f970d6e76f78ed9d027a (from https://pypi.python.org/simple/Jinja2/)
Exception:
Traceback (most recent call last):
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 104, in main
status = self.run(options, args)
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/commands/install.py", line 245, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/req.py", line 985, in prepare_files
self.unpack_url(url, location, self.is_download)
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/req.py", line 1109, in unpack_url
retval = unpack_http_url(link, location, self.download_cache, self.download_dir)
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/download.py", line 431, in unpack_http_url
resp = _get_response_from_url(target_url, link)
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/download.py", line 466, in _get_response_from_url
resp = urlopen(target_url)
File "/home/user/flask_study/venv-test/test/env/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/download.py", line 85, in __call__
response = urllib2.urlopen(self.get_request(url))
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 401, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 419, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1219, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
r = h.getresponse(buffering=True)
File "/usr/lib/python2.7/httplib.py", line 1034, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 365, in _read_status
line = self.fp.readline()
File "/usr/lib/python2.7/socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
File "/usr/lib/python2.7/ssl.py", line 241, in recv
return self.read(buflen)
File "/usr/lib/python2.7/ssl.py", line 160, in read
return self._sslobj.read(len)
SSLError: The read operation timed out
Storing complete log in /home/user/.pip/pip.log
为什么总是失败?自己操作老标准了,这么简单的几个小步骤还老是出错,不由得让我怀疑是否是撞墙了,可是又懒得买去翻~~一墙,
无法代理又无法翻~~一墙,被逼着想到了使用镜像的方法了,一些公共的网站在国内总有一些镜像,使用这些镜像地址来安装就可以了
有了目的性搜索很快就搜索到了,
http://e.pypi.python.org/这个就是官网了,清华大学提供的
建议非清华大学校内的使用这个镜像: http://e.pypi.python.org/simple(这也是一个http://pypi.v2ex.com/simple),清华校内的就使用这个:http://mirrors.tuna.tsinghua.edu.cn/pypi/simple
使用镜像的方法可以在每次执行pip的时候加上参数"-i http://e.pypi.python.org/simple"即可,
或者也可以在本地配置,这样就不用每次都加上参数了,应用Cheer Xiao的配置(http://blog.makto.me/post/2012-11-01/pypi-mirror):
使用pip的用户可以如下配置:
在unix和macos,配置文件为:$HOME/.pip/pip.conf
在windows上,配置文件为:%HOME%\pip\pip.ini
需要在配置文件内加上:
[global]
index-url=http://mirrors.tuna.tsinghua.edu.cn/pypi/simple
还有一个小技巧,就是把所有要安装的包写在一个文件里面,比如requirement.txt(每个包写一行,顶行头写),然后pip安装的时候只需要加参数“-r requirement.txt”即可。
安装好之后体验了一把,速度飕飕的,感觉就一个字:爽~~
ps:
使用步骤:
1.进入目录下使用命令:virtualenv env
2.source env/bin/activate,这时候你会发现命令行前面会多出个(env)标识,这表明你已经进入了虚拟环境了。
写个简单的hello.py验证:
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route('/', methods = ['GET', 'POST'])
def hello():
return 'Hello world!'
if __name__ == '__main__':
app.run()
提示没有flask包,使用上面的方法pip之,pip install -i http://e.pypi.python.org/simple flask,速度超快,安装之,运行,OK。
解惑:为什么这样安装酒OK了?而且不会对系统的原有的包造成影响?
查看env目录下的内容,发现在env/lib/pythonX.X/site-packages目录下有这些安装的包,比如我的下面有这些:
distribute-0.6.24-py2.7.egg itsdangerous-0.23-py2.7.egg-info Jinja2-2.7.1-py2.7.egg-info setuptools.pth
easy-install.pth itsdangerous.py markupsafe werkzeug
flask itsdangerous.pyc MarkupSafe-0.18-py2.7.egg-info Werkzeug-0.9.3-py2.7.egg-info
Flask-0.10.1-py2.7.egg-info jinja2 pip-1.1-py2.7.egg
这就是虚拟机的作用了。
终于可以安心的把精力集中到flask上了。
(决定以后每篇文章都先放一张搞笑的图,让自己乐呵一下)
#2013.08.27
直接安装flask会安装最新0.10的版本,很悲剧的是很多扩展包还没跟上此版本,所以很多功能没法用哦~~比如Falsk-Mail的版本
所以还是重新安装Flask的0.9版本,不过这也不是个事,有了envitualenv后环境非常容易,今天试了 -i http://e.pypi.python.org/simple竟然不行了 = =!,找到这个镜像地址不错http://pypi.v2ex.com/simple
哦,安装Flask0.9版本也很容易,直接pip install Flask==0.9即可。
配置入下
gedit ~/.pip/pip.conf 然后