使用pip install 还是 setup.py 文件进行安装?

使用pip install和requirements.txt文件来安装

根据spinningup中用到的包,做了稍许修改后,制作了现在的requirements.txt,在命令行中直接执行

pip install -r requirements.txt

就可以自动安装好文件中所指定的包.
requirements.txt内容如下:

protobuf>=3.6
cloudpickle==1.2.1
gym
ipython
matplotlib==3.1.1
numpy
pandas
pytest
psutil
scipy
seaborn==0.8.1
tensorflow==1.7.1
tqdm

说明:

  1. tensorflow版本为1.7.1是因为unity ml-agents的对应tf版本为1.7.1
  2. pytorch用pip安装经常容易出错,所以使用conda安装,如果安装1.5版本,在命令行执行
conda install pytorch torchvision cpuonly -c pytorch

这是针对CPU版本,GPU版本和其他pytorch版本,只需要访问官网下载即可.
​pytorch download link

使用setup.py文件进行安装

只要一个本地代码目录包含setup.py文件,就可以安装setup.py里面指定的代码包.

用spinningup的代码仓库为例.

机器学习 - 环境自动搭建_tensorflow


可以看到有个setup.py文件.文件内容如下:

from os.path import join, dirname, realpath
from setuptools import setup
import sys

assert sys.version_info.major == 3 and sys.version_info.minor >= 6, \
"The Spinning Up repo is designed to work with Python 3.6 and greater." \
+ "Please install it before proceeding."

with open(join("spinup", "version.py")) as version_file:
exec(version_file.read())

setup(
name='spinup',
py_modules=['spinup'],
version=__version__,#'0.1',
install_requires=[
'cloudpickle==1.2.1',
'gym[atari,box2d,classic_control]~=0.15.3',
'ipython',
'joblib',
'matplotlib==3.1.1',
'mpi4py',
'numpy',
'pandas',
'pytest',
'psutil',
'scipy',
'seaborn==0.8.1',
'tensorflow>=1.8.0,<2.0',
'torch==1.3.1',
'tqdm'
],
description="Teaching tools for introducing people to deep RL.",
author="Joshua Achiam",
)

可以看到有个install_requires,可以引导安装程序安装指定的代码包.
使用setup.py,只需要在命令行中使用

pip install -e .

就可以安装全部的代码包.
注意:
● pip install . 实际上执行的是 python setup.py install

参考:

  1. ​pip and setup.py​
  2. ​spinningup installation​