<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pipenv &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/pipenv/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Thu, 06 Nov 2025 11:36:11 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>Pipenv &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>安装 Python 依赖出现 MarkupSafe ImportError &#8230; Feature 报错的解决方法</title>
		<link>https://greyli.com/pip-pipenv-install-python-package-markupsafe-setup-py-importerror-cannot-import-name-feature/</link>
		<comments>https://greyli.com/pip-pipenv-install-python-package-markupsafe-setup-py-importerror-cannot-import-name-feature/#respond</comments>
		<pubDate>Thu, 09 Apr 2020 03:40:05 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Flask Web开发实战]]></category>
		<category><![CDATA[MarkupSafe]]></category>
		<category><![CDATA[pip]]></category>
		<category><![CDATA[Pipenv]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2899</guid>
		<description><![CDATA[这个报错在 3 月 8 号 setuptools 发布新版本之后出现，通常会在安装 Python 依赖时触发。 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>这个报错在 3 月 8 号 setuptools 发布新版本之后出现，通常会在安装 Python 依赖时触发。</p>
<h2>报错信息</h2>
<p>使用 pip 安装依赖时的报错如下：</p>
<pre class="">Collecting markupsafe==1.0
  Downloading https://.../MarkupSafe-1.0.tar.gz (14 kB)
    ERROR: Command errored out with exit status 1:
     command: '...\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'...\\pip-install-bsormril\\markupsafe\\setup.py'"'"'; __file__='"'"'...\\pip-install-bsormril\\markupsafe\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base '...\pip-install-bsormril\markupsafe\pip-egg-info'
         cwd: ...\pip-install-bsormril\markupsafe\
    Complete output (5 lines):
    Traceback (most recent call last):
      File "&lt;string&gt;", line 1, in &lt;module&gt;
      File "...\pip-install-bsormril\markupsafe\setup.py", line 6, in &lt;module&gt;
        from setuptools import setup, Extension, Feature
    ImportError: cannot import name 'Feature'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.</pre>
<p>使用 Pipenv 安装依赖时的报错如下：</p>
<pre class="">An error occurred while installing markupsafe==1.0 --hash=sha256:a6be69091dac236ea9c6bc7d012beab42010fa914c459791d627dad4910eb665! Will try again.
Installing initially failed dependencies…
[pipenv.exceptions.InstallError]:   File "...\Python\Python36\site-packages\pipenv\core.py", line 1874, in do_install
[pipenv.exceptions.InstallError]:       keep_outdated=keep_outdated
[pipenv.exceptions.InstallError]:   File "...Python\Python36\site-packages\pipenv\core.py", line 1253, in do_init
[pipenv.exceptions.InstallError]:       pypi_mirror=pypi_mirror,
[pipenv.exceptions.InstallError]:   File "...\Python\Python36\site-packages\pipenv\core.py", line 859, in do_install_dependencies
[pipenv.exceptions.InstallError]:       retry_list, procs, failed_deps_queue, requirements_dir, **install_kwargs
[pipenv.exceptions.InstallError]:   File "...\Python\Python36\site-packages\pipenv\core.py", line 763, in batch_install
[pipenv.exceptions.InstallError]:       _cleanup_procs(procs, not blocking, failed_deps_queue, retry=retry)
[pipenv.exceptions.InstallError]:   File "...\Python\Python36\site-packages\pipenv\core.py", line 681, in _cleanup_procs
[pipenv.exceptions.InstallError]:       raise exceptions.InstallError(c.dep.name, extra=err_lines)
[pipenv.exceptions.InstallError]: ['Looking in indexes: https://.../pypi/simple', 'Collecting markupsafe==1.0', '  Using cached https://.../MarkupSafe-1.0.tar.gz (14 kB)']
[pipenv.exceptions.InstallError]: ['ERROR: Command errored out with exit status 1:', '
command: \'...\\.virtualenvs\\helloflask-evdb6idn\\scripts\\python.exe\' -c \'import sys, setuptools, tokenize; sys.argv[0] = \'"\'"\'...\pip-install-pkgojp4t\\\\markupsafe\\\\setup.py\'"\'"\'; 
__file__=\'"\'"\'...\\\\pip-install-pkgojp4t\\\\markupsafe\\\\setup.py\'"\'"\';f=getattr(tokenize, \'"\'"\'open\'"\'"\', open)(__file__);code=f.read().replace(\'"\'"\'\\r\\n\'"\'"\', \'"\'"\'\\n\'"\'"\');f.close();exec(compile(code, __file__, \'"\'"\'exec\'"\'"\'))\' egg_info --egg-base \'...\\pip-install-pkgojp4t\\markupsafe\\pip-egg-info\'', '
cwd: ...\\pip-install-pkgojp4t\\markupsafe\\', '    Complete output (5 lines):', '    
Traceback (most recent call last):', '      
File "&lt;string&gt;", line 1, in &lt;module&gt;', ' 
File "...\\pip-install-pkgojp4t\\markupsafe\\setup.py", line 6, in &lt;module&gt;', '
from setuptools import setup, Extension, Feature', "
ImportError: cannot import name 'Feature'", '----------------------------------------',
'ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.']
ERROR: ERROR: Package installation failed...</pre>
<p>其他工具输出类似，主要异常信息是 MarkupSafe setup.py: ImportError: cannot import name Feature。通常会在安装 Flask 项目的依赖时发生，因为 MarkupSafe 是 Flask 的依赖之一。</p>
<h2>原因和解决方法</h2>
<p>出现这个报错的原因是因为 Python 打包工具 setuptools 在 <a href="https://setuptools.readthedocs.io/en/latest/history.html#v46-0-0" rel="nofollow noopener">46.0.0 版本</a>删掉了弃用的 <code>Feature</code>，而 MarkupSafe 刚好在 setup.py 文件里导入了这个类，所以会出现报错。</p>
<p>解决方法很多，最直接的是更新 MarkupSafe 到最新版本（1.1.1），新版本去掉了对 Feature 类的导入。如果使用 requirements.txt 存储依赖列表，那就把 MarkupSafe 的版本号改成 1.1.1（找到 MarkupSafe 开头那一行，替换版本号）：</p>
<pre class="">MarkupSafe==1.1.1</pre>
<p>然后重新执行：</p>
<pre class="">$ pip install -r requirements.txt</pre>
<p>对于 Pipenv，可以直接执行：</p>
<pre class="">$ pipenv install markupsafe==1.1.1</pre>
<p>如果你是《Flask Web 开发实战》的读者，正在为第一部分的示例程序安装依赖，那你还需要执行下面的命令固定 sendgrid-python 的版本（它在新版本添加了不向后兼容的 API 变动）：</p>
<pre class="">$ pipenv install sendgrid==5.3.0</pre>
<h2>《Flask Web 开发实战》读者备注</h2>
<p>如果你在 2020 年 3 月 8 号到 4 月 5 号之间买了《Flask Web 开发实战》，把示例程序克隆到了本地，然后尝试运行 pipenv install 或 pip install -r requirements.txt 命令来安装依赖，那很大概率你会遇到这个问题。</p>
<p>除了使用上面的方法之外，你还可以通过更新本地代码来解决。我最近给所有示例程序的依赖文件做了一次更新，除了书里涉及的 API 产生变动的依赖，其他依赖都已经更新到最新版本。</p>
<p>你可以使用下面的命令来更新你在本地的程序仓库（注意这会重置你对源码进行的修改）：</p>
<pre class="">$ git fetch --all
$ git fetch --tags
$ git reset --hard origin/master</pre>
<p>然后重新执行一次 pipenv install 或 pip install -r requirements.txt 即可解决这个问题。如果遇到其他虚拟环境和依赖安装的问题可以参考<a href="http://greyli.com/flask-web-book-pipenv-problem-solved/">这篇文章</a>解决。</p>
<p>相关链接：</p>
<ul>
<li><a href="https://discuss.helloflask.com/t/topic/458">https://discuss.helloflask.com/t/topic/458</a></li>
<li><a href="https://github.com/pallets/markupsafe/issues/57">https://github.com/pallets/markupsafe/issues/57</a></li>
<li><a href="https://github.com/pypa/setuptools/issues/65">https://github.com/pypa/setuptools/issues/65</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/pip-pipenv-install-python-package-markupsafe-setup-py-importerror-cannot-import-name-feature/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《Flask Web 开发实战》虚拟环境/依赖/Pipenv 等问题解决方法</title>
		<link>https://greyli.com/flask-web-book-pipenv-problem-solved/</link>
		<comments>https://greyli.com/flask-web-book-pipenv-problem-solved/#comments</comments>
		<pubDate>Tue, 07 Apr 2020 10:43:36 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Flask Web开发实战]]></category>
		<category><![CDATA[Pipenv]]></category>
		<category><![CDATA[虚拟环境]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2887</guid>
		<description><![CDATA[注：这篇文章的主要受众是《Flask Web 开发实战》的读者。 注2：文中的 $ 符号标识一条命令行命令的开 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p class="md-end-block md-p"><span class="md-plain">注：这篇文章的主要受众是《</span><span class="md-link"><a spellcheck="false" href="http://helloflask.com/book/"><span class="md-plain">Flask Web 开发实战</span></a></span><span class="md-plain">》的读者。</span></p>
<p class="md-end-block md-p"><span class="md-plain">注2：文中的 $ 符号标识一条命令行命令的开始，$ 前面是当前工作目录，# 号后面是注释。你实际只需要输入 $ 符号和 # 号之间的内容，不包括开头和结尾的空格。</span></p>
<p class="md-end-block md-p"><span class="md-plain">在群聊和论坛里总是看到和虚拟环境和依赖安装相关的各类问题，这篇文章希望能够提供一个统一的解决方案。下次如果遇到有人问虚拟环境/Pipenv/依赖安装相关的问题，请把这篇文章的链接丢过去。</span></p>
<h2 class="md-end-block md-heading"><span class="md-plain">安装 Python 库非常慢？</span></h2>
<p class="md-end-block md-p"><span class="md-plain">在进入正题之前，你需要先解决基础设施问题。你在执行 pip install 命令或 pipenv install 等命令时会不会网速非常慢？20k/s 或者干脆看到 Time out，Connection reset 之类的报错，这种情况下，你需要设置 PyPI 镜像。具体操作可以在这篇《</span><span class=" md-link"><a spellcheck="false" href="http://greyli.com/set-custom-pypi-mirror-url-for-pip-pipenv-poetry-and-flit/"><span class="md-plain">从国内的 PyPI 镜像（源）安装 Python 包</span></a></span><span class="md-plain">》看到。</span></p>
<h2 class="md-end-block md-heading"><span class="md-plain">要不要继续使用 Pipenv？</span></h2>
<p class="md-end-block md-p"><span class="md-plain">因为书里面在一开始介绍了使用 Pipenv 管理依赖和虚拟环境，同时所有的安装第三方库的命令也都是使用 Pipenv，所以我们要解决的第一个问题是「要不要继续使用 Pipenv？」</span></p>
<p class="md-end-block md-p"><span class="md-plain">我的建议是，如果你在使用的过程中没有遇到过任何报错，那么就继续使用它。直到你觉得它在某些地方不再让你满意。</span></p>
<p class="md-end-block md-p md-focus"><span class="md-plain md-expand">但是如果你在使用的过程中遇到了问题（首先确保你使用的是最新版本的 Pipenv），比如：</span></p>
<ul>
<li class="md-end-block md-p"><span class="md-plain">锁定依赖很慢，停留在「locking&#8230;」这样的提示不动</span></li>
<li class="md-end-block md-p"><span class="md-plain">执行正确的命令但是总是出现报错</span></li>
</ul>
<blockquote>
<p><span data-offset-key="fm33e-0-0">附注 如果你安装依赖时的报错是「MarkupSafe </span><span data-offset-key="fm33e-1-0">setup.py</span><span data-offset-key="fm33e-2-0">: ImportError: cannot import name Feature」，请参考</span><a class="Link ztext-link" href="http://greyli.com/pip-pipenv-install-python-package-markupsafe-setup-py-importerror-cannot-import-name-feature/" target="_blank" rel="noopener" data-offset-key="fm33e-3-0" data-editable="true"><span data-offset-key="fm33e-3-0">这篇文章</span></a><span data-offset-key="fm33e-4-0">解决。</span></p>
</blockquote>
<p class="md-end-block md-p"><span class="md-plain">那么就继续看下去。</span></p>
<h2 class="md-end-block md-heading"><span class="md-plain">如果不用 Pipenv，我该怎么办？</span></h2>
<p class="md-end-block md-p"><span class="md-plain">解决方法和替代工具非常多，这里给出两个。</span></p>
<p class="md-end-block md-p"><span class=""><strong><span class="md-plain">方法一：不用虚拟环境</span></strong></span></p>
<p class="md-end-block md-p"><span class="md-plain">最简单的解决方法就是不用虚拟环境。如果你是一个初学者，那么不用虚拟环境完全没问题。现在你把所有的 Python 包全都安装在一个盒子里，你只需要会使用 pip 安装依赖，也就是使用下面的 pip install 命令：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n184" mdtype="fences">$ pip install flask</pre>
<blockquote>
<p class="md-end-block md-p"><span class="md-plain">附注 顺便说一句，不用虚拟环境时，如果你是使用 Linux 和 macOS 系统的 Python 3 用户，那么执行 Python 和 pip 相关命令的时候需要输入的是 python3 和 pip3，比如：pip3 install flask。后面不再提示。</span></p>
</blockquote>
<p class="md-end-block md-p"><span class="md-plain">这个命令会为你安装 Flask。每到需要安装一个包，你就执行这个命令，把上面的 flask 替换成你要安装的包名即可。类似的，书中所有 pipenv install xxx 形式的命令也都替换为 pip install xxx。</span></p>
<p class="md-end-block md-p"><span class="md-plain">这时你可以跳过 Pipenv 和虚拟环境相关的内容。在第一章，当你把当前工作目录切换到 helloflask 文件夹内之后，整个 1.1 小节你只需要执行下面这行命令：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n189" mdtype="fences">$ pip install -r requirements.txt</pre>
<p class="md-end-block md-p"><span class="md-plain">而第二部分每章开头的下面这两行命令：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n191" mdtype="fences">$ pipenv install --dev
$ pipenv shell</pre>
<p class="md-end-block md-p"><span class="md-plain">都要替换为：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n193" mdtype="fences">$ pip install -r requirements.txt</pre>
<p class="md-end-block md-p"><span class="md-plain">这个命令会安装对应项目的所有依赖，所以后续介绍各个 Python 库时的安装命令不需要再执行。</span></p>
<p class="md-end-block md-p"><span class=""><strong><span class="md-plain">方法二：使用 virtualenv/venv 来管理虚拟环境，搭配 pip 来管理依赖</span></strong></span></p>
<p class="md-end-block md-p"><span class="md-plain">第二种方法是改用更基础的工具：你仍然使用 pip 安装依赖，同时搭配 virtualenv 或 venv 来管理虚拟环境。如果你选择这个方法，那就跳过书中对 Pipenv 的介绍，改为阅读这篇《</span><span class=" md-link"><a spellcheck="false" href="http://greyli.com/back-to-virtualenv-venv-and-pip/"><span class="md-plain">要不我们还是用回 virtualenv/venv 和 pip 吧</span></a></span><span class="md-plain">》。阅读完上述文章后，再继续阅读。</span></p>
<p class="md-end-block md-p"><span class="md-plain">现在你可以跳过 Pipenv 相关的内容。第一章切换进 helloflask 目录后，整个 1.1 小节你只需要执行下面的命令：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n198" mdtype="fences">$ python -m venv env  # Linux、macOS 系统的 Python3 用户，使用 python3 -m venv env
$ env\Scripts\activate  # Linux、macOS 系统使用 source env/bin/activate
$ pip install -r requirements.txt  # 这个命令会安装对应项目的所有依赖</pre>
<blockquote>
<p class="md-end-block md-p"><span class="md-plain">附注 ：上面命令里 # 号及之后的文字是注释，不需要输入。如果你使用 Python2，第一条命令需要改为 virtualenv env。这三行命令的作用依次为：创建虚拟环境、激活虚拟环境、从 requirements.txt 文件安装依赖列表。</span></p>
</blockquote>
<p class="md-end-block md-p"><span class="md-plain">类似的，第二部分每章开头的下面这两行命令：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n202" mdtype="fences">$ pipenv install --dev
$ pipenv shell</pre>
<p class="md-end-block md-p"><span class="md-plain">都要替换为：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n204" mdtype="fences">$ python -m venv env  # Linux、macOS 系统的 Python3 用户，使用 python3 -m venv env
$ env\Scripts\activate  # Linux、macOS 系统使用 source env/bin/activate
$ pip install -r requirements.txt  # 这个命令会安装对应项目的所有依赖</pre>
<p class="md-end-block md-p"><span class="md-plain">同时书中所有 pipenv install xxx 形式的命令都替换为 pip install xxx（不过你并不需要一个一个执行，因为每章开头执行 pip install -r requirements.txt 时会安装所有项目相关的依赖）。</span></p>
<p class="md-end-block md-p"><span class="md-plain">对于 PyCharm 设置 Python 解释器部分的内容，大致可以沿用，只不过在图 1-4 的位置你需要从列表里选择当前目录 env 文件夹（虚拟环境文件夹）中的 Python 解释器，根据操作系统的不同，将会是 env/bin/python 或 env\Scripts\python.exe。</span></p>
<p class="md-end-block md-p"><span class="md-plain">退出虚拟环境时，使用下面的命令：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n208" mdtype="fences">$ deactivate</pre>
<h2 class="md-end-block md-heading"><span class="md-plain">使用书中同样的代码，但是却出现报错？/ 我该怎么安装依赖？</span></h2>
<p class="md-end-block md-p"><span class="md-plain">有一些 Python 库在版本变化时会带来 API 的变化，而书中示例程序代码是基于每章开头注明的 Python 库来开发的，所以在更新依赖版本的时候可能会导致示例程序的代码出错。我建议你按照书中的命令来安装依赖，这会从项目依赖文件里安装固定版本的依赖列表，不要跳过第二部分每章开头的命令。</span></p>
<p class="md-end-block md-p"><span class="md-plain">具体来说，如果使用 Pipenv，对应的安装依赖的命令是：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n212" mdtype="fences">$ pipenv install --dev</pre>
<p class="md-end-block md-p"><span class="md-plain">如果使用 pip，对应的命令则是（如果创建了虚拟环境，需要先激活虚拟环境）：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n214" mdtype="fences">$ pip install -r requirements.txt</pre>
<p class="md-end-block md-p"><span class="md-plain">如果是第一章的示例程序，那么在 helloflask 目录下执行一次即可。如果是第二章的四个程序，那么在每一个程序的根目录执行一次即可。后续所有介绍新的 Python 包时给出的安装命令不用再执行。</span></p>
<h2 class="md-end-block md-heading"><span class="md-plain">关于第一部分示例程序的项目结构和启动问题</span></h2>
<p class="md-end-block md-p"><span class="md-plain">在第一部分的源码中，一共有 6 个 Flask 程序，分别保存在 helloflask/demos/ 目录下的六个子文件夹内。用来存储环境变量的 .env 和 .flaskenv 文件需要在这些子文件夹内创建，而不是放到顶层目录（helloflask/）。同时为了方便操作，这 6 个程序共用同一个虚拟环境，所以在 helloflask/ 目录下创建虚拟环境。</span></p>
<blockquote>
<p class="md-end-block md-p"><span class="md-plain">注意 不要在 helloflask/ 目录下创建 .env 和 .flaskenv 文件，这会导致子目录下的程序无法正确启动（</span><span class=" md-link"><a spellcheck="false" href="https://github.com/pallets/flask/issues/3561"><span class="md-plain">issue #3561</span></a></span><span class="md-plain">）。</span></p>
</blockquote>
<p class="md-end-block md-p"><span class="md-plain">如果你按照书里给出的提示执行命令，一般不会出现问题。但是为了防止各种意外情况，这里列一下本书第一部分的操作流程（$ 符号前是当前工作目录）。</span></p>
<p class="md-end-block md-p"><span class="md-plain">具体的操作顺序就是，克隆仓库以后，切换到 helloflask 目录：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n222" mdtype="fences">$ cd helloflask</pre>
<p class="md-end-block md-p"><span class="md-plain">然后创建虚拟环境、激活虚拟环境、安装依赖：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n224" mdtype="fences">/helloflask $ python -m venv env  # Linux、macOS 系统的 Python3 用户，使用 python3 -m venv env
/helloflask $ env\Scripts\activate  # Linux、macOS 系统使用 source env/bin/activate
/helloflask $ pip install -r requirements.txt  # 这个命令会安装对应项目的所有依赖</pre>
<p class="md-end-block md-p"><span class="md-plain">接着看到介绍 Flask 最小程序部分，启动第一部分的示例程序：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n226" mdtype="fences">helloflask/ $ cd demos/hello
helloflask/demos/hello $ flask run</pre>
<p class="md-end-block md-p"><span class="md-plain">现在看到了第二章，启动第二部分的示例程序（先执行 cd .. 回到上层目录，即 demos/ 目录）：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n228" mdtype="fences">helloflask/demos/hello $ cd ..
helloflask/demos $ cd http
helloflask/demos/http $ flask run</pre>
<p class="md-end-block md-p"><span class="md-plain">看到第三章，启动第三部分的示例程序：</span></p>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n230" mdtype="fences">helloflask/demos/http $ cd ..
helloflask/demos $ cd template
helloflask/demos/template $ flask run</pre>
<p class="md-end-block md-p"><span class="md-plain">以此类推。</span></p>
<h2 class="md-end-block md-heading"><span class="md-plain">遇到了自己没法解决的问题怎么办？</span></h2>
<p class="md-end-block md-p"><span class="md-plain">如果遇到问题，你可以先尝试：</span></p>
<ul>
<li class="md-end-block md-p"><span class="md-plain">看一下本书 </span><span class=" md-link"><a spellcheck="false" href="https://github.com/greyli/helloflask"><span class="md-plain">GitHub 仓库</span></a></span><span class="md-plain">里的 </span><span class="md-link"><a spellcheck="false" href="https://github.com/greyli/helloflask/blob/master/faq/faq.md"><span class="md-plain">FAQ 页面</span></a></span><span class="md-plain"> 有没有你要找的答案</span></li>
<li class="md-end-block md-p"><span class="md-plain">在搜索引擎搜索你的错误信息关键字，尝试自己解决</span></li>
</ul>
<p>自己无法解决的话，可以：</p>
<ul>
<li class="md-end-block md-p"><span class="md-plain">发到 </span><span class=" md-link"><a spellcheck="false" href="https://github.com/greyli/helloflask/issues"><span class="md-plain">GitHub</span></a></span><span class="md-plain">（issue）和</span><span class="md-link"><a spellcheck="false" href="https://discuss.helloflask.com/"><span class="md-plain">论坛</span></a></span><span class="md-plain">（用文字形式贴出完整的错误信息、相关代码和命令，尽可能的详细描述相关信息）。</span></li>
<li class="md-end-block md-p"><span class="md-plain">发到</span><span class=" md-link"><a spellcheck="false" href="http://helloflask.com#discuss"><span class="md-plain">交流群</span></a></span><span class="md-plain md-expand">（建议优先选择发到 GitHub 和论坛，我会定期回复，群聊沟通效率很低，更适合闲聊）</span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/flask-web-book-pipenv-problem-solved/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>回应《也谈「不要用 Pipenv」》</title>
		<link>https://greyli.com/reply-to-talk-dont-use-pipenv/</link>
		<comments>https://greyli.com/reply-to-talk-dont-use-pipenv/#respond</comments>
		<pubDate>Mon, 02 Sep 2019 08:02:52 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Pipenv]]></category>
		<category><![CDATA[Poetry]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2552</guid>
		<description><![CDATA[看了董伟明老师（@董伟明）的《也谈「不要用 Pipenv」》，这篇文章对其中的一些观点做出一些回应和解释。 也 [&#8230;]]]></description>
				<content:encoded><![CDATA[<div class="Editable-unstyled" data-offset-key="a98tu-0-0" data-editor="be9h6" data-block="true">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="a98tu-0-0">
<p>看了董伟明老师（@<a href="https://zhuanlan.zhihu.com/people/6d23d8e16eb481e548032719bb5898a4">董伟明</a>）的《<a href="https://zhuanlan.zhihu.com/p/80683249">也谈「不要用 Pipenv」</a>》，这篇文章对其中的一些观点做出一些回应和解释。</p>
<p>也看了 Frost Ming 老师（@<a href="https://zhuanlan.zhihu.com/people/811ac74098f8bc759f02d696eb0b22ea">岂不美哉</a>）的《<a href="https://zhuanlan.zhihu.com/p/80695813">Pipenv 有什么问题</a>》，很感谢他做出的努力，祝 Pipenv 早日脱离 Kenneth Retiz 的影响，越来越好。</p>
<p>（Kenneth Retiz 下文简称 KR）</p>
<h2>KR 有没有借 PyPA 之名来做背书？</h2>
<blockquote>
<p>所以作者并没有想着用来背书。</p>
</blockquote>
<p>我仍然认为 KR 有利用 PyPA 做背书，甚至在误导别人 Pipenv 是 Python 官方（混淆 PyPA 和 Python 官方的概念）推荐的工具。</p>
<p><strong>证据 1</strong></p>
<p>2017 年 8 月 29 号，在 Pipenv 还不够成熟的时候，PyPA 成员 Thea Flowers 创建了<a href="https://github.com/pypa/packaging.python.org/pull/369">一个 PR</a> 要把 Pipenv 添加到 PyPA 的打包教程里，介绍使用 Pipenv 安装和管理依赖（注意这时候 Pipenv 根据教程的内容在 Windows 上是没法正常使用的，具体见 9 月 2 号的<a href="https://github.com/pypa/packaging.python.org/issues/373">这个 issue</a>）。</p>
<p>2017 年 8 月 31 号，Thea Flowers 自己合并了 PR。注意这个教程页面是临时的单独页面，还没有正式放到打包教程的页面里。</p>
<p>2017 年 9 月 1 号，KR 在 Pipenv 的 README 里加了这样一行介绍语：「Pipenv — the <a href="https://packaging.python.org/new-tutorials/installing-and-using-packages/">officially recommended</a> Python packaging tool from <a href="https://packaging.python.org/new-tutorials/installing-and-using-packages/">Python.org</a>, free (as in freedom).」（<a href="https://github.com/pypa/pipenv/commit/6e06fc451767a57e6fccb828c74a1412f6cef687">commit</a>）</p>
<p>其中的关键内容翻译过来大概是「官方推荐的 Python 打包工具，来自 Python.org」。</p>
<p>仅仅因为在 PyPA 的打包文档里加入了一个短教程（其中介绍了使用 Pipenv 安装和管理依赖），然后 KR 就在 Pipenv 的介绍里宣传这是「官方推荐」，而注明的官方来源则是「Python.org」，这两个关键词背后的超链接都是 PyPA 打包文档的 Pipenv 介绍。packaging.python.org（PyPA） 和 python.org（Python 官方） 的区别很大，很明显，他清楚两者的区别，但又故意没有表达清楚。<br />
退一步讲，不管他的意图是什么，这样的措辞都会让人以为是 Python 官方推荐的打包工具，尤其是对 PyPA 这个组织不了解的人，看到 Python.org 都会认为是 Python 官方。</p>
<p><strong>证据 2</strong></p>
<p>在 PyCon 2018（五月），KR 在演讲《Pipenv: The Future of Python Dependency Management》里介绍 Pipenv 的卖点的时候，列在第一条的仍然是上面那一句「Officially recommended tool from python.org」：</p>
<p><img class="alignnone size-large wp-image-2553" src="http://greyli.com/wp-content/uploads/2019/09/pipenv-evidence-1024x576.jpg" alt="" width="625" height="352" srcset="https://greyli.com/wp-content/uploads/2019/09/pipenv-evidence-1024x576.jpg 1024w, https://greyli.com/wp-content/uploads/2019/09/pipenv-evidence-150x84.jpg 150w, https://greyli.com/wp-content/uploads/2019/09/pipenv-evidence-300x169.jpg 300w, https://greyli.com/wp-content/uploads/2019/09/pipenv-evidence-624x351.jpg 624w, https://greyli.com/wp-content/uploads/2019/09/pipenv-evidence.jpg 1586w" sizes="(max-width: 625px) 100vw, 625px" /></p>
<p>和在 README 里不同的是，这次在演讲上别人没法去点那个 python.org 的链接去甄别究竟是 python.org（Python 官方） 还是 packaging.python.org（PyPA）。而 KR 在介绍这里的时候没有任何说明，直接说是「来自 Python.org 的官方推荐」。</p>
<p>至于 KR 和 PyPA 或者说和 Thea Flowers 有什么关系？把 Pipenv 的介绍加到打包文档是 Thea Flowers 的个人意愿？还是 PyPA 的 35 个成员全部同意的结果？是什么促使 PyPA 在 Pipenv 还不成熟、甚至教程里的内容没法在 Windows 上正常操作的情况下添加到打包教程里？这些问题我暂时找不到答案。</p>
<p>（注：PyPA 指的是 Python Packaging Authority，一个负责维护 Python 打包相关的库（比如 pip、virtualenv 等）和文档的组织。）</p>
<h2>从 0 升到 18 的措辞问题</h2>
<blockquote>
<p>而所谓的18.X.X是 calver versioning(基于日历的版本)</p>
</blockquote>
<p>在上一篇文章里，我引用了一段 <a href="https://news.ycombinator.com/item?id=18612905">HN 上的评论</a>来概括 Pipenv 在推广方式上的问题：</p>
<blockquote>
<p>Kenneth Retiz 滥用他在 PyPA 的位置（而且快速把一个实际上是 beta 状态的产品的版本号从 0 升到 18）来暗示 Pipenv 已经非常稳定，受到大力支持并且非常官方，但事实却并不是这样。</p>
</blockquote>
<p>这句话的英文原文是：</p>
<blockquote>
<p>However, Kenneth abused his position with PyPA (and quickly bumped a what is a beta product to version 18) to imply Pipenv was more stable, more supported and more official than it really was.</p>
</blockquote>
<p>其中关于版本的部分是「and quickly bumped a what is a beta product to version 18」，可能是我乱翻译造成了误解……我认为原文里的 18 就是一个夸张的表达方式，把这里的数字换成 100 也可以表达同样的意思（也可能是指 <a href="https://github.com/pypa/pipenv/commit/90a02c634806f93cf6b4b40be204b5d160aa63eb">0.3.0 跳到 3.0.1</a> 那次）。换用日期版本号（CalVer）那次看起来没什么问题（11.10.4 -&gt; 2018.05.12），所以我认为这里的 18 和日期版本号没关系。</p>
<h2>Lockfile 只要过期就重新生成是合理的吗？</h2>
<blockquote>
<p>Kenneth Reitz 先是说 lockfile 只要是过期了就总是会被重新生成</p>
<p><span style="font-size: 1rem;">这是对的，Pipfile和Pipfile.lock是对应的，当执行pipenv install后改了Pipfile，对应的Pipfile.lock就</span><span style="font-size: 1rem;">定会改。错误的是，不应该改那些不相关包的版本: 既然已经是==的了，就表明确定了具体版本呀。</span></p>
<p><span style="font-size: 1rem;">这些问题，其实源于 Pipfile对应依赖在一开始没指定具体版本，也就是Pipfile对标requirements.txt，而Pipfile.lock只是当前环境的一个「快照」，如果Pipfile没有明确版本就用Pipfile.lock里面指定的。</span></p>
</blockquote>
<p>我的主要想法是这样的功能实现是不合理的。Pipenv 在安装一个包的时候默认就使用通配符（*）版本写到 Pipfile 是不合理的设计。这样的设计不符合正常的开发流程和使用习惯。如果我在安装一个包的时候就要明确自己要安装哪个版本，以便在 Pipfile 里固定版本，这样会很不方便，而且让 Pipfile.lock 的存在意义变得很弱。</p>
<p>按照 KR 自己的解释，Pipfile 对标的是 requirements.in，Pipfile.lock 对标的是 requirements.txt：</p>
<p><img class="alignnone size-full wp-image-2554" src="http://greyli.com/wp-content/uploads/2019/09/req1.jpg" alt="" width="910" height="679" srcset="https://greyli.com/wp-content/uploads/2019/09/req1.jpg 910w, https://greyli.com/wp-content/uploads/2019/09/req1-150x112.jpg 150w, https://greyli.com/wp-content/uploads/2019/09/req1-300x224.jpg 300w, https://greyli.com/wp-content/uploads/2019/09/req1-624x466.jpg 624w" sizes="(max-width: 910px) 100vw, 910px" /></p>
<p><img class="alignnone size-full wp-image-2555" src="http://greyli.com/wp-content/uploads/2019/09/req2.jpg" alt="" width="868" height="676" srcset="https://greyli.com/wp-content/uploads/2019/09/req2.jpg 868w, https://greyli.com/wp-content/uploads/2019/09/req2-150x117.jpg 150w, https://greyli.com/wp-content/uploads/2019/09/req2-300x234.jpg 300w, https://greyli.com/wp-content/uploads/2019/09/req2-624x486.jpg 624w" sizes="(max-width: 868px) 100vw, 868px" /><br />
按照大部分人的理解，Pipfile 是所有不固定版本的高层依赖的列表（unpinned），而 Pipfile.lock 是固定安装时采用版本的详细依赖列表（pinned），用来复现程序具体的依赖环境；除非我主动执行 update 命令更新某个依赖，否则 Pipfile.lock 不应该被改动。但实际的 Pipenv 并不是这样，更新 Pipfile.lock 变成了频繁发生（install/uninstall/update）的默认行为。</p>
<h2>Poetry 分析依赖慢</h2>
<blockquote>
<p>Resolving dependencies&#8230; (422.9s)</p>
<p><span style="font-size: 1rem;">安装个包7分钟，这&#8230; 谁能忍？你们试试把bluelog项目的依赖用poetry add加一遍需要多久？我反正体验不下去了</span></p>
</blockquote>
<p>实际测试安装 Flask-SQLAlchemy，解析依赖花了 42 秒（Windows+代理），没有 7 分钟那么夸张。因为解析依赖的结果会被缓存，我就在另一台 Mac（代理）上也试了一遍，结果只花了 8.3 秒。可能是网络状况的问题？</p>
<p>另外我试了把 Bluelog 的所有依赖一次性安装（Windows+代理），其中解析依赖只花了 16.8 秒（因为解析结果缓存的原因，实际也许会稍久一点）：</p>
<pre class="">$ poetry add flask flask-ckeditor flask-mail flask-sqlalchemy flask-wtf flask-moment python-dotenv bootstrap-flask flask-login flask-debu gtoolbar gunicorn psycopg2 flask-migrate
Using version ^1.1 for flask
Using version ^0.4.3 for flask-ckeditor
Using version ^0.9.1 for flask-mail
Using version ^2.4 for flask-sqlalchemy
Using version ^0.14.2 for flask-wtf
Using version ^0.9.0 for flask-moment
Using version ^0.10.3 for python-dotenv
Using version ^1.0 for bootstrap-flask
Using version ^0.4.1 for flask-login
Using version ^0.10.1 for flask-debugtoolbar
Using version ^19.9 for gunicorn
Using version ^2.8 for psycopg2
Using version ^2.5 for flask-migrate

Updating dependencies
Resolving dependencies... (16.8s)</pre>
<p>所以 Poetry 或许没那么糟糕（当然我还没深入使用过）。</p>
<h2>星星</h2>
<blockquote>
<p>虽然不是决定性的，但是对于这Star不到6K的项目来说我是不敢用的</p>
</blockquote>
<p>Pipenv 的 Star 的确很多（而且 README、文档甚至代码里到处都是星星✨），KR 可是个营销专家，但项目质量却并没有那么好。反正我现在一看见星星和蛋糕就有点头疼。</p>
<p>✨?✨</p>
<p>另外，顺便说一句，pip（5627）、virtualenv（3189）和 setuptools（883） 的 Star 数量都没到六千……</p>
<p>好吧，我承认最后这两段是在抬杠 :D</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/reply-to-talk-dont-use-pipenv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>不要用 Pipenv</title>
		<link>https://greyli.com/do-not-use-pipenv/</link>
		<comments>https://greyli.com/do-not-use-pipenv/#comments</comments>
		<pubDate>Sat, 31 Aug 2019 07:16:30 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Pipenv]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[虚拟环境管理工具]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2544</guid>
		<description><![CDATA[注意：本文写于 2019 年 8 月，其中描述的内容在新版本的 Pipenv 中或已得到修复或改进，请谨慎参考 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>注意：本文写于 2019 年 8 月，其中描述的内容在新版本的 Pipenv 中或已得到修复或改进，请谨慎参考。</p>
<p>如果你是《Flask Web 开发实战》的读者，请访问<a href="http://greyli.com/flask-web-book-pipenv-problem-solved/">《Flask Web 开发实战》虚拟环境/依赖/Pipenv 等问题解决方法</a>。</p>
<hr />
<p>Pipenv 让我用的很痛苦，有一种被欺骗的感觉，而且很后悔在《<a href="http://greyli.com/flask-web-development/">Flask Web 开发实战</a>》里采用它。</p>
<p>大部分情况下，它很好用，但却存在太多问题，有一些问题让人简直没法接受。我知道有人会说「这是开源程序，有 bug 就自己去修」、「爱用不用，没人强迫你」，但问题是，一个进行大肆推广，甚至借 PyPA 做背书来宣传（经常让人误以为是 Python 官方推荐）的工具却连基本的使用流程都没做好，这不是合理和正常的行为。 引用<a href="https://news.ycombinator.com/item?id=18612905">这个 HN 评论</a>的话说就是：</p>
<blockquote>
<p>Kenneth Retiz 滥用他在 PyPA 的位置（而且快速把一个实际上是 beta 状态的产品的版本号从 0 升到 18）来暗示 Pipenv 已经非常稳定，受到大力支持并且非常官方，但事实却并不是这样。</p>
</blockquote>
<p>在这篇（劝退）文章里，我会分别从包的安装、更新、卸载来测试并指出 Pipenv 的一些问题。</p>
<h2>测试准备</h2>
<ul>
<li>项目：<a href="https://github.com/greyli/bluelog">Bluelog</a>（一个 Flask 博客）</li>
<li>Pipenv 版本：2018.11.26（最新版本）</li>
<li>操作系统：Windows 10，Cmder</li>
<li>测试流程：每一次测试命令前都会删除已经创建的虚拟环境（pipenv &#8211;rm），重置 Pipfile 和 Pipfile.lock 文件变动，然后重新创建虚拟环境（pipenv install）。</li>
</ul>
<h2>安装包</h2>
<p>假设想给这个旧项目 Bluelog 添加新功能，拿到旧项目的代码，打算安装一个 Flask-Avatars 包。查了文档，发现安装包要使用 <a href="https://docs.pipenv.org/en/latest/basics/#pipenv-install">pipenv install</a> 命令，所以执行了下面的命令：</p>
<pre class="">$ pipenv install flask-avatars</pre>
<p>结果发现其他所有的不相干依赖都被更新了……</p>
<p>WTF，这不是反人类吗（说好的「Python Development Workflow for Humans.」呢）？我安装一个包，默认行为竟然是更新其他所有不相干且已经锁定版本的依赖！</p>
<p>翻了文档才发现，要加一个 &#8211;keep-outdated 选项才能避免更新其他锁定的依赖：</p>
<pre class="">$ pipenv install --help
...
--keep-outdated Keep out-dated dependencies from being updated in
Pipfile.lock. [env var: PIPENV_KEEP_OUTDATED]</pre>
<p>好吧，那先忍着，多打一个命令行选项就是了：</p>
<pre class="">$ pipenv install --keep-outdated flask-avatars</pre>
<p>WTF，为什么所有依赖还是被更新了？</p>
<p>好吧，有 bug 很正常，我来提个 issue 吧，哎，好像有很多 issue 了？</p>
<ul>
<li><a href="https://github.com/pypa/pipenv/issues/1554">install&#8217;s `&#8211;keep-outdated` does not seem to be respected</a></li>
<li><a href="https://github.com/pypa/pipenv/issues/3517">pipenv install &#8211;keep-outdated still updates packages</a></li>
</ul>
<p>重点评论：</p>
<div id="attachment_2545" style="width: 730px" class="wp-caption alignnone"><img class="wp-image-2545 size-full" src="http://greyli.com/wp-content/uploads/2019/08/pipenv-comments1.png" alt="" width="720" height="963" srcset="https://greyli.com/wp-content/uploads/2019/08/pipenv-comments1.png 720w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments1-112x150.png 112w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments1-224x300.png 224w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments1-624x835.png 624w" sizes="(max-width: 720px) 100vw, 720px" /><p class="wp-caption-text"><a href="https://github.com/pypa/pipenv/issues/1554#issuecomment-370850330">https://github.com/pypa/pipenv/issues/1554#issuecomment-370850330</a></p></div>
<p>Kenneth Reitz 先是说 lockfile 只要是过期了就总是会被重新生成（这是什么逻辑？），接着又说用 pipenv update depname，但其他人都回复不起作用（我下面会进行单独测试）。</p>
<p>接着，看到其他评论提到用 &#8211;selective-upgrade 选项：</p>
<pre class="">$ pipenv install --help
...
--selective-upgrade Update specified packages.</pre>
<p>我又继续使用 &#8211;selective-upgrade 选项：</p>
<pre class="">$ pipenv install --selective-upgrade flask-avatars</pre>
<p>仍然会更新所有依赖……</p>
<p>对了，顺便还测试了这个命令，依然没用：</p>
<pre class="">$ pipenv install --keep-outdated --selective-upgrade flask-avatars</pre>
<p>除了安装某个包会导致所有依赖版本被更新，Pipenv 在解决依赖的冲突上面也有一些不足，比如执行下面的安装命令（具体见 <a href="https://github.com/sdispater/poetry#dependency-resolution">Poetry README</a>）：</p>
<pre class="">$ pipenv install oslo.utils==1.4.0</pre>
<p>会提示无法安装成功：</p>
<pre class="">ERROR: ERROR: Could not find a version that matches pbr!=0.7,!=2.1.0,&lt;1.0,&gt;=0.6,&gt;=2.0.0</pre>
<h2>更新包</h2>
<p>假设我想更新 Bluelog 这个项目用的 Flask 版本（从 1.0.2 更新到最新的 1.1.1）。查了文档，找到了 update 命令（<a href="https://docs.pipenv.org/en/latest/basics/#example-pipenv-upgrade-workflow">https://docs.pipenv.org/en/latest/basics/#example-pipenv-upgrade-workflow</a>），于是我执行下面的命令：</p>
<pre class="">$ pipenv update flask
Locking [dev-packages] dependencies…
Success!
Locking [packages] dependencies…
Success!
Updated Pipfile.lock (fd55e3)!
Installing dependencies from Pipfile.lock (fd55e3)…
================================ 26/26 - 00:00:12
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!</pre>
<p>突然看到最后一行赫然写着「All dependencies are now up-to-date!」，我以为是搞错了，赶紧看了下 Pipfile.lock，WTF，为什么我所有的依赖（包括和 Flask 完全不相关的）又都被更新了？<br />
依然，已经有很多相关 issue：</p>
<ul>
<li><a href="https://github.com/pypa/pipenv/issues/2665">pipenv update &lt;pkg&gt; updates all packages and not just the selected</a></li>
<li><a href="https://github.com/pypa/pipenv/issues/966">Updating only one locked dependency</a></li>
</ul>
<p>重点评论：</p>
<div id="attachment_2546" style="width: 625px" class="wp-caption alignnone"><img class="wp-image-2546 size-large" src="http://greyli.com/wp-content/uploads/2019/08/pipenv-comments2-615x1024.png" alt="" width="615" height="1024" srcset="https://greyli.com/wp-content/uploads/2019/08/pipenv-comments2-615x1024.png 615w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments2-90x150.png 90w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments2-180x300.png 180w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments2-624x1039.png 624w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments2.png 720w" sizes="(max-width: 615px) 100vw, 615px" /><p class="wp-caption-text"><a href="https://github.com/pypa/pipenv/issues/966#issuecomment-346204439">https://github.com/pypa/pipenv/issues/966#issuecomment-346204439</a></p></div>
<p>如果这个 issue 没有被锁定，这一句「I have no idea.」下面的图标不知道还会被点多少次。我猜 Kenneth Reitz 对这个 issue 让多少人头疼也没有 idea。</p>
<p>继续搜索，查文档，发现 update 命令也有 &#8211;keep-outdated 和 &#8211;selective-upgrade 两个选项：</p>
<pre class="">$ pipenv update --help 
... 
--selective-upgrade Update specified packages.
--keep-outdated Keep out-dated dependencies from being updated in
Pipfile.lock. [env var: PIPENV_KEEP_OUTDATED]</pre>
<p>先来试下 &#8211;keep-outdated：</p>
<pre class="">$ pipenv update --keep-outdated flask</pre>
<p>no luck，还是更新了所有依赖。继续试一下 &#8211;selective-upgrade：</p>
<pre class="">$ pipenv update --selective-upgrade flask</pre>
<p>依然没用，仍然会更新所有依赖……</p>
<p>继续查 issue，发现下面这些：</p>
<ul>
<li><a href="https://github.com/pypa/pipenv/issues/2412">Upgrading a dependency with &#8211;selective-upgrade doesn&#8217;t seem to work with 2018.6.25</a></li>
<li><a style="font-size: 1rem;" href="https://github.com/pypa/pipenv/issues/3461">Confusion: &#8211;keep-outdated &#8211;selective-upgrade</a></li>
</ul>
<p>在 #3461 里发现了下面这个评论：</p>
<div id="attachment_2547" style="width: 730px" class="wp-caption alignnone"><img class="wp-image-2547 size-full" src="http://greyli.com/wp-content/uploads/2019/08/pipenv-comments3.png" alt="" width="720" height="132" srcset="https://greyli.com/wp-content/uploads/2019/08/pipenv-comments3.png 720w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments3-150x28.png 150w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments3-300x55.png 300w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments3-624x114.png 624w" sizes="(max-width: 720px) 100vw, 720px" /><p class="wp-caption-text"><a href="https://github.com/pypa/pipenv/issues/3461#issuecomment-455740272">https://github.com/pypa/pipenv/issues/3461#issuecomment-455740272</a></p></div>
<p>（因为 Frost Ming 是国内的同学，也是核心维护者，说明一下，这里无意冒犯，引用这个评论只是想说明 Pipenv 现在的开发状态。）</p>
<p>这段评论的重点是「In fact, the package name passed as argument is not used at all.」。</p>
<p>也就是说，pipenv update 实际上是不接受包名称参数的。这在下面这个评论也得到了印证：</p>
<div id="attachment_2548" style="width: 730px" class="wp-caption alignnone"><img class="wp-image-2548 size-full" src="http://greyli.com/wp-content/uploads/2019/08/pipenv-comments4.png" alt="" width="720" height="569" srcset="https://greyli.com/wp-content/uploads/2019/08/pipenv-comments4.png 720w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments4-150x119.png 150w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments4-300x237.png 300w, https://greyli.com/wp-content/uploads/2019/08/pipenv-comments4-624x493.png 624w" sizes="(max-width: 720px) 100vw, 720px" /><p class="wp-caption-text"><a href="https://github.com/pypa/pipenv/issues/3461#issuecomment-493847853">https://github.com/pypa/pipenv/issues/3461#issuecomment-493847853</a></p></div>
<blockquote>
<p>Here is the important caveat:pipenv updatealwaystargets every package in your lockfile, without exception. It does not accept arguments.</p>
</blockquote>
<p>一个还没实现的功能就写到文档里了？这真的不是开玩笑吗？不仅是写到了文档里，还写到了命令行帮助文档里：</p>
<pre class="">$ pipenv update --help
Usage: pipenv update [OPTIONS] [PACKAGES]...</pre>
<p>类似下面的场景：</p>
<ul>
<li>用户：怎么运行程序呢？好，查下文档，文档里说「执行 run 命令就可以运行程序」。哎？怎么没用？</li>
<li>开发者：哦，这个功能还没实现，先写出来让你练练手。</li>
</ul>
<p>最终的结果就是，如果你想更新一个包，那就只能手动把更新版本的包版本和 hash 编辑到 Pipfile.lock 里。这么做实在是太蠢了。</p>
<h2>卸载包</h2>
<p>假设我决定不再使用 Gunicorn，需要卸载它，在文档里查到 pipenv uninstall 命令<span data-offset-key="asi8m-0-0">（</span><a class="Link ztext-link" href="https://docs.pipenv.org/en/latest/basics/#pipenv-uninstall" target="_blank" rel="noopener" data-offset-key="asi8m-1-0" data-editable="true"><span data-offset-key="asi8m-1-0">https://docs.pipenv.org/en/latest/basics/#pipenv-uninstall</span></a><span data-offset-key="asi8m-2-0">），</span>于是执行下面的命令：</p>
<pre class="">$ pipenv uninstall gunicorn</pre>
<p>结果呢？为什么我所有的依赖又都被更新了！？好，我已经习惯了。看命令行帮助文档，同样有 &#8211;keep-outdated 命令：</p>
<pre class="">$ pipenv uninstall --help
...
--keep-outdated Keep out-dated dependencies from being updated in
Pipfile.lock. [env var: PIPENV_KEEP_OUTDATED] </pre>
<p>再试一下，虽然我已经不抱期待了：</p>
<pre class="">$ pipenv uninstall --keep-outdated gunicorn</pre>
<p>顺便说一句，卸载的另一个问题是，当你卸载一个包的时候，只会卸载这个包本身，而这个包引入的相关依赖都会被保留，需要手动使用 clean 或 sync 命令修正（参考 <a href="https://github.com/sdispater/poetry#dependency-resolution">Poetry README</a>）。</p>
<h2>不要使用 Pipenv（至少是现在）</h2>
<p>当然，Pipenv 一直在改进。比如 Windows 支持，Lock 很慢的问题，都有过很多的优化（暂且不提没优化之前的痛苦经历）。</p>
<p>但是，种种证据都在表明，这其实是一个半成品。<a href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/">承诺了很多，兑现的却很少</a>。或许过一段时间等它真正成熟了，能够保证基本使用流程，并且可以修改哪些反人类的设定以后再考虑用它（我怀疑这一条是否能实现，除非完全「去 Kenneth Reitz 化」，并且有一个核心维护者能够来推动执行）。</p>
<p>现在，请不要使用它。</p>
<p>我很抱歉在《Flask Web 开发实战》以及文章《<a href="http://greyli.com/pipenv-new-python-dependency-management-tool/">Pipenv：新一代Python项目环境与依赖管理工具</a>》中，推动更多人用它，给大家带来潜在的麻烦。我计划了一些补救措施，会逐一执行：</p>
<ul>
<li>写这篇文章【DONE】</li>
<li>给《Flask Web 开发实战》的五个实例项目追加 requirements.txt 文件，并在 README 中添加说明。</li>
<li>如果《Flask Web 开发实战》能出第二版，修改所有 Python 包安装命令，去掉所有 Pipenv 相关介绍。</li>
<li>写文章介绍替代的几种工具和用法，包括原有的 virtualenv+pip、virtualenvwrapper、Poetry 等。</li>
<li>在 PyCon China 2019 的闪电演讲《<a href="http://greyli.com/pyconchina-2019-lighting-talk-venv">Python 虚拟环境和依赖管理工具大乱斗</a>》里提及这个信息。</li>
<li>以后写推荐文章时对这种带着强烈个人风格的项目保持警惕，并对 Kenneth Reitz 这个名字相关的东西保持警惕。</li>
</ul>
<p>你可以选择用回 virtualenv+pip（+virtualenvwrapper），或是尝试新工具，我会在下一篇文章介绍主要替代品 Poetry 的基本用法。</p>
<h2>相关文章</h2>
<ul class="public-DraftStyleDefault-ul" data-offset-key="f8r7e-0-0">
<li class="Editable-styled public-DraftStyleDefault-unorderedListItem public-DraftStyleDefault-reset public-DraftStyleDefault-depth0 public-DraftStyleDefault-listLTR" data-block="true" data-editor="d4kdt" data-offset-key="f8r7e-0-0">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="f8r7e-0-0"><a class="Link ztext-link" href="https://zhuanlan.zhihu.com/p/80695813" target="_blank" rel="noopener" data-offset-key="f8r7e-0-0" data-editable="true"><span data-offset-key="f8r7e-0-0">Pipenv 有什么问题</span></a></div>
</li>
<li class="Editable-styled public-DraftStyleDefault-unorderedListItem public-DraftStyleDefault-depth0 public-DraftStyleDefault-listLTR" data-block="true" data-editor="d4kdt" data-offset-key="amab1-0-0">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="amab1-0-0"><a class="Link ztext-link" href="https://zhuanlan.zhihu.com/p/80683249" target="_blank" rel="noopener" data-offset-key="amab1-0-0" data-editable="true"><span data-offset-key="amab1-0-0">也谈「不要用 Pipenv」</span></a></div>
</li>
<li class="Editable-styled public-DraftStyleDefault-unorderedListItem public-DraftStyleDefault-depth0 public-DraftStyleDefault-listLTR" data-block="true" data-editor="d4kdt" data-offset-key="fe43v-0-0">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="fe43v-0-0"><a class="Link ztext-link" href="https://zhuanlan.zhihu.com/p/80727727" target="_blank" rel="noopener" data-offset-key="fe43v-0-0" data-editable="true"><span data-offset-key="fe43v-0-0">回应《也谈「不要用 Pipenv」》</span></a></div>
</li>
<li class="Editable-styled public-DraftStyleDefault-unorderedListItem public-DraftStyleDefault-depth0 public-DraftStyleDefault-listLTR" data-block="true" data-editor="d4kdt" data-offset-key="b5b6u-0-0">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="b5b6u-0-0"><a class="Link ztext-link" href="https://zhuanlan.zhihu.com/p/80826465" target="_blank" rel="noopener" data-offset-key="b5b6u-0-0" data-editable="true"><span data-offset-key="b5b6u-0-0">回应「回应《也谈「不要用 Pipenv」》」</span></a></div>
</li>
</ul>
<p>（1）</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/do-not-use-pipenv/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>从国内的 PyPI 镜像（源）安装 Python 包</title>
		<link>https://greyli.com/set-pypi-mirror/</link>
		<comments>https://greyli.com/set-pypi-mirror/#respond</comments>
		<pubDate>Wed, 27 Feb 2019 13:48:52 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[pip]]></category>
		<category><![CDATA[Pipenv]]></category>
		<category><![CDATA[Poetry]]></category>
		<category><![CDATA[PyPI]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2365</guid>
		<description><![CDATA[不论是使用 pip，还是 Pipenv、Poetry等工具，安装 Python 包的时候会默认从官方的 PyP [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>不论是使用 pip，还是 Pipenv、Poetry等工具，安装 Python 包的时候会默认从官方的 PyPI 源下载文件，速度比较慢。国内的一些公司和机构提供了 PyPI 镜像源（mirror source），你可以设置让这些工具从国内的镜像源安装 Python 包，以便提高下载速度。</p>
<p>官方 PyPI 源的 URL 为 https://pypi.org/simple （旧的 URL 为 https://pypi.python.org/simple ），下面我们将以豆瓣提供的镜像源为例（URL 为 https://pypi.doubanio.com/simple/），介绍不同工具更改 PyPI 镜像源的方法：</p>
<h2>pip</h2>
<p>临时设置可以通过 -i 选项：</p>
<pre>$ pip install -i https://pypi.doubanio.com/simple/ flask</pre>
<p>全局设置有不同的层级和文件位置，以用户全局（per-user）为例，在 Linux &amp; macOS 中，配置需要写到 ~/.pip/pip.conf 文件中；Windows 中，配置文件位置为 %HOMEPATH%\pip\pip.ini，%HOMEPATH% 即你的用户文件夹，一般为“\Users\&lt;你的用户名&gt;”，具体值可以使用 echo %HOMEPATH% 命令查看。</p>
<p>通常你需要手动创建对应的目录和文件，然后写入下面的内容：</p>
<pre>[global]
index-url = https://pypi.doubanio.com/simple
[install]
trusted-host = pypi.doubanio.com
</pre>
<p>附注：按照 pip 文档，上面的配置文件位置是旧（legacy）的配置方式，但是因为比较方便设置，这里沿用了。新的建议是 Linux &amp; macOS 放到 $HOME/.config/pip/pip.conf，Windows 则放到 %APPDATA%\pip\pip.ini。具体可以访问 <a href="https://pip.pypa.io/en/stable/user_guide/#config-file、">pip 文档配置部分</a>查看。</p>
<h2>Pipenv</h2>
<p>类似 pip 的 -i （&#8211;index-url）选项，你可以使用 &#8211;pypi-mirror 临时设置镜像源地址：</p>
<pre>$ pipenv install --pypi-mirror https://pypi.doubanio.com/simple flask</pre>
<p>如果想对项目全局（per-project）设置，可以修改 Pipfile 中 [[source]] 小节：</p>
<pre>[[source]]

url = "https://pypi.doubanio.com/simple"
verify_ssl = true
name = "douban"</pre>
<p>另外一种方式是使用环境变量 PIPENV_PYPI_MIRROR 设置（Windows 系统使用 set 命令）：</p>
<pre>$ export PIPENV_PYPI_MIRROR=https://pypi.doubanio.com/simple</pre>
<p>你可以通过把这个环境变量的设置语句写入到终端的配置文件里实现“永久”设置，Linux &amp; macOS 可参考<a href="https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables">这里</a>，Windows 可参考<a href="https://www.howtogeek.com/51807/how-to-create-and-use-global-system-environment-variables/">这里</a>。</p>
<h2>Poetry / Flit</h2>
<p>因为 Poetry，Flit 这些工具遵循 PEP 518 创建了一个 pyproject.toml 文件来替代 setup.py、Pipfile 等配置文件，所以我们可以在这个文件里更改 PyPI 源。</p>
<p>使用 Poetry 时，在 pyproject.toml 末尾添加下面的内容来设置自定义镜像源：</p>
<pre>[[tool.poetry.source]]
name = "douban"
url = "https://pypi.doubanio.com/simple/"</pre>
<p>目前暂时没有其他方式，未来或许会为 poetry add 命令添加一个相关的设置选项。</p>
<p>同样的，Flit 大概要添加下面这些内容（未测试）：</p>
<pre>[[tool.flit.source]]
name = "douban"
url = "https://pypi.doubanio.com/simple/"</pre>
<h2>常用的国内 PyPI 镜像列表</h2>
<ul>
<li>豆瓣 https://pypi.doubanio.com/simple/</li>
<li>网易 https://mirrors.163.com/pypi/simple/</li>
<li>阿里云 https://mirrors.aliyun.com/pypi/simple/</li>
<li>清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/</li>
</ul>
<p>顺便提一下，使用镜像源需要注意一个问题：包的版本可能不会及时更新，遇到这种情况可以通过临时换回官方源解决。</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/set-pypi-mirror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《Flask 入门教程》第 1 章：准备工作</title>
		<link>https://greyli.com/flask-tutorial-chapter-1/</link>
		<comments>https://greyli.com/flask-tutorial-chapter-1/#respond</comments>
		<pubDate>Tue, 04 Dec 2018 06:42:23 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[Pipenv]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1989</guid>
		<description><![CDATA[在通过这本书学习 Flask 开发前，我假设你了解了 Python 和 HTML 的基础知识。你的 Pytho [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
	在通过这本书学习 Flask 开发前，我假设你了解了 Python 和 HTML 的基础知识。你的 Python 版本可以是 2.7，也可以是 3.3 及以上版本。电脑的操作系统可以是 Windows，也可以是 macOS 或 Linux。
</p>
<h2>
	安装编辑器和浏览器<br />
</h2>
<p>
	对于编辑器来说，每个人都有不同的偏好，你可以自由选择。可以选择功能丰富的IDE（集成开发环境），比如&nbsp;<a href="https://www.jetbrains.com/pycharm/" rel="nofollow">PyCharm</a>；也可以选择相对轻量的编辑器，比如&nbsp;<a href="https://atom.io/" rel="nofollow">Atom</a>&nbsp;或&nbsp;<a href="https://www.sublimetext.com/" rel="nofollow">Sublime Text</a>。浏览器建议使用&nbsp;<a href="https://www.mozilla.org/en-US/firefox/new/" rel="nofollow">Firefox</a>&nbsp;或&nbsp;<a href="https://www.google.com/chrome/" rel="nofollow">Chrome</a>。
</p>
<h2>
	使用命令行<br />
</h2>
<p>
	在本书中，你需要使用命令行窗口来执行许多操作。你可以使用 Windows 下的 cmd.exe，或是 macOS 和 Linux 下的终端（Terminal）。下面我们执行一个最简单的&nbsp;<code>whoami</code>&nbsp;命令（即 Who Am I？）：
</p>
<pre>
$ whoami
greyli</pre>
<p>
	这个命令会打印出当前计算机用户的名称。其他常用的命令还有&nbsp;<code>cd</code>&nbsp;命令，用来切换目录（<strong>c</strong>hange&nbsp;<strong>d</strong>irectory）；<code>mkdir</code>&nbsp;命令，用来创建目录（<strong>m</strong>a<strong>k</strong>e&nbsp;<strong>dir</strong>ectory）。在不同的操作系统上，执行某个操作的命令可能会有所不同，在必要的地方，书里会进行提示。
</p>
<p>
	我们先来为我们的程序创建一个文件夹：
</p>
<pre>
$ mkdir watchlist
$ cd watchlist</pre>
<p>
	除非特别说明，从现在开始，本书假设你的工作目录将是在项目的根目录，即 watchlist/ 目录。
</p>
<p>
	为了确保你已经正确安装了 Python，可以执行下面的命令测试是否有报错：
</p>
<pre>
$ python --version
Python 2.7.11</pre>
<p>
	对于 Windows 用户，请使用&nbsp;<a href="http://cmder.net/" rel="nofollow">cmder</a>（一个基于&nbsp;<a href="https://conemu.github.io/" rel="nofollow">ConEmu</a>&nbsp;实现的终端模拟器） 来代替系统自带的 cmd.exe，或是使用安装 Git for Windows 后（下一节）附带的 Git Bash。cmder 集成了 Git Bash，支持一些在 Linux 或 macOS 下才能使用的命令（程序），比如 ls、cat、nano、ssh 等，这些命令我们在后面会用到。
</p>
<h2>
	使用 Git<br />
</h2>
<p>
	<a href="https://git-scm.com/" rel="nofollow">Git</a>&nbsp;是一个流行的版本控制工具，我们可以用它来记录程序源码和文件的变动情况，或是在编程时进行多人协作，你可以把它看做一个优雅的代码变动备份工具。
</p>
<p>
	如果你还不熟悉 Git 也没关系，可以先按照书中的命令去做，有时间再去了解原理。现在要做的第一件事就是在你的电脑上<a href="https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git" rel="nofollow">安装 Git</a>&nbsp;（可以执行&nbsp;<code>git --help</code>&nbsp;命令检查是否已经安装，没有提示&ldquo;命令未找到（Command not found）&rdquo;则表示已安装）。
</p>
<p>
	安装后可以在命令行先使用使用下面的命令查看版本，没有报错则表示已正确安装：
</p>
<pre>
$ git --version
git version 2.17.1</pre>
<p>
	为了让 Git 知道你是谁，以便在提交代码到版本仓库的时候进行记录，使用下面的命令设置你的信息：
</p>
<pre>
$ git config --global user.name &quot;Grey Li&quot;  # 替换成你的名字
$ git config --global user.email &quot;withlihui@gmail.com&quot;  # 替换成你的邮箱地址</pre>
<p>
	现在为我们的项目文件夹创建一个 Git 仓库，这会在我们的项目根目录创建一个 .git 文件夹：
</p>
<pre>
$ git init
Initialized empty Git repository in ~/watchlist/.git/</pre>
<p>
	Git 默认会追踪项目文件夹（或者说代码仓库）里所有文件的变化，但是有些无关紧要的文件不需要记录变化，我们在项目根目录创建一个 .gitignore 文件，在文件中写入忽略文件的规则。因为文件内容比较简单，我们直接在命令使用 nano 来创建：
</p>
<pre>
$ nano .gitignore</pre>
<p>
	在 nano 编辑界面写入常见的可忽略文件规则
</p>
<pre>
*.pyc
*~
__pycache__
.DS_Store</pre>
<p>
	使用 Control + O 和 Enter 键保存，然后按下 Control + X 键退出。在后续章节，对于简单的文件，都会使用 nano 创建，这部分操作你也可以使用编辑器来完成。
</p>
<h2>
	将程序托管到 GitHub（可选）<br />
</h2>
<p>
	这一步是可选的，将程序托管到 GitHub、GitLab 或是 BitBucket 等平台上，可以更方便的备份、协作和部署。这些托管平台作为 Git 服务器，你可以为本地仓库创建远程仓库。
</p>
<p>
	首先要注册一个 GitHub 账户，点击访问<a href="https://github.com/join">注册页面</a>，根据指示完成注册流程。登录备用。
</p>
<h3>
	设置 SSH 密钥<br />
</h3>
<p>
	一般情况下，当推送本地改动到远程仓库时，需要输入用户名和密码。因为传输通常是通过 SSH 加密，所以可以通过设置 SSH 密钥来省去验证账号的步骤。
</p>
<p>
	首先使用下面的命令检查是否已经创建了 SSH 密钥：
</p>
<pre>
$ cat ~/.ssh/id_rsa.pub</pre>
<p>
	如果显示&ldquo;No such file or directory&rdquo;，就使用下面的命令生成 SSH 密钥对，否则复制输出的值备用：
</p>
<pre>
$ ssh-keygen</pre>
<p>
	一路按下 Enter 采用默认值，最后会在用户根目录创建一个 .ssh 文件夹，其中包含两个文件，id_rsa 和 id_rsa.pub，前者是私钥，不能泄露出去，后者是公钥，用于认证身份，就是我们要保存到 GitHub 上的密钥值。再次使用前面提到的命令获得文件内容：
</p>
<pre>
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3Nza...省略 N 个字符...3aph book@greyli</pre>
<p>
	选中并复制输出的内容，访问 GitHub 的&nbsp;<a href="https://github.com/settings/keys">SSH 设置页面</a>（导航栏头像 &#8211; Settings &#8211; SSH and GPG keys），点击 New SSH key 按钮，将复制的内容粘贴到 Key 输入框里，再填一个标题，比如&ldquo;My PC&rdquo;，最后点击&ldquo;Add SSH key&rdquo;按钮保存。
</p>
<h3>
	创建远程仓库<br />
</h3>
<p>
	访问<a href="https://github.com/new">新建仓库页面</a>（导航栏&ldquo;+&rdquo; &#8211; New repository），在&ldquo;Repository name&rdquo;处填写仓库名称，这里填&ldquo;watchlist&rdquo;即可，接着选择仓库类型（公开或私有）等选项，最后点击&ldquo;Create repository&rdquo;按钮创建仓库。
</p>
<p>
	因为我们已经提前创建了本地仓库，所以需要指定仓库的远程仓库地址（如果还没有创建，则可以直接将远程仓库克隆到本地）：
</p>
<pre>
$ git remote add origin git@github.com:greyli/watchlist.git  # 注意更换地址中的用户名</pre>
<p>
	这会为本地仓库关联一个名为&ldquo;origin&rdquo;的远程仓库，<strong>注意将仓库地址中的&ldquo;greyli&rdquo;换成你的 GitHub 用户名</strong>。
</p>
<h2>
	创建虚拟环境<br />
</h2>
<p>
	虚拟环境是独立于 Python 全局环境的 Python 解释器环境，使用它的好处如下：
</p>
<ul>
<li>
		保持全局环境的干净
	</li>
<li>
		指定不同的依赖版本
	</li>
<li>
		方便记录和管理依赖
	</li>
</ul>
<p>
	我们将使用 Pipenv 来创建和管理虚拟环境、以及在虚拟环境中安装和卸载依赖包。它集成了 pip 和 virtualenv，可以替代这两个工具的惯常用法。另外，它还集成了 Pipfile，它是新的依赖记录标准，使用 Pipfile 文件记录项目依赖，使用 Pipfile.lock 文件记录固定版本的依赖列表。这两个文件替代了手动通过 requirements.txt 文件记录依赖的方式。我们首先使用 pip 安装 Pipenv，Windows 系统使用下面的命令：
</p>
<pre>
$ pip install pipenv</pre>
<p>
	Linux 和 macOS 使用下面的命令：
</p>
<pre>
$ sudo -H pip install pipenv</pre>
<p>
	使用 Pipenv 创建虚拟环境非常简单，使用&nbsp;<code>pipenv install</code>&nbsp;命令即可为当前项目创建一个虚拟环境：
</p>
<pre>
$ pipenv install</pre>
<p>
	这个命令执行的过程包含下面的行为：
</p>
<ul>
<li>
		为当前目录创建一个 Python 解释器环境，按照 pip、setuptool、virtualenv 等工具库。
	</li>
<li>
		如果当前目录有 Pipfile 文件或 requirements.txt 文件，那么从中读取依赖列表并安装。
	</li>
<li>
		如果没有发现 Pipfile 文件，就自动创建。
	</li>
</ul>
<p>
	创建虚拟环境后，我们可以使用&nbsp;<code>pipenv shell</code>&nbsp;命令来激活虚拟环境，如下所示：
</p>
<pre>
$ pipenv shell</pre>
<h2>
	安装 Flask<br />
</h2>
<p>
	无论是否已经激活虚拟环境，你都可以使用下面的命令来安装 Flask：
</p>
<pre>
$ pipenv install flask</pre>
<p>
	这会把 Flask 以及相关的一些依赖包安装到对应的虚拟环境，同时 Pipenv 会自动更新依赖文件中。
</p>
<h2>
	本章小结<br />
</h2>
<p>
	当你进行到这里，就意味这我们已经做好学习和开发Flask程序的全部准备了。使用&nbsp;<code>git status</code>&nbsp;命令可以查看当前仓库的文件变动状态：
</p>
<pre>
$ git status</pre>
<p>
	下面让我们将文件改动提交进 Git 仓库，并推送到在 GitHub 上创建的远程仓库：
</p>
<pre>
$ git add .
$ git commit -m &quot;I&#39;m ready!&quot;
$ git push -u origin master # 如果你没有把仓库托管到 GitHub，则跳过这条命令，后面章节亦同</pre>
<p>
	这里最后一行命令添加了&nbsp;<code>-u</code>&nbsp;参数，会将推送的目标仓库和分支设为默认值，后续的推送直接使用&nbsp;<code>git push</code>&nbsp;命令即可。在 GitHub 上，你可以通过&nbsp;<a href="https://github.com/greyli/watchlist">https://github.com/你的用户名/watchlist</a>&nbsp;查看你的仓库内容。
</p>
<p>
	<strong>提示</strong> 你可以在 GitHub 上查看本书示例程序的对应 commit：<a href="https://github.com/greyli/watchlist/commit/1b6fe4ae117c2b964f247d8b12d79753ea69406f" spellcheck="false">1b6fe4a</a>。
</p>
<h2>
	进阶提示<br />
</h2>
<ul>
<li>
		阅读 MDN 的&nbsp;<a href="https://developer.mozilla.org/zh-CN/docs/learn" rel="nofollow">《Web 入门教程》</a>（了解 HTML、CSS、JavaScript）。
	</li>
<li>
		阅读短教程<a href="http://rogerdudler.github.io/git-guide/index.zh.html" rel="nofollow">《Git 简明指南》</a>。
	</li>
<li>
		阅读文章<a href="https://zhuanlan.zhihu.com/p/37581807" rel="nofollow">《Pipenv：新一代Python项目环境与依赖管理工具》</a>或&nbsp;<a href="https://pipenv.readthedocs.io/en/latest/" rel="nofollow">Pipenv 官方文档</a>。
	</li>
<li>
		如果你打算开源你的程序，在项目根目录中添加一个 README.md （自述文件）和 LICENSE （授权声明）是很有必要的。详情可以访问&nbsp;<a href="https://opensource.guide/" rel="nofollow">Open Source Guides</a>&nbsp;了解。
	</li>
<li>
		在安装 Pipenv 时，你也可以使用&nbsp;<code>--user</code>&nbsp;选项进行用户安装（即&nbsp;<code>pip install --user pipenv</code>）。用户安装可以避免破坏全局的包，而且可以避免对不可信的包使用 sudo pip 导致的潜在安全问题。详情见&nbsp;<a href="https://docs.pipenv.org/install/#installing-pipenv" rel="nofollow">Pipenv 文档安装章节</a>。
	</li>
<li>
		本书主页 &amp; 相关资源索引：<a data-editable="true" data-offset-key="eh3t9-1-0" href="http://helloflask.com/tutorial" target="_blank">http://helloflask.com/tutorial</a>。
	</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/flask-tutorial-chapter-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pipenv使用时出现TypeError、ResourceWarning、或ImportError错误的解决方法</title>
		<link>https://greyli.com/pipenv-install-got-typeerror-resourcewarning-or-importerror/</link>
		<comments>https://greyli.com/pipenv-install-got-typeerror-resourcewarning-or-importerror/#comments</comments>
		<pubDate>Tue, 09 Oct 2018 04:18:11 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[pip]]></category>
		<category><![CDATA[Pipenv]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1953</guid>
		<description><![CDATA[10月16日更新 Pipenv 10.13 据说已经修复所有相关问题，可以尝试更新： $ python3 -m [&#8230;]]]></description>
				<content:encoded><![CDATA[<h2><b>10月16日更新</b></h2>
<p>Pipenv 10.13 据说已经修复所有相关问题，可以尝试更新：</p>
<div>
<pre class="">$ python3 -m pip install --upgrade pip 
$ python3 -m pip install --upgrade pipenv</pre>
</div>
<h2><b>Pipenv 2018.7.1</b></h2>
<p>Pipenv 2018.7.1版本和最新版本的pip不兼容，根据不同的命令和环境，会出现下面的异常：</p>
<ul>
<li>TypeError: &#8216;module&#8217; object is not callable （pipenv install）</li>
<li>ResourceWarning: Implicitly cleaning up &#8230; （pipenv install）</li>
<li>ImportError: cannot import name &#8216;get_installed_distributions&#8217; （pipenv graph）</li>
</ul>
<p>目前可以临时通过降级pip来解决：</p>
<pre class="">$ python3 -m pip install pip==10.0.1</pre>
<p>如果你使用Python2，则使用下面的命令：</p>
<pre class="">$ python -m pip install pip==10.0.1</pre>
<p>相关issue见<a href="https://github.com/pypa/pipenv/issues/2924">#2924</a>、<a href="https://github.com/pypa/pipenv/issues/2925">#2925</a>、<a href="https://github.com/pypa/pipenv/issues/2944">#2944</a>。</p>
<h2><b>Pipenv 2018.10.9</b></h2>
<p>如果使用的是最新的2018.10.9版本，那么在Python 2.7下会出现下列异常：</p>
<ul>
<li>ImportError: No module named weakref</li>
<li>ImportError: No module named shutil_get_terminal_size</li>
</ul>
<p>可以临时通过手动安装这两个库来解决：</p>
<div>
<pre class="">$ pip install backports.weakref backports.shutil_get_terminal_size</pre>
</div>
<h2>Pipenv 修复版本发布后</h2>
<p>等到Pipenv发布新版本修复了这些问题后，可以通过下面的命令升级pipenv和pip：</p>
<pre class="">$ python3 -m pip install --upgrade pip
$ python3 -m pip install --upgrade pipenv</pre>
<p>如果你在阅读<a href="http://helloflask.com/book">《Flask Web开发实战》</a>时还遇到了其他问题，可以先到<a href="https://github.com/greyli/helloflask/blob/master/faq/faq.md">FAQ</a>中查看，没有解决可以在<a href="https://github.com/greyli/helloflask">helloflask仓库</a>创建issue。</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/pipenv-install-got-typeerror-resourcewarning-or-importerror/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
