Jenkins Realization CI/CD, To run Pyhon Script , And some of them VM By default, only Python 2, But the script must use Python 3 Environment . Or the system comes with Python 2 and Python 3 edition , But it needs to be updated Python 3. Of course, if you can find it easily DevOps The engineer , This problem is solved very well , Directly in VM Module mounting Python 3 Can then , But there are also people who cannot be contacted DevOps, Only forced Script Installation , Each run Python Before the script , Let's start with random assignment VM Put it on again Python 3. I am the latter , Not directly log in To VM operation , Only through the command belongs to the blind man and elephant exploration , Record some of the pits you've experienced .
Summary :
You can pass some orders , Let's see Python Environment .
perform whereis python command
console output:
More fortunate , There are many versions of this machine Python.
perform ls -l /usr/bin|grep python Before the command first looks Python Corresponding to soft link
console output:
Python 3 The corresponding is Python 3.8 edition
Python 2 The corresponding is Python 2.7 edition
Python The corresponding is Python 3
You can further verify the version : Execute the following command
python -V
python2 -V
python3 -V
console output:
Learn from some commands Python Environmental conditions , But if something bad happens , The environment is not equipped with any Python 3 edition , Then you have to install a new set Python 3 Environmental Science .
By executing orders cat /proc/version
console output:
Learned yes Ubuntu System , Different systems can also have an impact .
And down here we are Ubuntu System to install new Python 3 Environmental Science
handle ModuleNotFoundError: No module named '_bz2'
Command 'lsb_release -a' returned non-zero exit status 1
complete Shell Script :
cd /usr/local
if [ ! -d "/usr/local/py3" ]; then
sudo mkdir /usr/local/py3
fi
sudo chmod -R 777 /usr/local/py3
cd /tmp
sudo apt-get install -y zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz
tar -xvJf Python-3.11.0.tar.xz
cd Python-3.11.0/
sudo ./configure prefix=/usr/local/py3 CFLAGS=-fPIC
sudo make && make install
sudo ln -sf /usr/local/py3/bin/python3.11 /usr/bin/python3
sudo ln -sf /usr/local/py3/bin/pip3.11 /usr/bin/pip3
python3 -V
pip3 -V
Most of Linux Distribution version (Ubuntu、CentOS etc. ) It's all brought by default Python. yes , we have Linux The distribution even comes with two versions Python, For example, the latest version of Ubuntu Will bring it Python 2.x and Python 3.x. What do you think is existing Python 3 The version is not new enough , Then we need to update Python edition .
stay Ubuntu The terminal can be updated by executing the following two commands Python edition :
sudo apt-get update
sudo apt-get install python3.11
The first command specifies the update /etc/apt/sources.list and /etc/apt/sources.list.d The source address listed , This ensures that you get the latest installation package .
The second command specifies the installation Python 3.11, This is the latest Python edition .
Console Output:
Compared with the previous one, there is no upgrade , More than a python3.11 了 , If you need to use this version , Refer to the above to change the soft link .