環境如下:
系統版本 | 普通使用者 | 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-..tgz # 下面以安裝Python-3.5.2.tgz為例
2、解壓Python-3.5.2.tgz。
[lucy@min software]$ tar xf Python-..tgz
[lucy@min software]$ ls
Python-. Python-..tgz
3、編譯安裝Python-3.5.2到/home/lucy/python352下。
[[email protected] 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-.]$ su - root
Password:
[root@min ~]# yum install gcc -y
重新編譯安裝Python-3.5.2:
[lucy@min Python-]$ ./configure --prefix=/home/lucy/python352
[lucy@min Python-]$ make
[lucy@min Python-]$ make install
......
Ignoring ensurepip failure: pip requires SSL/TLS
make install到最後提示“failure:pip 8.1.1 requires SSL/TLS”,在安裝編譯Python-3.5.2時預設也會安裝pip,但pip依賴于SSL/TLS,故得先安裝openssh-devel。
[[email protected] Python-3.5.2]$ su - root
Password:
[[email protected] ~]# yum install openssl-devel -y
重新"make install":
[[email protected] ~]# exit
[[email protected] Python-3.5.2]$ make install
......
Successfully installed pip-8.1.1 setuptools-20.10.1
[[email protected] python352]$ ls
bin include lib share
[[email protected] python352]$ ls bin/
total 24032
lrwxrwxrwx 1 lucy lucy 8 Aug 16 22:28 2to3 -> 2to3-3.5
-rwxrwxr-x lucy lucy Aug : to3-
-rwxrwxr-x lucy lucy Aug : easy_install-
lrwxrwxrwx 1 lucy lucy 7 Aug 16 22:28 idle3 -> idle3.5
-rwxrwxr-x lucy lucy Aug : idle3.
-rwxrwxr-x lucy lucy Aug : pip3
-rwxrwxr-x lucy lucy Aug : pip3.
lrwxrwxrwx 1 lucy lucy 8 Aug 16 22:28 pydoc3 -> pydoc3.5
-rwxrwxr-x lucy lucy Aug : pydoc3.
lrwxrwxrwx 1 lucy lucy 9 Aug 16 22:28 python3 -> python3.5
-rwxr-xr-x lucy lucy Aug : python3.
lrwxrwxrwx 1 lucy lucy 17 Aug 16 22:28 python3.5-config -> python3.5m-config
-rwxr-xr-x lucy lucy Aug : python3.m
-rwxr-xr-x lucy lucy Aug : python3.m-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 lucy lucy Aug : pyvenv-
4、安裝virtualenvwrapper。
[lucy@min bin]$ ./pip3 install virtualenvwrapper
Collecting virtualenvwrapper
......
Successfully installed pbr-. six-. stevedore-. virtualenv-. virtualenv-clone-. virtualenvwrapper-.
[lucy@min bin]$ ls -l virtualenvwrapper.sh
-rwxrwxr-x lucy lucy Aug : virtualenvwrapper.sh
安裝完成後會在目前目錄下生成virtualenvwrapper.sh,在使用virtualenvwrapper時, 需要将virtualenvwrapper.sh的資訊讀入目前的shell環境。
5、配置lucy使用者的.bashrc檔案。
[[email protected] ~]$ 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配置檔案。
[[email protected] ~]$ 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建立工作空間。
[[email protected] ~]$ 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 (default, Aug , ::)
[GCC (Red Hat -)] 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) [[email protected] ~]$ pip list
pip (.)
setuptools (.)
wheel (.)