linux环境安装python3的环境(包含setuptools,pip)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux环境安装python3的环境(包含setuptools,pip)相关的知识,希望对你有一定的参考价值。

date 20191125 周一
在linux 6.5上安装 python3.7.3 的环境
因为我们的mac上的环境为:
(base) majihuideMacBook-Pro:~ majihui$ python -V
Python 3.7.3


接下来,在我的本地测试环境上 安装 python3.7.3
[root@templatec6 ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
王金灿给我的 python 的 编译环境 linux上 linux centos6.5和centos7 都是可以的
例如centos 先创建好编译需要的环境
yum groupinstall "Development tools" -y
yum install zlib-devel -y
yum install openssl-devel -y
yum install libffi-devel -y

执行命令下载源码,编译,安装。 
[root@templatec6 opt]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
[root@templatec6 opt]# tar -zxvf Python-3.7.3.tgz 
[root@templatec6 opt]# ll
total 22444
drwxr-xr-x. 10 root root     4096 Oct 29 15:53 gitlab
drwxr-xr-x  18  501  501     4096 Mar 26  2019 Python-3.7.3
-rw-r--r--   1 root root 22973527 Mar 26  2019 Python-3.7.3.tgz
[root@templatec6 opt]# cd Python-3.7.3
[root@templatec6 Python-3.7.3]# ./configure --prefix=/usr/local/python3.7.3
接下来执行 
make  
make install
[root@templatec6 Python-3.7.3]# ls -l /usr/local/
total 48
drwxr-xr-x  9 root root 4096 Nov  5 15:59 apache-tomcat-9.0.8
drwxr-xr-x. 2 root root 4096 Sep 23  2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
drwxr-xr-x. 2 root root 4096 Sep 23  2011 include
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x  6 root root 4096 Nov 25 17:06 python3.7.3
drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x. 5 root root 4096 Jul 27 16:00 share
drwxr-xr-x. 2 root root 4096 Sep 23  2011 src

[root@templatec6 bin]# python -V
Python 2.6.6
[root@templatec6 bin]# ln -s /usr/local/python3.7.3/bin/python3 /usr/bin/python3      
[root@templatec6 bin]# python3 -V
Python 3.7.3
接下来安装 安装 setuptools,pip
什么是setuptools
setuptools是Python distutils增强版的集合,它可以帮助我们更简单的创建和分发Python包,尤其是拥有依赖关系的。用户在使用setuptools创建的包时,并不需要已安装setuptools,只要一个启动模块即可。

Python pip 安装与使用  https://www.runoob.com/w3cnote/python-pip-install-usage.html 

python中pip是什么?
pip是一个以Python计算机程序语言写成的软件包管理系统,他可以安装和管理软件包,另外不少的软件包也可以在“Python软件包索引”(英语:Python Package Index,简称PyPI)中找到。
命令行接口
pip的其中一个主要特点就是其方便使用的命令行接口,这让用户可以透过以下的一句文字命令来轻易地安装Python软件包:
python中pip是什么  https://www.php.cn/python-tutorials-424381.html 

基本上是 setuptools,pip 就类似于 rpm 和 yum的关系  一般情况下 pip 去install 安装  

安装 setuptools
wget https://pypi.python.org/packages/45/29/8814bf414e7cd1031e1a3c8a4169218376e284ea2553cc0822a6ea1c2d78/setuptools-36.6.0.zip#md5=74663b15117d9a2cc5295d76011e6fd1
unzip setuptools-36.6.0.zip
cd setuptools-36.6.0
python setup.py install

官方地址为 https://pypi.org/project/setuptools/42.0.0/#files 
[root@templatec6 opt]# wget https://files.pythonhosted.org/packages/ab/41/ab6ae1937191de0c9cbc115d0e91e335f268aa1cd85524c86e5970fdb68a/setuptools-42.0.0.zip
[root@templatec6 opt]# unzip setuptools-42.0.0.zip 
[root@templatec6 opt]# cd setuptools-42.0.0
[root@templatec6 setuptools-42.0.0]# ll
total 232
-rw-rw-r-- 1 root root   1669 Nov 23 19:53 bootstrap.py
-rw-rw-r-- 1 root root 147065 Nov 23 19:53 CHANGES.rst
-rw-rw-r-- 1 root root    537 Nov 23 19:53 conftest.py
drwxr-xr-x 4 root root   4096 Nov 25 17:20 docs
-rw-rw-r-- 1 root root    126 Nov 23 19:53 easy_install.py
-rw-rw-r-- 1 root root  10338 Nov 23 19:53 launcher.c
-rw-rw-r-- 1 root root   1078 Nov 23 19:53 LICENSE
-rw-rw-r-- 1 root root    497 Nov 23 19:53 MANIFEST.in
-rw-rw-r-- 1 root root   1640 Nov 23 19:53 msvc-build-launcher.cmd
-rw-rw-r-- 1 root root   1514 Nov 23 19:53 pavement.py
-rw-rw-r-- 1 root root   3563 Nov 23 19:53 PKG-INFO
drwxr-xr-x 5 root root   4096 Nov 25 17:20 pkg_resources
-rw-rw-r-- 1 root root    426 Nov 23 19:53 pytest.ini
-rw-rw-r-- 1 root root   1850 Nov 23 19:53 README.rst
-rw-rw-r-- 1 root root   1713 Nov 23 19:53 setup.cfg
-rwxrwxr-x 1 root root   5990 Nov 23 19:53 setup.py
drwxr-xr-x 6 root root   4096 Nov 25 17:20 setuptools
drwxr-xr-x 2 root root   4096 Nov 25 17:20 setuptools.egg-info
-rw-rw-r-- 1 root root    663 Nov 23 19:53 towncrier_template.rst
-rw-rw-r-- 1 root root   1757 Nov 23 19:53 tox.ini
[root@templatec6 setuptools-42.0.0]# which python
/usr/bin/python
[root@templatec6 setuptools-42.0.0]# which python3
/usr/bin/python3
[root@templatec6 setuptools-42.0.0]# python3 setup.py install  
...
...
...
creating dist
creating ‘dist/setuptools-42.0.0-py3.7.egg‘ and adding ‘build/bdist.linux-x86_64/egg‘ to it
removing ‘build/bdist.linux-x86_64/egg‘ (and everything under it)
Processing setuptools-42.0.0-py3.7.egg
Copying setuptools-42.0.0-py3.7.egg to /usr/local/python3.7.3/lib/python3.7/site-packages
Adding setuptools 42.0.0 to easy-install.pth file
Installing easy_install script to /usr/local/python3.7.3/bin
Installing easy_install-3.7 script to /usr/local/python3.7.3/bin

Installed /usr/local/python3.7.3/lib/python3.7/site-packages/setuptools-42.0.0-py3.7.egg
Processing dependencies for setuptools==42.0.0
Finished processing dependencies for setuptools==42.0.0

安装pip

安装pip
下载地址:https://pypi.python.org/pypi/pip,和上面一样下载下来,这里可以选择tgz格式的安装包
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
tar -zxvf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install

[root@templatec6 opt]# wget https://files.pythonhosted.org/packages/ce/ea/9b445176a65ae4ba22dce1d93e4b5fe182f953df71a145f557cffaffc1bf/pip-19.3.1.tar.gz
[root@templatec6 opt]# tar zxvf pip-19.3.1.tar.gz 
[root@templatec6 opt]# cd pip-19.3.1
[root@templatec6 pip-19.3.1]# ll
total 160
-rw-r--r-- 1 1000 users  20110 Oct 18 03:32 AUTHORS.txt
drwxr-xr-x 4 1000 users   4096 Oct 18 16:16 docs
-rw-r--r-- 1 1000 users   1090 Jul 18 17:11 LICENSE.txt
-rw-r--r-- 1 1000 users    755 Sep 30 05:36 MANIFEST.in
-rw-r--r-- 1 1000 users 103735 Oct 18 04:18 NEWS.rst
-rw-r--r-- 1 1000 users   3688 Oct 18 16:16 PKG-INFO
-rw-r--r-- 1 1000 users    919 Sep 30 05:36 pyproject.toml
-rw-r--r-- 1 1000 users   2130 Jul 22 21:35 README.rst
-rw-r--r-- 1 1000 users    662 Oct 18 16:16 setup.cfg
-rw-r--r-- 1 1000 users   2722 Oct 18 16:15 setup.py
drwxr-xr-x 4 1000 users   4096 Oct 18 16:16 src
[root@templatec6 pip-19.3.1]# python3 setup.py install
...
...
...
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying src/pip.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying src/pip.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying src/pip.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying src/pip.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying src/pip.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying src/pip.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating dist
creating ‘dist/pip-19.3.1-py3.7.egg‘ and adding ‘build/bdist.linux-x86_64/egg‘ to it
removing ‘build/bdist.linux-x86_64/egg‘ (and everything under it)
Processing pip-19.3.1-py3.7.egg
creating /usr/local/python3.7.3/lib/python3.7/site-packages/pip-19.3.1-py3.7.egg
Extracting pip-19.3.1-py3.7.egg to /usr/local/python3.7.3/lib/python3.7/site-packages
Adding pip 19.3.1 to easy-install.pth file
Installing pip script to /usr/local/python3.7.3/bin
Installing pip3 script to /usr/local/python3.7.3/bin
Installing pip3.7 script to /usr/local/python3.7.3/bin

Installed /usr/local/python3.7.3/lib/python3.7/site-packages/pip-19.3.1-py3.7.egg
Processing dependencies for pip==19.3.1
Finished processing dependencies for pip==19.3.1

[root@templatec6 pip-19.3.1]# find / -name "pip"
/opt/pip-19.3.1/build/lib/pip
/opt/pip-19.3.1/src/pip
/opt/gitlab/embedded/lib/python3.4/site-packages/pip
/opt/Python-3.7.3/Tools/msi/pip
/usr/local/python3.7.3/lib/python3.7/site-packages/pip
/usr/local/python3.7.3/lib/python3.7/site-packages/pip-19.3.1-py3.7.egg/pip
/usr/local/python3.7.3/bin/pip
[root@templatec6 pip-19.3.1]# which pip
/usr/bin/which: no pip in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.8.0_151/bin:/root/bin)
[root@templatec6 pip-19.3.1]# /usr/local/python3.7.3/bin/pip --version
pip 19.3.1 from /usr/local/python3.7.3/lib/python3.7/site-packages/pip-19.3.1-py3.7.egg/pip (python 3.7)


