文章目录
前提
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.bat
和deactivate.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.
这个问题参考: