博主树莓派上有两个版本的python,一个是python2.7, 一个是python3.9
?注:上面也有2.7版本的python,但python2并没有pip模块,只有python3的pip模块。
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python2 get-pip.py
可通过上面命令语句来安装python2的pip模块。
指定版本安装库时可以如下语句
python2 -m pip install 库的名称
如下安装的是keras?
?完毕后可看到,keras已经安装上了。
?下面再通过源码方式去安装一个python3.7,以来验证多版本python3的管理问题。
先安装必要库
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev
sudo apt-get install -y libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
sudo apt-get install -y libncurses5-dev libncursesw5-dev xz-utils tk-dev
下载python3.7版本?
sudo wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
?解压
sudo tar -zxvf Python-3.7.3.tgz
?进入到解压缩后文件夹内,输入如下命令,这里会安装到指定的目录下,方便后期删除
sudo ./configure --prefix=/home/pi/python3.7
sudo make -j4
sudo make install
如下是查验一些信息
这边使用如下两条软链接命令来指定当前所使用的版本
sudo ln -s /home/pi/python3.7/bin/python3 /usr/bin/python3
sudo ln -s /home/pi/python3.7/bin/pip3 /usr/bin/pip3
可以看到当前pip3和python3均指向了?python3.7版本,而不是前面默认的python3.9版本
执行pip3 list时会报如下错误信息:
pip3 list
Package Version
---------- -------
pip 19.0.3
setuptools 40.8.0
Traceback (most recent call last):
File "/usr/bin/pip3", line 10, in <module>
sys.exit(main())
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_internal/__init__.py", line 78, in main
return command.main(cmd_args)
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 228, in main
timeout=min(5, options.timeout)
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 93, in _build_session
insecure_hosts=options.trusted_hosts,
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_internal/download.py", line 344, in __init__
self.headers["User-Agent"] = user_agent()
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_internal/download.py", line 108, in user_agent
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 675, in linux_distribution
self.version(),
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 735, in version
self.lsb_release_attr('release'),
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 892, in lsb_release_attr
return self._lsb_release_info.get(attribute, '')
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 550, in __get__
ret = obj.__dict__[self._fname] = self._f(obj)
File "/home/pi/python3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 998, in _lsb_release_info
stdout = subprocess.check_output(cmd, stderr=devnull)
File "/home/pi/python3.7/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/home/pi/python3.7/lib/python3.7/subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
?解决办法如下:
如下命令查找lsb_release.py
sudo find / -name 'lsb_release.py'
?再执行如下命令:
sudo cp /usr/share/pyshared/lsb_release.py /home/pi/python3.7/lib/python3.7
?此时pip3 list可以正常列出安装包的信息
?执行如下命令
sudo pip3 install tensorflow
可以看到python3.7中有tensorflow的库了。同时默认安装的python3.9中并没有tensorflow的库。
?所以可以通过如上两条软连接语句来切换版本。
可通过如下命令来升级pip
sudo python3 -m pip install --upgrade pip
可以看到这边也不一定非要Anconda来实现多版本的管理。前面有介绍过用Anaconda来配置虚拟环境,以来让不同环境得以相互独立,下面也介绍下不通过Anaconda来实现创建虚拟环境。
|