安装Flask时出错 - Python

3 投票
3 回答
14317 浏览
提问于 2025-04-18 12:41

我正在尝试在“虚拟环境”中安装Flask。激活虚拟环境后,我在使用pip命令时遇到了一些错误(这和我的用户名中有空格有关)。不过我最终找到了解决办法,只需要输入pip所在的正确路径,这样就可以避开用户名文件夹的问题。

(venv) C:\Users\Daniel P-C>cd C:\Python34\Scripts

(venv) C:\Python34\Scripts>pip

 Usage:
      pip <command> [options]
 etc...

在尝试安装Flask时,我遇到了以下错误。

(venv) C:\Python34\Scripts>pip install flask
Downloading/unpacking flask
  Running setup.py (path:C:\Users\DANIEL~1\AppData\Local\Temp\pip_build_Daniel P
-C\flask\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)
  Running setup.py (path:C:\Users\DANIEL~1\AppData\Local\Temp\pip_build_Daniel P
-C\Werkzeug\setup.py) egg_info for package Werkzeug

    warning: no files found matching '*' under directory 'werkzeug\debug\templat
es'
    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)
  Running setup.py (path:C:\Users\DANIEL~1\AppData\Local\Temp\pip_build_Daniel P
-C\Jinja2\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 'do
cs\_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 itsdangerous>=0.21 (from flask)
  Running setup.py (path:C:\Users\DANIEL~1\AppData\Local\Temp\pip_build_Daniel P
-C\itsdangerous\setup.py) egg_info for package itsdangerous

    warning: no previously-included files matching '*' found under directory 'do
cs\_build'
Downloading/unpacking markupsafe (from Jinja2>=2.4->flask)
  Downloading MarkupSafe-0.23.tar.gz
  Running setup.py (path:C:\Users\DANIEL~1\AppData\Local\Temp\pip_build_Daniel P
-C\markupsafe\setup.py) egg_info for package markupsafe

Installing collected packages: flask, Werkzeug, Jinja2, itsdangerous, 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'
  Running setup.py install for Werkzeug

    warning: no files found matching '*' under directory 'werkzeug\debug\templat
es'
    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'
  Running setup.py install for Jinja2

    warning: no files found matching '*' under directory 'custom_fixers'
    warning: no previously-included files matching '*' found under directory 'do
cs\_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'
  Running setup.py install for itsdangerous

    warning: no previously-included files matching '*' found under directory 'do
cs\_build'
  Running setup.py install for markupsafe

    building 'markupsafe._speedups' extension
    ==========================================================================
    WARNING: The C extension could not be compiled, speedups are not enabled.
    Failure information, if any, is above.
    Retrying the build without the C extension now.


    ==========================================================================
    WARNING: The C extension could not be compiled, speedups are not enabled.
    Plain-Python installation succeeded.
    ==========================================================================
Successfully installed flask Werkzeug Jinja2 itsdangerous markupsafe
Cleaning up...

系统显示Flask已经成功安装,但当我运行Python并尝试输入import flask时,感觉好像并没有安装?我该怎么办?

(venv) C:\Python34\Scripts>python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (In
tel)] on win32
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'

顺便说一下,我是在Windows 7上运行的。

3 个回答

-2

你有没有把Python添加到你的路径里?

试试这个:

  1. 点击开始菜单
  2. 搜索“高级系统设置”并点击它
  3. 点击“环境变量”。
  4. 在“路径”变量后面加上 ;C:\python27
  5. 重启命令提示符。
1

我遇到的情况和你完全一样,Jan的回答对我没用。

问题其实很简单,我是通过apt-get安装的virtualenv:

$ sudo apt-get install virtualenv

而不是用pip来安装:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ sudo python get-pip.py
$ sudo pip install virtualenv

这样做后问题就解决了。你甚至可以在之后卸载apt-get安装的那个包:

$ sudo apt-get remove virtualenv

virtualenv 依然安装着(而且是正确的版本!)。

2

确保你使用的是虚拟环境中的pip和python

要把一个模块或包通过 pip 安装到虚拟环境中,你必须使用虚拟环境里的 pip

如果你想在虚拟环境中导入已经安装的模块或包,你需要从这个虚拟环境中运行 python 解释器。

如果你从 C:\Python 34\Scripts 这个目录运行 pippython,很可能你是在使用系统级别的 pip,这样会把 flask 安装到系统的Python里。在Linux上,这会出现问题,因为安装到系统级别的Python需要管理员权限,而在Windows的默认Python安装中,它不会报错。

建议:在默认Python安装目录之外创建你的虚拟环境,并使用这个虚拟环境里的 pippython。通常,当你激活虚拟环境时,这些操作会自动完成,所以不需要特别指定 pippython 的完整路径(不过,明确指定虚拟环境里的 pythonpip 也不会有什么坏处)。

撰写回答