接下来安装 virtualenv 安装这个的目的是 创建虚拟环境 每个项目每个项目用独立的环境
//
在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4。所有第三方的包都会被pip安装到Python3的site-packages目录下。

如果我们要同时开发多个应用程序,那这些应用程序都会共用一个Python,就是安装在系统的Python 3。如果应用A需要jinja 2.7,而应用B需要jinja 2.6怎么办?

这种情况下,每个应用可能需要各自拥有一套“独立”的Python运行环境。virtualenv就是用来为一个应用创建一套“隔离”的Python运行环境。
参考链接: https://www.liaoxuefeng.com/wiki/1016959663602400/1019273143120480

参考链接: Linux安装pip和setuptools https://blog.csdn.net/yanpenggong/article/details/89957857
Linux下Python环境部署 https://blog.csdn.net/shylonegirl/article/details/83017139


[root@templatec6 opt]# cd /usr/local/python3.7.3/bin/
[root@templatec6 bin]# ll
total 21592
lrwxrwxrwx 1 root root        8 Nov 25 17:06 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root      113 Nov 25 17:06 2to3-3.7
-rwxr-xr-x 1 root root      404 Nov 25 17:26 easy_install
-rwxr-xr-x 1 root root      412 Nov 25 17:26 easy_install-3.7
lrwxrwxrwx 1 root root        7 Nov 25 17:06 idle3 -> idle3.7
-rwxr-xr-x 1 root root      111 Nov 25 17:06 idle3.7
-rwxr-xr-x 1 root root      365 Nov 25 17:31 pip
-rwxr-xr-x 1 root root      367 Nov 25 17:31 pip3
-rwxr-xr-x 1 root root      371 Nov 25 17:31 pip3.7
lrwxrwxrwx 1 root root        8 Nov 25 17:06 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root       96 Nov 25 17:06 pydoc3.7
lrwxrwxrwx 1 root root        9 Nov 25 17:06 python3 -> python3.7
-rwxr-xr-x 2 root root 11033390 Nov 25 17:05 python3.7
lrwxrwxrwx 1 root root       17 Nov 25 17:06 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 11033390 Nov 25 17:05 python3.7m
-rwxr-xr-x 1 root root     3103 Nov 25 17:06 python3.7m-config
lrwxrwxrwx 1 root root       16 Nov 25 17:06 python3-config -> python3.7-config
lrwxrwxrwx 1 root root       10 Nov 25 17:06 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root      453 Nov 25 17:06 pyvenv-3.7
[root@templatec6 bin]# /usr/local/python3.7.3/bin/pip install virtualenv
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/virtualenv/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/virtualenv/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/virtualenv/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/virtualenv/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/virtualenv/
Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/virtualenv/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement virtualenv (from versions: none)
ERROR: No matching distribution found for virtualenv
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping
// 报错的意思是没找到,需要配置国内的pip的源

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple 

