win10创建py虚拟环境并安装TensorFlow

前提


python3.7已安装,环境变量已配置




测试virtualenv(多余的步骤)

安装virtualenv

(我是小白才装的,实际上不需要装)

直接使用pip3 install安装

在这里插入图片描述
查看virtualenv的版本和位置

virtualenv --version

在这里插入图片描述

创建虚拟环境

使用virtualenv names来创建新的虚拟环境
在这里插入图片描述
查看虚拟环境目录结构
在这里插入图片描述
查看所有的虚拟环境
使用lsvirtualenv
在这里插入图片描述
按网上教程是需要安装virtualenvwrapper
在这里插入图片描述
安装完之后还是不能用
在这里插入图片描述
在网上搜问题所在,发现对于python3.7,应该使用venv模块,不再使用virtualenv

在python3.3之前,需要利用virtualenv等工具来实现python虚拟开发环境的配置,在python3.3中加入了venv模块支持原生创建虚拟环境。但在python3.3版本中venv模块没有内置pip包,在3.4版本后加入了pip包,创立并激活虚拟环境后可直接使用。

删除安装包

在这里插入图片描述
在这里插入图片描述
已经创建的虚拟环境目录,直接删除目录

使用自带的venv创建虚拟环境

先找个地方创建虚拟目录,比如d:/python3venv

在这个文件夹下创建一个demo文件夹测试demo_venv

进入demo_venv目录,使用以下指令创建虚拟目录

# 注意点
python -m venv .

在这里插入图片描述
创建成功之后没有提示,直接进入虚拟目录下看,目录结构如下
在这里插入图片描述

激活虚拟环境

只有激活之后,才算进入该虚拟环境,否则安装包时,依然是安装在全局环境之下

激活的程序在虚拟目录/Scripts下,有activate.batdeactivate.bat

进入./Scripts目录下

执行activate.bat激活,激活之后直接跳到新的cmd页面

在这里插入图片描述

关闭虚拟环境

在这里插入图片描述

测试安装tensorflow

直接使用pip3安装

pip3 install tensorflow

从下图可以看到,这样安装的版本(目前)是tensorflow-2.5.0.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
安装完之后,发现虚拟环境目录下/Scripts和Lib目录下都多了很多文件,说明是安装到虚拟环境中了
在这里插入图片描述
在这里插入图片描述
安装GPU版本:

pip3 install tensorflow-gpu

测试tensorflow是否可用

先使用pip3 show指令查看是否有tensorflow信息

pip3 show tensorflow

在这里插入图片描述
先进入python交互式命令行

import tensorflow as tf
hello = tf.constant('hello tensorflow')
print(hello)

----------------------------------
结果:
tf.Tensor(b'hello tensorflow', shape=(), dtype=string)
import tensorflow as tf
a = tf.constant(1)
b = tf.constant(2)
result = a + b
print(result)

----------------------------------
结果:
tf.Tensor(3, shape=(), dtype=int32)

在这里插入图片描述
说明tensorflow安装完成;

在主python环境中测试

新开一cmd窗口,进入python交互式命令行
提示模块未找到ModuleNotFoundError: No module named 'tensorflow'
说明我们的虚拟环境和主环境是分离的
在这里插入图片描述

pycharm使用刚创建的虚拟环境

新建一个project,修改项目名,选择已存在的解释器,点击后面的三个点
在这里插入图片描述
选择虚拟环境,在解释器后面点三个点,选择刚创建的虚拟环境,进入Scripts目录下找到python.exe,点确认
在这里插入图片描述
然后创建
在这里插入图片描述
目录结构如下图
在这里插入图片描述
新建一py文件,输入以下内容,运行后发现可以正常运行,说明使用创建的虚拟环境没有问题。

import tensorflow as tf

hello = tf.constant("hello tensorflow")

print(hello)

在这里插入图片描述

tensorflow的警告信息

虽然上面测试程序可以运行,但是出现了一些错误

2021-07-15 15:49:54.943112: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-07-15 15:49:54.943282: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-07-15 15:49:58.324183: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2021-07-15 15:49:58.324632: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
tf.Tensor(b'hello tensorflow', shape=(), dtype=string)
2021-07-15 15:49:58.340360: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-LVFJD7Q
2021-07-15 15:49:58.341321: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-LVFJD7Q
2021-07-15 15:49:58.342317: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

这个问题参考:

https://blog.csdn.net/zz_aiytag/article/details/118763928

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值