环境如下:
系统版本 | 普通用户 | Python版本 |
---|---|---|
CentOS Linux release 7.1.1503 (Core) | lucy | Python 3.5.2 |
搭建步骤:
1、下载Python源码包,附上官网下载链接:https://www.python.org/downloads/。
[lucy@min software]$ ls
Python-3.5.2.tgz # 下面以安装Python-3.5.2.tgz为例
2、解压Python-3.5.2.tgz。
[lucy@min software]$ tar xf Python-3.5.2.tgz
[lucy@min software]$ ls
Python-3.5.2 Python-3.5.2.tgz
3、编译安装Python-3.5.2到/home/lucy/python352下。
[lucy@min Python-3.5.2]$ ./configure --prefix=/home/lucy/python352
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
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... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home/lucy/software/Python-3.5.2':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
configure时报错“no acceptable C compiler found in $PATH”,在环境变量$PATH下没找到可用的C编译器,安装GCC编译器便可解决。
[lucy@min Python-3.5.2]$ su - root
Password:
[root@min ~]# yum install gcc -y
重新编译安装Python-3.5.2:
[lucy@min Python-3.5.2]$ ./configure --prefix=/home/lucy/python352
[lucy@min Python-3.5.2]$ make
[lucy@min Python-3.5.2]$ make install
......
Ignoring ensurepip failure: pip 8.1.1 requires SSL/TLS
make install到最后提示“failure:pip 8.1.1 requires SSL/TLS”,在安装编译Python-3.5.2时默认也会安装pip,但pip依赖于SSL/TLS,故得先安装openssh-devel。
[lucy@min Python-3.5.2]$ su - root
Password:
[root@min ~]# yum install openssl-devel -y
重新"make install":
[root@min ~]# exit
[lucy@min Python-3.5.2]$ make install
......
Successfully installed pip-8.1.1 setuptools-20.10.1
[lucy@min python352]$ ls
bin include lib share
[lucy@min python352]$ ls bin/
total 24032
lrwxrwxrwx 1 lucy lucy 8 Aug 16 22:28 2to3 -> 2to3-3.5
-rwxrwxr-x 1 lucy lucy 111 Aug 16 22:28 2to3-3.5
-rwxrwxr-x 1 lucy lucy 251 Aug 16 22:29 easy_install-3.5
lrwxrwxrwx 1 lucy lucy 7 Aug 16 22:28 idle3 -> idle3.5
-rwxrwxr-x 1 lucy lucy 109 Aug 16 22:28 idle3.5
-rwxrwxr-x 1 lucy lucy 223 Aug 16 22:29 pip3
-rwxrwxr-x 1 lucy lucy 223 Aug 16 22:29 pip3.5
lrwxrwxrwx 1 lucy lucy 8 Aug 16 22:28 pydoc3 -> pydoc3.5
-rwxrwxr-x 1 lucy lucy 94 Aug 16 22:28 pydoc3.5
lrwxrwxrwx 1 lucy lucy 9 Aug 16 22:28 python3 -> python3.5
-rwxr-xr-x 2 lucy lucy 12284743 Aug 16 22:26 python3.5
lrwxrwxrwx 1 lucy lucy 17 Aug 16 22:28 python3.5-config -> python3.5m-config
-rwxr-xr-x 2 lucy lucy 12284743 Aug 16 22:26 python3.5m
-rwxr-xr-x 1 lucy lucy 3090 Aug 16 22:28 python3.5m-config
lrwxrwxrwx 1 lucy lucy 16 Aug 16 22:28 python3-config -> python3.5-config
lrwxrwxrwx 1 lucy lucy 10 Aug 16 22:28 pyvenv -> pyvenv-3.5
-rwxrwxr-x 1 lucy lucy 246 Aug 16 22:28 pyvenv-3.5
4、安装virtualenvwrapper。
[lucy@min bin]$ ./pip3 install virtualenvwrapper
Collecting virtualenvwrapper
......
Successfully installed pbr-1.10.0 six-1.10.0 stevedore-1.17.0 virtualenv-15.0.3 virtualenv-clone-0.2.6 virtualenvwrapper-4.7.1
[lucy@min bin]$ ls -l virtualenvwrapper.sh
-rwxrwxr-x 1 lucy lucy 41384 Aug 16 22:46 virtualenvwrapper.sh
安装完成后会在当前目录下生成virtualenvwrapper.sh,在使用virtualenvwrapper时, 需要将virtualenvwrapper.sh的信息读入当前的shell环境。
5、配置lucy用户的.bashrc文件。
[lucy@min ~]$ vim .bashrc
# 在脚本的最后添加如下语句
if [ -f /home/lucheng/python352/bin/virtualenvwrapper.sh ]; then
export WORKON_HOME=$HOME/.virtualenvs
source /home/lucy/python352/bin/virtualenvwrapper.sh
fi
添加完之后记得重新加载.bashrc配置文件。
[lucy@min ~]$ source .bashrc
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
提示没有正确设置VIRTUALENVWRAPPER_PYTHON环境变量的值,需要在.bashrc配置文件中添加VIRTUALENVWRAPPER_PYTHON的值:
if [ -f /home/lucy/python352/bin/virtualenvwrapper.sh ]; then
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=$HOME/python352/bin/python3
source /home/lucy/python352/bin/virtualenvwrapper.sh
fi
再重新加载.bashrc配置文件即可。
6、使用mkvirtualenv创建工作空间。
[lucy@min ~]$ mkvirtualenv --python=/home/lucy/python352/bin/python3 lucy-python3
which: no virtualenv in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/lucy/.local/bin:/home/lucy/bin)
ERROR: virtualenvwrapper could not find virtualenv in your path
提示报错:virtualenvwrapper在$PATH中找不到virtualenv,只需要把刚刚安装的/home/lucy/python352/bin添加到$PATH中即可。
[lucy@min ~]$ vim .bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/python352/bin
[lucy@min ~]$ source .bash_profile
[lucy@min ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/lucy/.local/bin:/home/lucy/bin:/home/lucy/.local/bin:/home/lucy/bin:/home/lucy/python352/bin
[lucy@min ~]$ mkvirtualenv --python=/home/lucy/python352/bin/python3 lucy-python3
Running virtualenv with interpreter /home/lucy/python352/bin/python3
Using base prefix '/home/lucy/python352'
New python executable in /home/lucy/.virtualenvs/lucy-python3/bin/python3
Also creating executable in /home/lucy/.virtualenvs/lucy-python3/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/lucy/.virtualenvs/lucy-python3/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/lucy/.virtualenvs/lucy-python3/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/lucy/.virtualenvs/lucy-python3/bin/preactivate
virtualenvwrapper.user_scripts creating /home/lucy/.virtualenvs/lucy-python3/bin/postactivate
virtualenvwrapper.user_scripts creating /home/lucy/.virtualenvs/lucy-python3/bin/get_env_details
(lucy-python3) [lucy@min ~]$ python
Python 3.5.2 (default, Aug 16 2016, 22:07:02)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
可以看到,mkvirtualenvwrapper创建完工作空间后会直接跳到工作空间中,若是想退出工作空间,可使用deactivate命令。
(lucy-python3) [lucy@min ~]$ deactivate
[lucy@min ~]$
当需要编程时,便又需进入工作空间,可使用workon命令进入某个工作空间。
[lucy@min ~]$ workon lucy-python3
(lucy-python3) [lucy@min ~]$
进入工作空间后,便可以随便安装包,在工作空间中安装的包并不会对系统有任何影响,换句话说,安装的包只在工作空间内可用,若是退出了工作空间,便无法使用了。
有时想要查看到底安装了哪些包,可以使用pip list命令进行查看。
(lucy-python3) [lucy@min ~]$ pip list
pip (8.1.2)
setuptools (25.2.0)
wheel (0.29.0)