使用pip安装完包后,只需再执行一次命令pip install xx,就会显示安装路径

使用pip freeze命令可以查看安装了那些包 

用这个,是金灿给我的 
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com 
参考链接 : pip国内源设置方法 https://blog.csdn.net/weixin_41712059/article/details/86704492 

---------------------------------------------------------------------------------------------------
我的实际操作为:
[root@templatec6 .pip]# pwd
/root/.pip
[root@templatec6 .pip]# cat pip.conf 
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

[root@templatec6 .pip]# /usr/local/python3.7.3/bin/pip install virtualenv
Looking in indexes: http://pypi.douban.com/simple
Collecting virtualenv
  Downloading http://pypi.doubanio.com/packages/62/77/6a86ef945ad39aae34aed4cc1ae4a2f941b9870917a974ed7c5b6f137188/virtualenv-16.7.8-py2.py3-none-any.whl (3.4MB)
     |████████████████████████████████| 3.4MB 341kB/s 
Installing collected packages: virtualenv
  WARNING: The script virtualenv is installed in ‘/usr/local/python3.7.3/bin‘ which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed virtualenv-16.7.8 

[root@templatec6 bin]# pwd
/usr/local/python3.7.3/bin
[root@templatec6 bin]# ll
total 21596
lrwxrwxrwx 1 root root        8 Nov 25 17:06 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root      113 Nov 25 17:06 2to3-3.7
-rwxr-xr-x 1 root root      404 Nov 25 17:26 easy_install
-rwxr-xr-x 1 root root      412 Nov 25 17:26 easy_install-3.7
lrwxrwxrwx 1 root root        7 Nov 25 17:06 idle3 -> idle3.7
-rwxr-xr-x 1 root root      111 Nov 25 17:06 idle3.7
-rwxr-xr-x 1 root root      365 Nov 25 17:31 pip
-rwxr-xr-x 1 root root      367 Nov 25 17:31 pip3
-rwxr-xr-x 1 root root      371 Nov 25 17:31 pip3.7
lrwxrwxrwx 1 root root        8 Nov 25 17:06 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root       96 Nov 25 17:06 pydoc3.7
lrwxrwxrwx 1 root root        9 Nov 25 17:06 python3 -> python3.7
-rwxr-xr-x 2 root root 11033390 Nov 25 17:05 python3.7
lrwxrwxrwx 1 root root       17 Nov 25 17:06 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 11033390 Nov 25 17:05 python3.7m
-rwxr-xr-x 1 root root     3103 Nov 25 17:06 python3.7m-config
lrwxrwxrwx 1 root root       16 Nov 25 17:06 python3-config -> python3.7-config
lrwxrwxrwx 1 root root       10 Nov 25 17:06 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root      453 Nov 25 17:06 pyvenv-3.7
-rwxr-xr-x 1 root root      209 Nov 25 17:51 virtualenv        // 这个就下载好了

