在之前的博客中介绍了更换PIP源的方法,但有些时候内网还是无法访问外网的PIP源,这样就需要搭建自己的PIP源。 本文介绍具体的搭建方法:
Python 更改 PyPI 源
http://www.cndba.cn/dave/article/2261
1 安装PIP
因为Linux 默认都会自动安装Python,所以我们只需要安装pip即可。
先从官网下载并安装setuptools-36.5.0.zip,然后安装pip-9.0.1.tar.gz。
[dave@www.cndba.cn work]# wget https://pypi.python.org/packages/a4/c8/9a7a47f683d54d83f648d37c3e180317f80dc126a304c45dc6663246233a/setuptools-36.5.0.zip#md5=704f500dd55f4bd0be905444f3ba892c
[dave@www.cndba.cn work]# unzip setuptools-36.5.0.zip
[dave@www.cndba.cn work]# cd setuptools-36.5.0/
[dave@www.cndba.cn work]# python setup.py install
[dave@www.cndba.cn work]# cd ..
[dave@www.cndba.cn work]# wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
[dave@www.cndba.cn work]# tar xzvf pip-9.0.1.tar.gz
[dave@www.cndba.cn work]# cd pip-9.0.1/
[dave@www.cndba.cn work]# python setup.py install
2 pip源配置
2.1 创建pip源目录:
[dave@www.cndba.cn work]# mkdir -p /work/pypi/Packages
2.2 安装pip2pi软件
可以使用pip安装,也可以下载包来安装:
[dave@www.cndba.cn Packages]# pip install pip2pi
Collecting pip2pi
Downloading pip2pi-0.7.0-py2.py3-none-any.whl
Requirement already satisfied: pip>=1.1 in /usr/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg (from pip2pi)
Installing collected packages: pip2pi
Successfully installed pip2pi-0.7.0
安装完之后会有2个命令:pip2pi,pip2tgz:
[dave@www.cndba.cn Packages]# pip2
pip2 pip2.7 pip2pi pip2tgz
官网说明:
pip2pi helps to alleviate these problems by making it blindingly simple to maintain a PyPI-compatible repository of packages your software depends on. To mirror a package and all of its requirements, use pip2tgz。Note that pip2tgz passes package arguments directly to pip, so packages can be specified in any format that pip recognizes:
[dave@www.cndba.cn Packages]# pip2pi --help
Usage: pip2pi TARGET [PIP_OPTIONS] PACKAGES ...
Adds packages PACKAGES to PyPI-compatible package index at TARGET.
If TARGET contains ':' it will be treated as a remote path. The
package index will be built locally and rsync will be used to copy
it to the remote host.
PIP_OPTIONS can be any options accepted by `pip install -d`, like
`--index-url` or `--no-use-wheel`.
For example, to create a remote index:
$ pip2pi example.com:/var/www/packages/ -r requirements.txt
To create a local index:
$ pip2pi ~/Sites/packages/ foo==1.2
2.3 配置要同步的pypi源
[dave@www.cndba.cn ~]# cat ~/.pip/pip.conf
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host= pypi.mirrors.ustc.edu.cn
[dave@www.cndba.cn ~]#
2.4 单个软件包同步
格式:pip2tgz /directory PackageName==version
[dave@www.cndba.cn Packages]# pip2tgz /work/pypi/Packages pbr==0.5.21
DEPRECATION: pip install --download has been deprecated and will be removed in the future. Pip now has a download command that should be used instead.
Collecting pbr==0.5.21
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/68/71/515cf4d6272a30a5203514a86dcf6a7fd563e8c58de7f81a0c0cace0a362/pbr-0.5.21.tar.gz (123kB)
100% |████████████████████████████████| 133kB 1.7MB/s
Saved ./pbr-0.5.21.tar.gz
Collecting pip>=1.0 (from pbr==0.5.21)
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/b6/ac/7015eb97dc749283ffdec1c3a88ddb8ae03b8fad0f0e611408f196358da3/pip-9.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 941kB/s
Saved ./pip-9.0.1-py2.py3-none-any.whl
Successfully downloaded pbr pip
Done. 2 new archives currently saved in '/work/pypi/Packages'.
[dave@www.cndba.cn Packages]# ls
pbr-0.5.21.tar.gz pip-9.0.1-py2.py3-none-any.whl pypiserver-1.2.0-py2.py3-none-any.whl
[dave@www.cndba.cn Packages]#
2.5 批量同步
如果需要同步的包很多,可以将包添加到requirements.txt文件中,一起同步:
格式:pip2tgz /directory -r ./requirements.txt
执行批量同步后,会依次将以下软件同步到指定的目录下:
[dave@www.cndba.cn work]# cat requirements.txt
pbr>=0.6,<1.0
SQLAlchemy>=0.7.8,<=0.9.99
amqplib>=0.6.1
anyjson>=0.3.3
argparse
boto>=2.12.0,!=2.13.0
[dave@www.cndba.cn work]# pip2tgz /work/pypi/Packages -r /work/requirements.txt
DEPRECATION: pip install --download has been deprecated and will be removed in the future. Pip now has a download command that should be used instead.
Collecting pbr<1.0,>=0.6 (from -r /work/requirements.txt (line 1))
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/a8/87/23e26858c1a45ff7ed352261e34fb99b33a97bfac0a6e5ece8df7c983d02/pbr-0.11.1-py2.py3-none-any.whl (79kB)
100% |████████████████████████████████| 81kB 1.1MB/s
Saved ./pypi/Packages/pbr-0.11.1-py2.py3-none-any.whl
Collecting SQLAlchemy<=0.9.99,>=0.7.8 (from -r /work/requirements.txt (line 2))
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/80/3c/6c3dca2ac92fa01e59642cd85a1b7178e75d8ce16ed7f211676226ca150c/SQLAlchemy-0.9.10.tar.gz (4.3MB)
100% |████████████████████████████████| 4.3MB 321kB/s
Saved ./pypi/Packages/SQLAlchemy-0.9.10.tar.gz
Collecting amqplib>=0.6.1 (from -r /work/requirements.txt (line 3))
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/75/b7/8c2429bf8d92354a0118614f9a4d15e53bc69ebedce534284111de5a0102/amqplib-1.0.2.tgz (58kB)
100% |████████████████████████████████| 61kB 3.7MB/s
Saved ./pypi/Packages/amqplib-1.0.2.tgz
Collecting anyjson>=0.3.3 (from -r /work/requirements.txt (line 4))
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/c3/4d/d4089e1a3dd25b46bebdb55a992b0797cff657b4477bc32ce28038fdecbc/anyjson-0.3.3.tar.gz
Saved ./pypi/Packages/anyjson-0.3.3.tar.gz
Collecting argparse (from -r /work/requirements.txt (line 5))
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl
Saved ./pypi/Packages/argparse-1.4.0-py2.py3-none-any.whl
Collecting boto!=2.13.0,>=2.12.0 (from -r /work/requirements.txt (line 6))
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/bd/b7/a88a67002b1185ed9a8e8a6ef15266728c2361fcb4f1d02ea331e4c7741d/boto-2.48.0-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 762kB/s
Saved ./pypi/Packages/boto-2.48.0-py2.py3-none-any.whl
Collecting pip (from pbr<1.0,>=0.6->-r /work/requirements.txt (line 1))
File was already downloaded /work/pypi/Packages/pip-9.0.1-py2.py3-none-any.whl
Successfully downloaded pbr SQLAlchemy amqplib anyjson argparse boto pip
Done. 6 new archives currently saved in '/work/pypi/Packages'.
[dave@www.cndba.cn work]# cd /work/pypi/Packages/
[dave@www.cndba.cn Packages]# ls
amqplib-1.0.2.tgz pbr-0.11.1-py2.py3-none-any.whl simple
anyjson-0.3.3.tar.gz pbr-0.5.21.tar.gz SQLAlchemy-0.9.10.tar.gz
argparse-1.4.0-py2.py3-none-any.whl pip-9.0.1-py2.py3-none-any.whl
boto-2.48.0-py2.py3-none-any.whl pypiserver-1.2.0-py2.py3-none-any.whl
[dave@www.cndba.cn Packages]#
2.6 建立索引
同步完成后,对所有包创建索引
命令:dir2pi /directory
[dave@www.cndba.cn Packages]# dir2pi /work/pypi/Packages/
[dave@www.cndba.cn Packages]#
2.7 安装web
安装pypiserver,如果有web服务器,如apache、nginx,就不要安装pypiserver。
[dave@www.cndba.cn Packages]# pip install pypiserver
Collecting pypiserver
Using cached https://mirrors.ustc.edu.cn/pypi/web/packages/55/a3/c01a39aafa6d9a94380e5014f66fd5274623a037911abdb65d17e8442e49/pypiserver-1.2.0-py2.py3-none-any.whl
Installing collected packages: pypiserver
Successfully installed pypiserver-1.2.0
[dave@www.cndba.cn Packages]#
[dave@www.cndba.cn yum.repos.d]# yum install -y httpd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
dave | 4.1 kB 00:00:00
(1/2): dave/group_gz | 136 kB 00:00:00
(2/2): dave/primary_db | 3.9 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-45.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================
Package Arch Version Repository Size
==========================================================================================================
Installing:
httpd x86_64 2.4.6-45.el7 dave 1.2 M
Installing for dependencies:
apr x86_64 1.4.8-3.el7 dave 103 k
apr-util x86_64 1.5.2-6.el7 dave 92 k
httpd-tools x86_64 2.4.6-45.el7 dave 84 k
Transaction Summary
=========================================================================================================
Install 1 Package (+3 Dependent packages)
Total download size: 1.4 M
Installed size: 4.3 M
Downloading packages:
---------------------------------------------------------------------------------------------------------
Total 19 MB/s | 1.4 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-3.el7.x86_64 1/4
Installing : apr-util-1.5.2-6.el7.x86_64 2/4
Installing : httpd-tools-2.4.6-45.el7.x86_64 3/4
Installing : httpd-2.4.6-45.el7.x86_64 4/4
Verifying : httpd-tools-2.4.6-45.el7.x86_64
Verifying : apr-util-1.5.2-6.el7.x86_64 2/4
Verifying : httpd-2.4.6-45.el7.x86_64 3/4
Verifying : apr-1.4.8-3.el7.x86_64 4/4
Installed:
httpd.x86_64 0:2.4.6-45.el7
Dependency Installed:
apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-45.el7
Complete!
[dave@www.cndba.cn yum.repos.d]#
2.8 配置http
[dave@www.cndba.cn /]# cat /etc/httpd/conf/httpd.conf
**修改该值到包目录:**
DocumentRoot "/work/pypi/Packages"
**添加以下内容:**
<Directory "/work/pypi/Packages">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
然后重启httpd:
[dave@www.cndba.cn /]# systemctl restart httpd.service
然后使用http://192.168.1.110/simple/ 作为pip源就可以直接使用了。
2.9 定时同步包
如果要定时同步包,可以使用bandersnatch 1.6.1
https://pypi.python.org/pypi/bandersnatch/1.6.1
该包可以同步最新的包。 全部同步大约需要50G的空间。
Configuration
• Run bandersnatch mirror - it will create an empty configuration file for you in /etc/bandersnatch.conf.
• Review /etc/bandersnatch.conf and adapt to your needs.
• Run bandersnatch mirror again. It will populate your mirror with the current status of all PyPI packages - roughly 50GiB at the time of writing.
• Run bandersnatch mirror regularly to update your mirror with any intermediate changes.
2.10 注意事项
配置之后,打开web时很容易出现You don’t have permission to access on this server的错误。
因为我们的测试平台是Redhat 7.3. httpd版本是2.4.6. 所以要按照上一步的配置来修改。 如果修改之后还出现权限问题,那么检查web目录的权限和所有者。 可以考虑赋予755并修改chmod o+x 的权限。
[dave@www.cndba.cn /]# rpm -qi httpd
Name : httpd
Version : 2.4.6
Release : 45.el7
Architecture: x86_64
Install Date: Sun 01 Oct 2017 01:49:00 PM CST
Group : System Environment/Daemons
Size : 3877656
License : ASL 2.0
Signature : RSA/SHA256, Wed 14 Sep 2016 03:02:21 PM CST, Key ID 199e2f91fd431d51
Source RPM : httpd-2.4.6-45.el7.src.rpm
Build Date : Wed 03 Aug 2016 08:34:57 PM CST
Build Host : x86-020.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor : Red Hat, Inc.
URL : http://httpd.apache.org/
Summary : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.
[dave@www.cndba.cn /]#
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 上一篇:Linux 升级 Python 并修改默认版本
- 下一篇:为什么越优秀的人越勤奋?