系统需求
对python有过深入研究过的人都知道,python3.x版本功能比2.x更好用,因此做项目现在主流还是得用python3.x版本;但有个问题:python3.x版本与python2.x版本有很多函数是不能完全兼容的,linux系统有很多系统应用都得依赖于python2.x,为了安全起见,系统自带的python2.x不要轻易卸载,否则会招致不必要的麻烦,之前本人也吃过这方面的亏,后面浪费大量时间在修复系统。解决最好的办法只有:安装python3.x,不影响原来的python2.x使用,设置使用3时使用命令python3。
软件(源码)安装
软件下载页地址:https://download.csdn.net/download/u012029030/11649334
文件包名:Python-3.5.5.tar.gz
1、把压缩包上传到linux指定目录,进行解压:
# tar zxf Python-3.5.5.tar.gz
2、进入解压后的目录:
# cd Python-3.5.5
# chmod u+x configure
3、创建python3安装目录:
# mkdir /usr/local/python3
4、预编译:
# ./configure --prefix=/usr/local/python3
checking build system type… x86_64-pc-linux-gnu
checking host system type… x86_64-pc-linux-gnu
checking for python3.5… no
checking for python3… no
checking for python… python
checking for --enable-universalsdk… no
checking for --with-universal-archs… no
checking MACHDEP… linux
checking for --without-gcc… no
checking for --with-icc… no
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ISO C89… none needed
checking how to run the C preprocessor… gcc -E
checking for grep that handles long lines and -e… /usr/bin/grep
checking for --with-cxx-main=… no
checking for g++… no
…
…
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
5、编译和安装
# make && make install
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -I. -I./Include -DPy_BUILD_CORE -o Programs/python.o ./Programs/python.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -I. -I./Include -DPy_BUILD_CORE -o Parser/acceler.o Parser/acceler.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -I. -I./Include -DPy_BUILD_CORE -o Parser/gra
-----------------
--------------------
env: /mnt/mysoft/Python-3.5.5/Modules/_ctypes/libffi/configure: Permission denied
Failed to configure _ctypes module
*** WARNING: renaming “_ctypes” since importing it failed: build/lib.linux-x86_64-3.5/_ctypes.cpython-35m-x86_64-linux-gnu.so: cannot open shared object file: No such file or directory
error: [Errno 2] No such file or directory: ‘build/lib.linux-x86_64-3.5/_ctypes.cpython-35m-x86_64-linux-gnu.so’ -> ‘build/lib.linux-x86_64-3.5/_ctypes.cpython-35m-x86_64-linux-gnu_failed.so’
make: *** [sharedmods] Error 1
到这里有错误,是说一个执行文件权限不够,那么对/mnt/mysoft/Python-3.5.5/Modules/_ctypes/libffi/configure添加一个可执行权限即可:
# chmod u+x /mnt/mysoft/Python-3.5.5/Modules/_ctypes/libffi/configure
再次执行编译和安装:
# make && make install
-------------------
------------------
最后没有出现错误提示就表示已安装成功。
6、创建python3访问指令:
# ln -s /usr/local/python3/bin/python3.5 /usr/local/bin/python3
7、最后试试:
# python3
Python 3.5.5 (default, Sep 2 2019, 15:39:23)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
8、试试原来的python2
# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
现在两个版本在同一个系统下已实现完成分离状态,互不影响,两者并存。
9、安装Ipython工具
# cd /usr/local/python3/bin
# ./pip3 install Ipython
Collecting Ipython
Downloading https://files.pythonhosted.org/packages/fb/44/f28a13852e562af719f9de1761680a84a93e8b4c50e22d00d68f60ee2e8b/ipython-7.8.0-py3-none-any.whl (775kB)
100% |████████████████████████████████| 778kB 58kB/s
-------------------
安装完成后:
# ln -s /usr/local/python3/bin/ipython3 /usr/local/bin/ipython3
# ipython3
/usr/local/python3/lib/python3.5/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
warn(“IPython History requires SQLite, your history will not be saved”)
Python 3.5.5 (default, Sep 2 2019, 15:39:23)
Type ‘copyright’, ‘credits’ or ‘license’ for more information
IPython 7.8.0 – An enhanced Interactive Python. Type ‘?’ for help.
In [1]: