在Centos6.7中建立Python爬虫的虚拟环境,安装virtualenv和virtualenvwrapper.sh

说明:本人的里linux的系统为Centos6.7,里面默认的python版本为python2.6.6,直接输入命令:python,就可以看到。但是python2我想使用2.7.13,python3相使用3.5.3版本。如果我们直接将内置的python2.6.6版本改掉,就出现一个问题,就是系统中的yum命令就不能用,这是很不方便的。于是我们独立的安装python2.7.13和3.5.3.然后使用工具virtualenv和virtualenvwrapper.sh,分别建立相应的python版本的虚拟环境。

注意:为了方便,我的操作都是在root用户下进行;

一、安装python2.7.3

1.配置jdk的环境变量:                    

export JAVA_HOME=/usr/local/jdk1.8.0_73

export PATH=$PATH:$JAVA_HOME/bin

2.下载python2.7.13和python3.5.3对于的版本,两种方式,

一种是下载到本地:地址为:https://www.python.org/ftp/python/

 

或者直接在linux中输入

python2.7.13版本:wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz

python3.5.3版本:wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tgz

3.下载编译python所需要的依赖,直接输入命令

 

[root@python ~]#yum groupinstall "Development tools"
[root@python ~]#yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel groupinstall "Development tools"
[root@python ~]#yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel

4.编译安装python2.7.13

 

[root@python ~]#tar -zxvf Python-2.7.13.tgz
[root@python ~]#cd Python-2.7.13
[root@python Python-2.7.13]#./configure --prefix=/usr/local
[root@python Python-2.7.13]#make && make altinstall2.7.13.tgz
[root@python ~]#cd Python-2.7.13
[root@python Python-2.7.13]#./configure --prefix=/usr/local
[root@python Python-2.7.13]#make && make altinstall

注意:运用make altinstall而不是make install,这样跟系统的2.6就分别在不同的路径下,不会有冲突。python2.7.13在/usr/local/bin下,python2.6.6在/usr/bin下

 

输入:

 

[root@python ~]# python2.7
Python 2.7.13 (default, Jun 25 2018, 15:28:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

安装完成;

二、python3.5.3

之前下载过依赖和python3.5.3.tgz包,所以直接编译安装:

注意:我们在这里建立一个新的local路径,将编译的python3.5.3放在里面,之前/usr/local/里面放了编译的python2.7.13,这样做也是为了两个版本之间冲突

 

 
[root@python ~]#mkdir /usr/local/local 
[root@python ~]#tar -zxvf Python-3.5.3.tgz
[root@python ~]#cd Python-3.5.33.5.3.tgz
[root@python ~]#cd Python-3.5.3
[root@python Python-3.5.3]#./configure --prefix=/usr/local/local
[root@python Python-3.5.3]#make && make altinstall3.5.3]#./configure --prefix=/usr/local/local
[root@python Python-3.5.3]#make && make altinstall

python3.5.3需要配置环境变量:

 

[root@python ~]# vi /etc/profile

添加环境变量:

 export PYTHON_HOME=/usr/local/local

 export PATH=$PATH:$PYTHON_HOME/bin

输入:

 

[root@python ~]# source /etc/profile
[root@python ~]# python3.5
Python 3.5.3 (default, Jun 25 2018, 16:03:47) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

到此python2.7.13和python3.5.3安装完成

 

三、安装virtualenv和virtualenvwrapper.sh

1.需要用到pip进行安装,所以先安装pip:

命令:

 

[root@python Python-2.7.13]# wget https://bootstrap.pypa.io/get-pip.py -o get-pip.py[root@python Python-2.7.13
[root@python Python-2.7.13]# python2.7  get-pip.py

注意:这里可能会报错。

 

[root@python Python-2.7.13]# python2.7 get-pip.py        
  File "get-pip.py", line 1
    --2018-06-25 15:39:51--  https://bootstrap.pypa.io/get-pip.py
                  ^
SyntaxError: invalid syntax

这时候看看路径下是不是有多个get-pip.py   如下图     

 

-rw-r--r--.  1 root root     2900 Jun 25 15:39 get-pip.py
-rw-r--r--.  1 root root  1642329 Apr 24 06:15 get-pip.py.1

 

这时候把get-pip.py改为get-pip.py.1文件,

就可以了:

[root@python Python-2.7.13]# python2.7 get-pip.py.1 


2.安装好pip,进行virtualenv安装

安装virtualenv:

 

[root@python ~]#yum install epel-release -y

[root@python ~]#yum install python-virtualenv -y

测试:

 

[root@python ~]# virtualenv testfile2
New python executable in /root/test/bin/python2.7
Also creating executable in /root/test/bin/python
Installing setuptools, pip, wheel...done.

我们也可以指定python3.5来建立:

[root@python ~]# virtualenv --python=/usr/local/local/bin/python3.5 testfile3
Running virtualenv with interpreter /usr/local/local/bin/python3.5
Using base prefix '/usr/local/local'
New python executable in /root/testfile3/bin/python3.5
Also creating executable in /root/testfile3/bin/python
Installing setuptools, pip, wheel...done.

到此virtualenv安装完成。

3.virtualenvwapper.sh安装,输入命令

 

[root@python ~]#pip install virtualenvwrapperpip install virtualenvwrapper
找到virtualenvwrapper.sh的安装路径virtualenvwrapper.sh的安装路径
 
[root@python ~]# find / -name virtualenvwrapper.sh 
/usr/local/bin/virtualenvwrapper.sh
运行一下:
[root@python ~]# source /usr/local/bin/virtualenvwrapper.sh[root@python ~]# source /usr/local/bin/virtualenvwrapper.sh
[root@python ~]# mkvirtualenv test
New python executable in /usr/local/local/bin/python2.7
Traceback (most recent call last):
 
 
  File "/usr/local/bin/virtualenv", line 11, in 
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 712, in main
    symlink=options.symlink)
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 927, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 1233, in install_python
    shutil.copyfile(executable, py_executable)
  File "/usr/local/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 26] Text file busy: '/root/Envs/test/bin/python2.7'
注意:这里可能会有报错的产生;New python executable in /root/Envs/test/bin/python2.7,因为我们安装了python2.7.13,
所以就不用原来的python2.6.6版本,那我们就将新的python2.7配到环境变量里
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7

我们在环境变量里配置虚拟环境的存储路径,virtualenvwrapper.sh的路径,virtualenv路径,方便以后操作
最后的环境变量配置如图:

 
 

jdk路径: 

export JAVA_HOME=/usr/local/jdk1.8.0_73 

export PATH=$PATH:$JAVA_HOME/bin

python3.5.3路径: 

export PYTHON_HOME=/usr/local/local export 

PATH=$PATH:$PYTHON_HOME/bin

虚拟环境的存储位置,~/Envs需要手动创建:

export WORKON_HOME=~/Envs

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv

用于每次开机启动:

source /usr/local/bin/virtualenvwrapper.sh

到此,virtualenvwrapper.sh安装完毕;

4.测试使用:

配置完环境变量都要输入命令,使其配置生效:

[root@python ~]# source /etc/profile

下次启动就不需要了!会自动启动virtualenvwrapper.sh

 

创建test2后,会自动进行test2虚拟环境,不加限制条件,默认是python2,.7.13创建:

[root@python ~]# mkvirtualenv test2

New python executable in /root/Envs/test2/bin/python2.7
Not overwriting existing python script /root/Envs/test2/bin/python (you must use /root/Envs/test2/bin/python2.7)

Installing setuptools, pip, wheel...done.

(test2) [root@python bin]# pwd

/root/Envs/test2/bin

 

[root@python bin]# source activate

 

当我们用python2.7.13创建完成后,我们进入到test2的bin目录下,之间输入python,就会为我们进入python2.7.13

(test2) [root@python bin]# python
Python 2.7.13 (default, Jun 25 2018, 15:28:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> 

如果想知名python3.5.3创建的虚拟环境:

(test2) [root@python Envs]# mkvirtualenv --python=/usr/local/local/bin/python3.5 test3 

     Running virtualenv with interpreter /usr/local/local/bin/python3.5

     Using base prefix '/usr/local/local'
     New python executable in /root/Envs/test3/bin/python3.5
     Also creating executable in /root/Envs/test3/bin/python
    Installing setuptools, pip, wheel...done.
    virtualenvwrapper.user_scripts creating /root/Envs/test3/bin/predeactivate
    virtualenvwrapper.user_scripts creating /root/Envs/test3/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /root/Envs/test3/bin/preactivate
    virtualenvwrapper.user_scripts creating /root/Envs/test3/bin/postactivate
   virtualenvwrapper.user_scripts creating /root/Envs/test3/bin/get_env_details

 

进行test3的bin目录下,直接输入python,这是给你们显示的Python 3.5.3版本:

(test3) [root@python bin]# python
Python 3.5.3 (default, Jun 25 2018, 16:03:47) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

 

workon展示创建的所以虚拟环境:

(test3) [root@python bin]# workon

  py2scrapy
  py3scrapy
  test2
  test3
  test

 

在创建的虚拟环境目录的bin目录下,输入source activate,进行虚拟环境:

[root@python bin]# source activate

(test3) [root@python bin]# 

 

deactivate退出当前虚拟环境:

(test2) [root@python ~]# deactivate
[root@python ~]# 

 

 

 

 

 

 

 

 

你可能感兴趣的:(python爬虫,linux虚拟环境)