Python management under CentOS

Posted by Kaizard on Wed, 01 Apr 2020 09:39:57 +0200

Upgrade Python

View system version

cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

View Python version

python -V
Python 2.7.5

Installing Python 3

Install all development kits

yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel -y

Download the latest python installation package

https://www.python.org/downloads/

# download
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
wget http://mirrors.sohu.com/python/3.6.4/Python-3.6.4.tgz
# decompression
tar -xavf Python-2.7.14.tgx
cd Python-2.7.14
# Compilation and installation
# . / configure --help to view compilation parameters
# The default installation is' / usr / local / bin ',' / usr / local / lib 'etc
./configure && make && make install

pyenv managing Python versions

install

https://github.com/pyenv/pyenv#installation

# 1.Check out pyenv where you want it installed.
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# 2.Define environment variable PYENV_ROOT
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

# 3.Add pyenv init to your shell 
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
 
# 4.Restart your shell so the path changes take effect.
# exec "$SHELL"
source ~/.bash_profile

# 5.Install Python versions into $(pyenv root)/versions.
pyenv install 2.7.8

upgrade

# upgrade
cd $(pyenv root)
git pull

# Switch version branch
cd $(pyenv root)
git fetch
git tag
git checkout v0.1.0

uninstall

rm -rf $(pyenv root)

Use

command

# pyenv has 11 commands in total
[root@www ~]# pyenv 
pyenv 1.1.5
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable
   
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

View python version

# View version
pyenv versions
pyenv version

# Set version
pyenv global 3.6.3
pyenv local 3.6.3
pyenv shell 3.6.3
pyenv shell --unset

# Explain
# Global sets the global Python version by writing the version number to the ~ /. pyenv/version file
# Local sets the local version of the program. pyenv writes the version number to the. Python version file in the current directory
# The shell sets the Python version for the shell by setting the pyenv · version environment variable of the current shell.
# --The unset parameter can be used to cancel the version of the current shell setting.

Manage python versions

# view help
pyenv help install

# View the python version that can be installed through pyenv
pyenv install -l

# Install specified version, - v shows installation details
pyenv install -v 2.7.14
pyenv install -v 3.6.3

# Uninstall a version
pyenv uninstall 2.7.14

# Execute the following command every time you install or uninstall a version
# Create shims for all installed executables (such as ` ~ /. pyenv/versions/*/bin / * ')
pyenv rehash

Mirror image

Mirror image:

Manual installation

Download the required version and put it under the ~ /. pyenv/cache folder

#Modify download link
vi /root/.pyenv/plugins/python-build/share/python-build/2.7.6

Then execute the pyenv install version number to install the corresponding python version
pyenv install 3.6.3 -v

One click script installation

# pyenv install 3.6.3
v=3.6.3|wget http://mirrors.sohu.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v -v

# Step-by-step installation
v=3.6.3
wget http://mirrors.sohu.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/
pyenv install $v -v

Set mirror variable

# Set mirror URL variable
export PYTHON_BUILD_MIRROR_URL="http://pyenv.qiniudn.com/pythons/"

# Install 2.7.5
pyenv install 2.7.5 -v

Set up image service

Rename the installation package to 64 bit sha code

sha.py

# -*- coding:utf-8 -*-
import os
import hashlib
import sys
__author__ = 'dave'
def get_hash(filepath):
    if not os.path.exists(filepath):
        print('File not exists.')
        return
    # algo = hashlib.md5()
    algo = hashlib.sha256()
    with open(filepath, 'rb') as f:
        while True:
            data = f.read(4096)
            if not data:
                break
            algo.update(data)
    return algo.hexdigest()
if __name__ == '__main__':
	filepath = sys.argv[1]
    # md5sum = get_hash('Python-3.3.6.tar.xz')
    md5sum = get_hash(filepath)
    print(md5sum)
    print(len(md5sum))

Set image address

export PYTHON_BUILD_MIRROR_URL="http://127.0.0.1:8000/"
# or
export PYTHON_BUILD_MIRROR_URL="http://0.0.0.0:8000/"

Opening service

# Be sure to switch to the directory containing the image and execute the following command
cd ~/.pyenv/cache/

# python3
python -m http.server
# python2
python -m SimpleHTTPServer

install

# Open another terminal window
pyenv install 3.3.6

virtualenv managing Python projects

install

https://github.com/pyenv/pyenv-virtualenv

# 1.Check out pyenv-virtualenv into plugin directory
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

# 2.Add pyenv virtualenv-init to your shell
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
# Fish shell note: Add this to your ~/.config/fish/config.fish
# status --is-interactive; and source (pyenv virtualenv-init -|psub)

# 3.Restart your shell to enable pyenv-virtualenv
# exec "$SHELL"
source ~/.bash_profile

Use

view help

pyenv help virtualenv
Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
       pyenv virtualenv --version
       pyenv virtualenv --help

  -f/--force       Install even if the version appears to be installed already

With pyenv virtualenv, we can create multiple "working environments" for the same Python interpreter.

# For example, we create two new working environments:
pyenv virtualenv 2.7.14 first_project
pyenv virtualenv 2.7.14 second_project

# You can view the working environment using the virtualenvs subcommand
pyenv virtualenvs
  2.7.14/envs/first_project (created from /root/.pyenv/versions/2.7.14)
  2.7.14/envs/second_project (created from /root/.pyenv/versions/2.7.14)
  first_project (created from /root/.pyenv/versions/2.7.14)
  second_project (created from /root/.pyenv/versions/2.7.14)

# Enter or exit a work environment through the activate and deactivate subcommands
pyenv activate first_project

# If you want to delete the virtual environment, use:
pyenv virtualenv-delete first_project

Manage Python packages

setuptools

# The easy? Install command is installed in the / usr/local/bin directory
wget https://bootstrap.pypa.io/ez_setup.py -O - | python

setuptools-0.6c11-py2.7.egg

sh setuptools-0.6c11-py2.7.egg

Install pip

# pip command is installed in / usr/local/bin directory
easy_install pip

pip

How to use HKUST image to accelerate pip https://lug.ustc.edu.cn/wiki/mirrors/help/pypi

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider

Configure image ~ /. pip/pip.conf

[global]
index-url = https://mirrors.ustc.edu.cn/pypi/web/simple
format = columns

Plug-in mirroring

Topics: Python shell git pip