版权声明:勤学 修德 明辨 笃实 - CSDN周雄伟 https://blog.csdn.net/ebzxw/article/details/80701613 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f1a9c33fcd.css">
<div class="htmledit_views" id="content_views">
<p>已有环境:python3.7.1<br></p><p>anaconda隔离管理多个环境,互不影响。这里,在anaconda中安装最新的python3.6.5 版本。</p><p>linux环境下使用anaconda安装tensorflow步骤见:<a href="https://blog.csdn.net/ebzxw/article/details/80693152" rel="nofollow" target="_blank">https://blog.csdn.net/ebzxw/article/details/80693152</a></p><p><strong>一. 安装anaconda</strong></p><p>1. 下载地址: <a href="https://www.anaconda.com/download/#windows" rel="nofollow" target="_blank">https://www.anaconda.com/download/#windows</a></p><p><img src="https://img-blog.csdn.net/20180615101323644?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>2. 执行下载文件 Anaconda3-5.2.0-<a href="https://www.baidu.com/s?wd=Windows&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd" target="_blank">Windows</a>-x86_64.exe, 默认配置安装。</p><p><span style="background-color:rgb(255,255,255);">3. 检查安装结果。进入到windows中的命令模式:</span></p><p style="background-color:rgb(255,255,255);">(1)检测anaconda环境是否安装成功:conda --version</p><p style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615193442552?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p style="background-color:rgb(255,255,255);">(2)检测目前安装了哪些环境变量:conda info --envs</p><p style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615193459890?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p style="background-color:rgb(255,255,255);">(3) 查看当前有哪些可以使用的tensorflow版本:<strong>conda search --full -name tensorflow</strong></p><p><img src="https://img-blog.csdn.net/20180615193553707?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""></p><p><span style="background-color:rgb(255,255,255);">(4) 查看tensorflow包信息及依赖关系:<strong>conda info tensorflow</strong></span><br></p><p><span style="background-color:rgb(255,255,255);"><strong><img src="https://img-blog.csdn.net/2018061521501133?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></strong></span></p><p><span style="font-weight:bold;">二. 在anaconda中安装tensorflow</span></p><p><span style="background-color:rgb(255,255,255);">1. 进入windows命令模式,创建tfenv环境,安装python3.6: </span><span style="background-color:rgb(255,255,255);"><strong>conda create --name tfenv python=3.6</strong></span></p><div><img src="https://img-blog.csdn.net/20180615220036678?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></div><p><span style="background-color:rgb(255,255,255);">2 . <span style="background-color:rgb(255,255,255);">激活tensflow的tfenv环境: activate tfenv</span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615220551804?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"> 检测tfenv的环境添加到了Anaconda里面:conda info --envs</span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/2018061522080997?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">看到,已经创建成功。</span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">检测当前环境中的python的版本:python --version</span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615221846949?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">退出tfenv的环境:deactivate</span><br></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615222102462?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">3. 在tfenv环境<a href="https://www.baidu.com/s?wd=%E4%B8%AD%E6%AD%A3%E5%BC%8F&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd" target="_blank">中正式</a>安装tensorflow包</span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">1)<span style="background-color:rgb(255,255,255);">激活tensflow的tfenv环境: activate tfenv</span></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">2)pip install --upgrade --ignore-installed tensorflow</span></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615222656314?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615224852188?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">3) 验证功能正常:python 进入代码环境</span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615225105865?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></span></p><pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">hello = tf.constant(<span class="hljs-string">'hello,tf'</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">sess = tf.Session()</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">print(sess.run(hello))</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><img src="https://img-blog.csdn.net/20180615225701383?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);">可以看到, 该环境下 tensorflow 工作正常。</span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="font-weight:700;">三. 安装可能的异常</span><br></span></span></span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"></span></span></span></span></p><p style="background-color:rgb(255,255,255);"><span><span style="color:rgb(255,0,0);">温馨提示:如果你的conda和tensorflow环境都是安装成功的,但是一用测试代码进行跑的时候就出问题了,那么注意,这个原因你由于你在安装tensorflow的时候,是直接在cmd下,而不是在你用conda激活的一个环境,所以导致,tensorflow并没有直接嵌入到conda环境,所以,就导致无法导入模块的一个错误;</span></span></p><p style="background-color:rgb(255,255,255);"><span><span style="color:rgb(255,0,0);">解决方法:(1)只需要在activate tfenv</span></span></p><p style="background-color:rgb(255,255,255);"><span><span style="color:rgb(255,0,0);">(2)然后再使用 <span style="background-color:rgb(255,255,255);">pip install --upgrade --ignore-installed tensorflow </span>命令安装就可以了</span></span></p><p><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="background-color:rgb(255,255,255);"><span style="font-weight:700;background-color:rgb(255,255,255);">四. 将tensorflow嵌入到IDE中</span></span></span></span></span></p><p>这里的关键是配置后,IDE使用的python环境包含tensorflow就可以。</p><p>1. windows操作命令下设置默认python环境</p><p>可通过环境变量的顺序来设置。(这里是之前就有的python3.6.1环境和在anaconda中装的python3.6.5)</p><p><img src="https://img-blog.csdn.net/20180616100818536?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>“系统属性”页面,点击“环境变量” ,选中PATH,点“编辑”</p><p><img src="https://img-blog.csdn.net/20180616100952319?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>选中希望优先执行的python版本路径,“上移”到顶。 这里是把anaconda安装后默认在最上面,改为原来的3.6.1版本了。</p><p><img src="https://img-blog.csdn.net/20180616101110912?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p><strong>结果验证与环境切换:</strong></p><p><img src="https://img-blog.csdn.net/20180616101310565?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p><img src="https://img-blog.csdn.net/20180616101403452?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>2. VSCODE里设置默认python环境 (演示设置为原来python3.6.1)<br></p><p>打开编辑器。 文件 - 首选项 - 设置</p><p>找到“用户工作区设置”,更改 python.pythonPath 配置变量即可。</p><p></p><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas, 'Courier New', monospace;font-size:14px;line-height:19px;white-space:pre;"><div> <span style="color:#9cdcfe;">"python.pythonPath"</span>: <span style="color:#ce9178;">"C:</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">Users</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">user</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">AppData</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">Local</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">Programs</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">python</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">Python36</span><span style="color:#d7ba7d;">\</span><span style="color:#ce9178;">python.exe"</span></div><div><span style="color:#608b4e;">// "python.pythonPath": "C:\Users\user\Anaconda3\python.exe" </span></div><div></div></div>界面如下图:<p><img src="https://img-blog.csdn.net/20180616101653664?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vienh3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><br></p><p>重启vscode即可。</p> </div>
</div>