这里写目录标题
- 编译安装 Python
- 专题: 同系统多版本 Python 问题
- altinstall bininstall maninstall
- 兼容性修正
- Redhat/CentOS 系
- Debian/Ubuntu 系
- 快速虚拟化环境工具
- Step 1: Install Python Dependencies
- Step 2: Download latest Python 3.9 Archive
- Step 3: Install Python 3.9 on CentOS 8 / CentOS 7
- 什么是 virtualenv?
- virtualenv 的优点
- 安装和新建虚拟环境
- 新建虚拟环境:
- 激活和关闭当前的虚拟环境
- 列出当前的虚拟环境都安装了哪些包
- virtualenvwrapper
- 安装 virtualenvwrapper(确保 virtualenv 已安装)
- virtualenvwrapper 基本使用
编译安装 Python
在 CentOS 上因为自带的 Python 版本比较旧,所以要安装高版本的 Python 但同时要求不能影响原来的 python3
命令。
先不急着动手,可以先看看关于多版本的专题。
专题: 同系统多版本 Python 问题
非常不建议直接覆盖当前系统的 python
和 python3
命令,Python 每个版本之间多少有点不兼容问题,处理起来非常麻烦,万一系统用到了,就是个大问题。
Building Python:
make install
can overwrite or masquerade thepython3
binary.make altinstall
is therefore recommended instead ofmake install
since it only installsexec_prefix/bin/pythonversion
.
If you want it to install to, for example, /usr/bin
instead of the default (/usr/local/bin
in ubuntu/debian), then instead of ./configure
, type ./configure --prefix=/usr
when told to use it in the guide.
For in your $HOME/bin
directory, use --prefix=$HOME
.
If it doesn’t exist, add $HOME/bin
to your $PATH like this:
$ export PATH=$HOME/bin:$PATH
This may already be in your .bashrc
in ubuntu, and others. If it is, when you next log in, $HOME/bin
will be added to your $PATH
automatically.
altinstall bininstall maninstall
Let’s take a look at the generated Makefile!
First, the install target:
install: altinstall bininstall maninstall
It does everything altinstall
does, along with bininstall
and maninstall
Here’s bininstall
; it just creates the python
and other symbolic links.
# Install the interpreter by creating a symlink chain:
# $(PYTHON) -> python2 -> python$(VERSION))
# Also create equivalent chains for other installed files
bininstall: altbininstall
-if test -f $(DESTDIR)$(BINDIR)/$(PYTHON) -o -h $(DESTDIR)$(BINDIR)/$(PYTHON); \
then rm -f $(DESTDIR)$(BINDIR)/$(PYTHON); \
else true; \
fi
(cd $(DESTDIR)$(BINDIR); $(LN) -s python2$(EXE) $(PYTHON))
-rm -f $(DESTDIR)$(BINDIR)/python2$(EXE)
(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)$(EXE) python2$(EXE))
... (More links created)
And here’s maninstall
, it just creates “unversioned” links to the Python manual pages.
# Install the unversioned manual pages
maninstall: altmaninstall
-rm -f $(DESTDIR)$(MANDIR)/man1/python2.1
(cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python$(VERSION).1 python2.1)
-rm -f $(DESTDIR)$(MANDIR)/man1/python.1
(cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python2.1 python.1)
TLDR: altinstall
skips creating the python
link and the manual pages links, install
will hide the system binaries and manual pages.
兼容性修正
Redhat/CentOS 系
一般在 /usr/bin/
下放一个链接,链接到 /usr/local/bin/
下面:
$ ln -s /usr/local/bin/pip /usr/bin/pip
$ ln -s /usr/local/bin/pip3 /usr/bin/pip3
$ ln -s /usr/local/bin/pip3.9 /usr/local/bin/pip3
Debian/Ubuntu 系
快速虚拟化环境工具
- virtualenv
- venv
Step 1: Install Python Dependencies
Login to your CentOS 8 / CentOS 7 system as root or user with sudo privileges.
Then do system update
sudo yum -y install epel-release
sudo yum -y update
Reboot after the upgrade before you continue to install dependencies
sudo reboot
Install required software development tools required to build Python 3.9 on CentOS 8 / CentOS 7:
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel -y
Confirm gcc
is available:
$ gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Step 2: Download latest Python 3.9 Archive
Ensure wget
is installed:
sudo yum install wget -y
Use wget to download the latest Python 3.9 release.
wget https://www.python/ftp/python/3.9.10/Python-3.9.10.tgz
Extract the archive file using tar:
tar xvf Python-3.9.10.tgz
Switch to the directory created from the file extraction:
cd Python-3.9*/
Step 3: Install Python 3.9 on CentOS 8 / CentOS 7
Run the command below to configure Python installation.
./configure --enable-optimizations
Build Python 3.9 on CentOS 8 / CentOS 7:
sudo make altinstall # altinstall is important, DO NOT use `make install`.
Python 安装时
make install
和make altinstall
的区别: altinstall skips creating the python link and the manual pages links.altinstall
跳过创建 Python 链接和手册页链接的操作。如果使用make install
,在系统中将会有两个不同版本的 Python 在/usr/bin
目录中,这将会导致很多问题。
Be patient as this takes quite some time depending on number of CPU cores in your system.
Check Python 3.9 installation on CentOS 8 / CentOS 7. Run below command to confirm successful installation of Python 3.9 on CentOS 8 / CentOS 7:
$ python3.9 --version
Python 3.9.10
Pip3.9 must have been installed as well:
$ pip3.9 --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Upgrade pip
$ /usr/local/bin/python3.9 -m pip install --upgrade pip
$ pip3.9 --version
什么是 virtualenv?
virtualenv 可以创建独立 Python 开发环境, 比如当前的全局开发环境是 python3.6, 现在我们有一个项目需要使用 django1.3, 另一个项目需要使用 django1.9, 这个时候就可以使用 virtualenv 创建各自的 python 开发环境了。
virtualenv 的优点
- 使不同的应用开发环境独立
- 环境升级不影响其他的应用, 也不会影响全局的 python 开发环境
- 它可以防止系统中出现包管理混乱和版本的冲突
安装和新建虚拟环境
cmd 下输入: 前提是你的 python 安装路径已经被添加到环境变量中
C:\>pip install virtualenv
新建虚拟环境:
C:\>virtualenv testenv
Using base prefix 'c:\\python36'
New python executable in C:\testenv\Scripts\python.exe // 默认安装在当前目录下
Installing setuptools, pip, wheel...done.
可以在当前目录下看一下:
激活和关闭当前的虚拟环境
C:\testenv\Scripts>activate // 激活
(testenv) C:\testenv\Scripts> // 注意终端发生了变化
(testenv) C:\testenv\Scripts>deactivate // 关闭当前虚拟环境
C:\testenv\Scripts>
列出当前的虚拟环境都安装了哪些包
(testenv) c:\testenv\Scripts>pip3 list
pip (9.0.1)
setuptools (37.0.0)
wheel (0.30.0)
现在你就可以在你创建的虚拟环境下安装需要使用到的包了
virtualenvwrapper
鉴于 virtualenv 不便于对虚拟环境集中管理, 所以推荐直接使用 virtualenvwrapper。 virtualenvwrapper 提供了一系列命令使得和虚拟环境工作变得便利。它把你所有的虚拟环境都放在一个地方。
安装 virtualenvwrapper(确保 virtualenv 已安装)
pip install virtualenvwrapper
pip install virtualenvwrapper-win #Windows 使用该命令
安装完成后, 在 ~/.bashrc
写入以下内容
export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh
- 第一行: virtualenvwrapper 存放虚拟环境目录
- 第二行: virtrualenvwrapper 会安装到 python 的
bin
目录下, 所以该路径是python
安装目录下bin/virtualenvwrapper.sh
source ~/.bashrc # 读入配置文件, 立即生效
virtualenvwrapper 基本使用
- 创建虚拟环境 mkvirtualenv
mkvirtualenv venv
这样会在 WORKON_HOME
变量指定的目录下新建名为 venv
的虚拟环境。
若想指定 python 版本, 可通过 --python
指定 python 解释器
mkvirtualenv --python=/usr/local/python3.5.3/bin/python venv
- 基本命令
- 查看当前的虚拟环境目录
[root@localhost ~]# workon
py2
py3
- 切换到虚拟环境
[root@localhost ~]# workon py3
(py3) [root@localhost ~]#
- 退出虚拟环境
(py3) [root@localhost ~]# deactivate
[root@localhost ~]#
- 删除虚拟环境
rmvirtualenv venv
更多推荐
编译安装 Python
发布评论