创建一个 python 的 虚拟环境    
[root@templatec6 bin]# /usr/local/python3.7.3/bin/virtualenv /opt/python_test
Using base prefix ‘/usr/local/python3.7.3‘
New python executable in /opt/python_test/bin/python3
Also creating executable in /opt/python_test/bin/python
Installing setuptools, pip, wheel...
done. 
[root@templatec6 bin]# cd /opt/python_test/
[root@templatec6 python_test]# ll
total 12
drwxr-xr-x 2 root root 4096 Nov 25 18:02 bin
drwxr-xr-x 2 root root 4096 Nov 25 18:02 include
drwxr-xr-x 3 root root 4096 Nov 25 18:02 lib
lrwxrwxrwx 1 root root    3 Nov 25 18:02 lib64 -> lib
[root@templatec6 python_test]# cd bin/
[root@templatec6 bin]# ll
total 10828
-rw-r--r-- 1 root root     2205 Nov 25 18:02 activate
-rw-r--r-- 1 root root     1424 Nov 25 18:02 activate.csh
-rw-r--r-- 1 root root     3089 Nov 25 18:02 activate.fish
-rw-r--r-- 1 root root     1751 Nov 25 18:02 activate.ps1
-rw-r--r-- 1 root root     1517 Nov 25 18:02 activate_this.py
-rw-r--r-- 1 root root     1146 Nov 25 18:02 activate.xsh
-rwxr-xr-x 1 root root      242 Nov 25 18:02 easy_install
-rwxr-xr-x 1 root root      242 Nov 25 18:02 easy_install-3.7
-rwxr-xr-x 1 root root      229 Nov 25 18:02 pip
-rwxr-xr-x 1 root root      229 Nov 25 18:02 pip3
-rwxr-xr-x 1 root root      229 Nov 25 18:02 pip3.7
lrwxrwxrwx 1 root root        7 Nov 25 18:02 python -> python3
-rwxr-xr-x 1 root root 11033390 Nov 25 18:02 python3
lrwxrwxrwx 1 root root        7 Nov 25 18:02 python3.7 -> python3
-rwxr-xr-x 1 root root     2334 Nov 25 18:02 python-config
-rwxr-xr-x 1 root root      220 Nov 25 18:02 wheel

