用pycharm运行python语言查看库的方法以及版本。补:查看模块是否存在。
比如说matplotlib库:
import matplotlib
attributes = dir(matplotlib)
for item in attributes:
if not item.startswith('_'):
print(item)
遍历matplotlib库里方法,要求是方法开头不带有'_'。
结果为:
查看库的版本:
import h5py
print(h5py.__version__)
查看模块是否存在:
try:
import os
import gzip
from sklearn.neighbors import KNeighborsClassifier
print("All required modules are available.")
except ImportError as e:
missing_module = str(e).split()[-1]
print(f"Module '{missing_module}' is missing.")
输出All required modules are available.上文中所有导入的模块都存在。
安装jupyter
激活pytorch环境
在pytorch环境中输入:
conda install nb_conda_kernels
等待安装完成后输入:
jupyter notebook
打开后点击带有pytorch环境的。
用GPU还是CPU进行运行:(其中数字不同选择不同)
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"