如何使用 pip 在 Python3 上安装 Flask?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24525588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:45:29  来源:igfitidea点击:

How to install Flask on Python3 using pip?

pythonpython-3.xflaskpip

提问by kramer65

I want to try using Flask with Python3. I've got Python 3.4 on Ubuntu 14.04, which supposedly ships with pip included. So I tried

我想尝试在 Python3 中使用 Flask。我在 Ubuntu 14.04 上安装了 Python 3.4,据说它附带了 pip。所以我试过了

pip3 install flask

this ends in:

这结束于:

Cleaning up...
Command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip_build_kramer65/flask/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-i98xjzea-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_kramer65/flask
Storing debug log for failure in /tmp/tmpqc3b2nu5

So I tried importing it, but to no avail:

所以我尝试导入它,但无济于事:

kramer65@vps1:~/cxs$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'flask'

I can of course download it and install using sudo python3 setup.py installit that way, but I would rather do it "the standard way" so that things are easily and more standard to setup on production machines.

我当然可以下载它并以sudo python3 setup.py install这种方式安装它,但我更愿意以“标准方式”进行安装,以便在生产机器上设置更容易、更标准。

Does anybody know how I can import Flask with Python3 and pip? All tips are welcome!

有人知道如何使用 Python3 和 pip 导入 Flask 吗?欢迎所有提示!

Error log is available in http://pastebin.com/hd6LyVFP

错误日志可在http://pastebin.com/hd6LyVFP 中找到

采纳答案by msvalkon

You seem to have a permission issue. From the log you pasted to pastebin:

您似乎有权限问题。从您粘贴到 pastebin 的日志中:

error: could not create '/usr/local/lib/python3.4/dist-packages/flask': Permission denied

This is because pipwill attempt to install the package globally unless you specify a certain installation location. If you want to install this globally you must use sudoor install as a user with privileges.

这是因为pip除非您指定某个安装位置,否则将尝试全局安装包。如果要全局安装它,则必须sudo以具有特权的用户身份使用或安装。

Try the following:

请尝试以下操作:

sudo pip3 install flask

Or specify to a certain dir:

或者指定到某个目录:

pip install -t <path> flask

However, with the latter method you will have to always inject the path to sys.modulesso I suggest you just use sudoif you can.

但是,使用后一种方法,您必须始终注入路径,sys.modules因此我建议您尽可能使用sudo

Or even more preferrably, use virtualenv. Virtualenv allows you to very easilypackage your application for production because you can install only the packages you need and you've thus got automatic package isolation. Generating a requirements.txtis then as simple as pip freeze > requirements.txt. Remeber that if you end using a virtualenv, you must notuse sudoto install packages as they will then be installed outside the virtualenv.

或者更可取的是,使用virtualenv. Virtualenv 允许您非常轻松地将应用程序打包用于生产,因为您可以只安装您需要的包,因此您可以自动进行包隔离。生成 arequirements.txt就像 一样简单pip freeze > requirements.txt。请记住,如果您结束使用 virtualenv,则不能使用sudo安装包,因为它们将被安装在 virtualenv 之外。

回答by Vj Raw

For Python 3.6.4 version, it is possible to install Flask by doing:

对于 Python 3.6.4 版本,可以通过执行以下操作来安装 Flask:

sudo pip3.6 install flask