[root@templatec6 bin]# pwd
/opt/python_test/bin
[root@templatec6 bin]# /opt/python_test/bin/pip --version
pip 19.3.1 from /opt/python_test/lib/python3.7/site-packages/pip (python 3.7)
[root@templatec6 bin]# /opt/python_test/bin/python -V
Python 3.7.3

[root@templatec6 bin]# cd /tmp/
[root@templatec6 tmp]# ll
total 8
drwx------. 2 git  git  4096 Oct 30 20:21 gitaly-ruby899036078
drwxr-xr-x  3 root root 4096 Nov 25 18:13 pycharm_project_455 

[root@templatec6 pycharm_project_455]# pwd
/tmp/pycharm_project_455
[root@templatec6 pycharm_project_455]# ll
total 12
-rw-r--r-- 1 root root   21 Nov 25 15:49 hello.py
-rw-r--r-- 1 root root 3769 Jul 30 21:15 launcher
drwxr-xr-x 4 root root 4096 Nov 25 18:14 venv
[root@templatec6 pycharm_project_455]# /opt/python_test/bin/python hello.py 
hello world 

[root@templatec6 bin]# source /opt/python_test/bin/activate 
(python_test) [root@templatec6 bin]# python -V
Python 3.7.3
(python_test) [root@templatec6 bin]# which  python
/opt/python_test/bin/python
(python_test) [root@templatec6 bin]# python /tmp/pycharm_project_455/hello.py 
hello world

以上是关于linux环境安装python3的环境(包含setuptools,pip)的主要内容,如果未能解决你的问题,请参考以下文章

Linux环境下安装python3

linux环境安装python3

python3的linux环境编译安装

windows和linux下 Python2,Python3 的环境及安装

linux安装python3环境并配置虚拟环境

linux安装python3环境并配置虚拟环境