使用Virtualenv安装Flask

3
我正在尝试按照以下教程操作:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world#commentform 但是我似乎很迷茫。首先,从我的理解来看,运行命令“python virtualenv.py flask”会在文件夹“flask”中创建一个虚拟Python环境。这个理解正确吗?
其次,这是否意味着如果我进入该文件夹并运行“pip install flask”,它就会将Flask安装到该文件夹中?当我运行任何这些pip install命令时,我的终端都会充斥着我不理解的疯狂信息。
Benjamins-MacBook:flask test$ pip install flask==0.9
Downloading/unpacking flask==0.9
Downloading Flask-0.9.tar.gz (481kB): 481kB downloaded
Running setup.py egg_info for package flask

warning: no files found matching '*' under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
warning: no previously-included files matching '*.pyo' found under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'examples'
warning: no previously-included files matching '*.pyo' found under directory 'examples'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'docs/_themes/.git'
Downloading/unpacking Werkzeug>=0.7 (from flask==0.9)
Downloading Werkzeug-0.9.4.tar.gz (1.1MB): 1.1MB downloaded
Running setup.py egg_info for package Werkzeug

warning: no files found matching '*' under directory 'werkzeug/debug/templates'
warning: no files found matching '*' under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
warning: no previously-included files matching '*.pyo' found under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'examples'
warning: no previously-included files matching '*.pyo' found under directory 'examples'
no previously-included directories found matching 'docs/_build'
Downloading/unpacking Jinja2>=2.4 (from flask==0.9)
Downloading Jinja2-2.7.1.tar.gz (377kB): 377kB downloaded
Running setup.py egg_info for package Jinja2

warning: no files found matching '*' under directory 'custom_fixers'
warning: no previously-included files matching '*' found under directory 'docs/_build'
warning: no previously-included files matching '*.pyc' found under directory 'jinja2'
warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'jinja2'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
Downloading/unpacking markupsafe (from Jinja2>=2.4->flask==0.9)
Downloading MarkupSafe-0.18.tar.gz
Running setup.py egg_info for package markupsafe

Installing collected packages: flask, Werkzeug, Jinja2, markupsafe
Running setup.py install for flask

warning: no files found matching '*' under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
warning: no previously-included files matching '*.pyo' found under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'examples'
warning: no previously-included files matching '*.pyo' found under directory 'examples'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'docs/_themes/.git'
error: could not create '/Library/Python/2.7/site-packages/flask': Permission denied
Complete output from command /usr/bin/python -c "import          setuptools;__file__='/private/var/folders/9l/6fgb2st97cs2b2jkj7g4bs5m0000gp/T/pip_build_test/flask/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/9l/6fgb2st97cs2b2jkj7g4bs5m0000gp/T/pip-PbWDVv-record/install-record.txt --single-version-externally-managed:

有没有想法我做错了什么?
3个回答

7
你需要先在新的flask文件夹中获取bin/activate脚本,以激活虚拟环境。
如果你使用的是(最有可能),那么执行以下命令:
cd new/flask/directory
. bin/activate
pip install ...

当你想要退出虚拟环境时,可以关闭终端选项卡,或者运行以下命令:

deactivate

请注意,当您激活虚拟环境时,它只在您当前所在的终端选项卡中处于活动状态。
如果您不先运行该命令,那么pip将尝试安装到系统目录中,因此会出现您看到的权限错误。

谢谢,但是呃..我该如何获取bin/activate? - david
嘿..你能再解释一下吗?我是个初学者,真的不知道那是什么意思。 - david

0

在创建虚拟环境后,必须先激活它。

切换到刚创建的虚拟环境目录,并从bin目录中调用激活脚本:

$ source flask/bin/activate

另外,请查看这里的第一个答案:尝试安装Flask 0.9后出现警告和错误


0
错误提示: error: could not create '/Library/Python/2.7/site-packages/flask': Permission denied 这意味着您需要 root 权限来安装 flask。请尝试使用 sudo pip install flask==0.9

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接