背景:代码是python3版本编写,则运用python2.7运行会报错,运用python3版本运行又显示没有安装tensorflow。所以运用python3版配置与安装tensorflow。
目的:运用anaconda来进行python3.6安装与配置tensorflow
目录
一、问题描述
二、旧版本解决方法(无用可不看)
2.1 安装python3
2.2 升级旧pip
2.3 为python3安装pip3
三、配置环境
3.1 安装anaconda
3.2 创建环境
3.3 激活环境
3.4 配置tensorflow-gpu
3.5 更新pip
运用python运行程序时,python3的程序,运用python2运行就会报错。
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ python dataset_tool_tf.py --input-dir datasets/part_BSDS300/images/train --out=datasets/part_bsd300.tfrecords
Loading image list from datasets/part_BSDS300/images/train
Traceback (most recent call last):
File "dataset_tool_tf.py", line 94, in
main()
File "dataset_tool_tf.py", line 70, in main
os.makedirs(outdir, exist_ok=True)
TypeError: makedirs() got an unexpected keyword argument 'exist_ok'
出现此错误,原来作者要求版本为python 3.6 ,这样报错的版本为python 2.7.6
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
python3的版本输入的时候,报错为tensorflow配置不对。
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ python3 dataset_tool_tf.py --input-dir datasets/part_BSDS300/images/train --out=datasets/part_bsd300.tfrecords
Traceback (most recent call last):
File "dataset_tool_tf.py", line 12, in
import tensorflow as tf
ImportError: No module named 'tensorflow'
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ python3
Python 3.5.2 (default, May 23 2017, 10:15:40)
[GCC 5.4.1 20160904] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
输入python3,如果没有出现以下信息表示python3没有安装
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ python3
Python 3.5.2 (default, May 23 2017, 10:15:40)
[GCC 5.4.1 20160904] on linux
Type "help", "copyright", "credits" or "license" for more information.
先到官方网站下载python3的安装包
https://www.python.org/downloads/source/ ---例如Python-3.5.2.tar.xz
上传包到服务器
解压 tar -xf Python-3.5.2.tar.xz
编译安装
!!!!注意 注意 ⚠️ 在编译之前需要安装一些必须的依赖,否则当报错的时候还得重新编译
安装必要依赖(至少需要如下两个)
yum install openssl-devel -y
yum install zlib-devel -y
好了现在可以安心的编译咯:
cd Python-3.5.2
./configure --prefix=/opt/Python #安装目录可以自己定义无所谓。
make
make install
编译完成后会在如 /opt/下生成Python的文件夹 ,没错这就是编译完成的python --为了方便之行小伙伴们可以自己定义一个软连接如下:
# ln -s /opt/Python/bin/python3 /usr/bin/python3
这样就可以直接使用python3了。
升级旧版本pip,
sudo pip install --upgrade pip
Installing collected packages: pip
Found existing installation: pip 18.0
Uninstalling pip-18.0:
Successfully uninstalled pip-18.0
Successfully installed pip-19.0.2
此时pip2被升级为19.0.2
输入pip3,下面信息反映出pip3没有安装
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ pip3
程序“pip3”尚未安装。 您可以使用以下命令安装:
sudo apt-get install python3-pip
您必须启用universe 组件
输入sudo apt-get install python3-pip之后依然出现相应的报错
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ sudo apt-get install python3-pip
[sudo] password for jcx:
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
python3-pip 已经是最新的版本。
下列软件包是自动安装的并且现在不需要了:
libasound2:i386 libexpat1:i386 libfontconfig1:i386 libfreetype6:i386
libice6:i386 libjpeg62:i386 libntdb1 libsm6:i386 libx11-6:i386 libxau6:i386
libxcb1:i386 libxdamage1:i386 libxdmcp6:i386 libxext6:i386 libxfixes3:i386
libxinerama1:i386 libxrandr2:i386 libxrender1:i386 libxtst6:i386 python-ntdb
Use 'apt-get autoremove' to remove them.
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 406 个软件包未被升级。
jcx@smart-dsp:~/Desktop/xxr2019/NVlabs_noise2noise$ pip3
程序“pip3”尚未安装。 您可以使用以下命令安装:
sudo apt-get install python3-pip
您必须启用universe 组件
解决方案见其他人帖子,我们放弃这种方案,运用anaconda来配置相应的环境
为了更好的进行版本管理及不同环境下的管理,我们安装anaconda来进行相应的版本管理。
https://blog.csdn.net/weixin_36474809/article/details/87804903
创建相应的环境
conda create -n n2n python=3.6
激活及取消相应的环境运用下面命令行。
# To activate this environment, use:
# > source activate n2n
#
# To deactivate an active environment, use:
# > source deactivate
#
注意,旧版本输入conda activate n2n,
出现下面错误结果
jcx@smart-dsp:~/Desktop/xxr2019/anaconda$ conda activate n2n
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If your shell is Bash or a Bourne variant, enable conda for the current user with
$ echo ". /home/jcx/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc
or, for all users, enable conda with
$ sudo ln -s /home/jcx/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh
The options above will permanently enable the 'conda' command, but they do NOT
put conda's base (root) environment on PATH. To do so, run
$ conda activate
in your terminal, or to put the base environment on PATH permanently, run
$ echo "conda activate" >> ~/.bashrc
Previous to conda 4.4, the recommended way to activate conda was to modify PATH in
your ~/.bashrc file. You should manually remove the line that looks like
export PATH="/home/jcx/anaconda3/bin:$PATH"
^^^ The above line should NO LONGER be in your ~/.bashrc file! ^^^
需要输入新的指令 source activate n2n
jcx@smart-dsp:~/Desktop/xxr2019/anaconda$ source activate n2n
(n2n) jcx@smart-dsp:~/Desktop/xxr2019/anaconda$
上一步若完成失败,则输入下面会出现报错conda install tensorflow-gpu
jcx@smart-dsp:~/Desktop/xxr2019/anaconda$ conda install tensorflow-gpu
Solving environment: failed
UnsatisfiableError: The following specifications were found to be in conflict:
- tensorflow-gpu
Use "conda info " to see the dependencies for each package.
若是上一步运行正确,则会安装相应tf,我们安装的tf信息如下:
The following NEW packages will be INSTALLED:
_tflow_select: 2.1.0-gpu
absl-py: 0.7.0-py36_0
astor: 0.7.1-py36_0
blas: 1.0-mkl
c-ares: 1.15.0-h7b6447c_1
cudatoolkit: 9.2-0
cudnn: 7.3.1-cuda9.2_0
cupti: 9.2.148-0
gast: 0.2.2-py36_0
grpcio: 1.16.1-py36hf8bcb03_1
h5py: 2.9.0-py36h7918eee_0
hdf5: 1.10.4-hb1b8bf9_0
intel-openmp: 2019.1-144
keras-applications: 1.0.6-py36_0
keras-preprocessing: 1.0.5-py36_0
libgfortran-ng: 7.3.0-hdf63c60_0
libprotobuf: 3.6.1-hd408876_0
markdown: 3.0.1-py36_0
mkl: 2019.1-144
mkl_fft: 1.0.10-py36ha843d7b_0
mkl_random: 1.0.2-py36hd81dba3_0
numpy: 1.15.4-py36h7e9f1db_0
numpy-base: 1.15.4-py36hde5b4d6_0
protobuf: 3.6.1-py36he6710b0_0
scipy: 1.2.0-py36h7c811a0_0
six: 1.12.0-py36_0
tensorboard: 1.12.2-py36he6710b0_0
tensorflow: 1.12.0-gpu_py36he74679b_0
tensorflow-base: 1.12.0-gpu_py36had579c0_0
tensorflow-gpu: 1.12.0-h0d30ee6_0
termcolor: 1.1.0-py36_1
werkzeug: 0.14.1-py36_0
最终输出如下信息,表明安装完成。
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(n2n) jcx@smart-dsp:~/Desktop/xxr2019/anaconda$ python -m pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.0.1
Uninstalling pip-19.0.1:
Successfully uninstalled pip-19.0.1
Successfully installed pip-19.0.2