centos7 installs Python 3 and tab completion

centos7 installs Python 3 and tab completion

 

1. Install python3

1.1 download python source package

Website: https://www.python.org/downloads/release/python-366/

Download address: https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz

To compile and install Python 3.6

1. Download Python 3 source package
 2. Download Python 3 compiled dependency package
 3. Decompress the source package 
4. Enter the source package folder 
5. Compile and install
    1. Enter the source package directory
    2. LS view source package content
    3. Release the Makefile  
    4. compilation
    5. Compile and install, and this step will finally generate / opt/python36/
    6. Go to the / opt directory to view the python36 folder. All python3 we want are here
 6. Change the path variable of linux and add python3
    Be sure to put the directory of python3 first
    To make the path settings permanent, add them to the / etc/profile global environment variable configuration file
    Reload configuration file / etc/profile
 7. Install django module
 8. Check the python modules of the current interpreter
To compile and install Python 3.6

1.1.1 the library environment before installing python is very important

yum install -y gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

1.2 download, decompress and install the source package

tar -xvf Python-3.6.6.tgz  
cd  Python-3.6.6  # Enter the source package folder

1,configure

This step is generally used to generate makefiles to prepare for the next step of compilation. You can control the installation by adding parameters after configure, such as code:

./configure --prefix=/usr

The above meaning is to install the software under / usr, the execution file will be installed in / usr/bin (instead of the default / usr/local/bin), and the resource file will be installed in / usr/share (instead of the default / usr/local/share).

At the same time, some software configuration files can be set by specifying the -- sys config = parameter. Some software can also add -- with, -- enable, -- without, -- disable and other parameters to control the compilation. You can check the detailed instructions help by allowing. / configure --help.

2,make

This step is compilation. Most source code packages are compiled in this step (of course, some software written by perl or python needs to call perl or python to compile).

If there is an error in the process of making, you should write down the error code (note that it is not only the last line), and then you can submit the bug report to the developer (usually there is a submission address in INSTALL), or your system has less dependency libraries, etc., which need to study the error code carefully.

The function of make is to start source code compilation and provide some functions. These functions are provided by its Makefile settings file. For example, make install generally means to install. make uninstall means to uninstall. Without parameters, it is the default to compile source code.

make is Linux A control program for automatic compilation in the development kit. It calls automatically through the compilation specification written in Makefile gcc , ld, and programs that run some required programs for compilation. In general, the Makefile control code he uses is generated by the configure setup script based on the given parameters and system environment.

3,make install

This command is used for installation (of course, some software needs to run make check or make test first for some tests). This step generally requires that you have root permission (because you need to write files to the system)

wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz
xz -d Python-3.6.7.tar.xz
tar -xf Python-3.6.7.tar
#yum install gcc -y
./configure --prefix=/opt/python make && make install
---------------------
use python View default installation path
>>> import sys
>>> sys.path
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
--------------------
python3.4 Not added by default path
stay/etc/profile Last line add
export PATH=$PATH:/opt/python/bin
Then?
source /etc/profile

#python Quick path It was changed, yum Used python2,trouble

# Try to install django Come by python3 The interpreter runs it
pip3 install -i https://pypi.douban.com/simple django==1.11.16

# Create django project and APP
django-admin startproject mysite
django-admin startapp app01

1.2.3 automatic completion of Newton tab

After downloading setuptool 3.4, easy? Install-3.4 is installed by default

Download address:
https://pypi.python.org/pypi/setuptools/36.2.7
//Download command:
wget https://pypi.python.org/packages/07/a0/11d3d76df54b9701c0f7bf23ea9b00c61c5e14eb7962bb29aed866a5844e/setuptools-36.2.7.zip#md5=b9e6c049617bac0f9e908a41ab4a29ac
Decompression:
unzip setuptools-36.2.7.zip
//Compile:
python3 setup.py build
python2 setup.py build
//Installation after no error:
python3 setup.py install
python2 setup.py install

Download readline

#yum install -y readline-devel -y 
#pip install readline
tab completion
#python startup file
import readline,rlcompleter
#tab completion
readline.parse_and_bind('tab:complete')
----------------------------
python Historical record
#history file
import os
history_file = os.path.join(os.environ["HOME"],".pythonhistory")
[root@mm_local ~]# echo "export PYTHONSTARTUP='/root/.pythonstartup'" >> /root/.bashrc   
[root@mm_local ~]# source /root/.bashrc  
[root@mm_local ~]# cat /root/.pythonstartup   
import os  
import readline  
import rlcompleter  
import atexit  
  
  
#tab completion  
readline.parse_and_bind("tab: complete")  
  
  
#history file  
history_file = os.path.join(os.environ["HOME"],".pythonhistory")  
try:  
    readline.read_history_file(history_file)  
except IOError:  
    pass  
atexit.register(readline.write_history_file,history_file)  
  
  
del os,history_file,readline,rlcompleter  
[root@mm_local ~]# python  
Python 2.7.3 (default, Jun  5 2013, 22:40:26)   
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2  
Type "help", "copyright", "credits" or "license" for more information.  
>>> import sys  
>>> sys.path<tab> #Press tab The key will automatically complete  
  
[root@mm_local ~]# python  
>>>  
>>> sys.path #Press the up and down arrow keys to find the history command 
Whole code

Add python's startup environment variable to make it permanent and automatic

Just execute one command at startup

import rlcompleter, readline
readline.parse_and_bind('tab:complete')
You can save the above code in ~ /. pythonrc.py file.

Then, set it in ~ /. bashrc

export PYTHONSTARTUP=~/.pythonrc.py
 In this way, bash will set the environment variable PYTHONSTARTUP every time it starts the terminal, and then python will use the initialization code in the file specified by this variable when it starts.

In the python interpreter, enter a part of a variable, or add a period after the variable, and then press TAB to see the power of auto completion.
 

Tags: Linux Python Makefile Django yum

Posted on Fri, 10 Jan 2020 23:46:41 -0800 by Steffen