centos7部署python3虚拟环境

七彩网络

本站赞助商:西昆云
首页>> 京东云 >>centos7部署python3虚拟环境

服务器

一、CentOS 上部署Python3
1、安装依赖包

yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel curl epel-release gcc

2、官网下载python原码包

curl -o python3.6.tgz https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

3、解压安装

tar -xf Python-3.6.5.tgz -C /usr/local/src/

4、修改配置文件 
首先进入上一步解压的目录

cd /usr/local/src/Python-3.6.5/

执行如下命令,取消对应位置的注释

sed -ri 's/^#readline/readline/' Modules/Setup.dist

sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist

sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist

sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist

sed -ri 's/^#([\t]*-L\$(SSL))/\1/' Modules/Setup.dist

5、开始编译安装

./configure --enable-shared

--enable-shared 指定安装共享库,共享库在使用用其他需调用用python的软件时会用用到,比比如使用用mod_wgsi连接Apache与python时需要

make -j 2 && make install

-j cpu核心心数

6、配置共享库文文件

1)为所有用用户设置共享库目目录
#cd /usr/local/src/Python-3.6.5/
#cp libpython3.6m.so.1.0 /usr/local/lib64/
#cp libpython3.6m.so.1.0 /usr/lib/
#cp libpython3.6m.so.1.0 /usr/lib64/
2)刷新环境变量
#source /etc/profile

7、测试python

python3

Python 3.6.5 (default, Mar 29 2019, 17:13:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>> exit()

8、测试 pip3

pip3 -V

pip 9.0.3 from /usr/local/lib/python3.6/site-packages (python 3.6)

二、使用第三方的虚拟工具 Virtualenvwrapper
2.1 安装virtualenvwrapper 提供了一系列命令使得和虚拟环境⼯作变得愉快许多。它把你所有的虚拟环境都放在一个地方。更重要的是,你只需运行一个命令即可进入到你的虚拟环境,就不必像之前需要先进如虚拟环境的目录下安装(确保 virtualenv 已经安装了)

pip3 install virtualenvwrapper

2.2 修改默认的环境目录:

cat ~/.bashrc

.bashrc

User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Source global definitions

if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
#指定解释器
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.6
#这个可以自定义 虚拟环境存放目录
export WORKON_HOME=~/.virtualenv
#项目存放目录,需要自己创建
export PROJECT_HOME=~/.virtualenv/project_data
#自动加载virtualenvmrapper
source /usr/local/bin/virtualenvwrapper.sh

2.3 创建存放虚拟环境的目录
mkdir ~/.virtualenv

此目录应该和上面的 WORKON_HOME 变量的值相同

2.4 初始化一下

source ~/.bashrc

2.5 基本使用: 创建一个虚拟环境:

mkvirtualenv test

2.6 退出虚拟环境:
deactivate
[root@redis ld.so.conf.d]# workon test
(test) [root@redis test]# deactivate

2.7 进入虚拟环境:
[root@redis ld.so.conf.d]# workon 
python3
test
[root@redis ld.so.conf.d]# workon test
(test) [root@redis test]#

2.8 创建项目:
(test) [root@redis test]# mkproject test1
Using base prefix '/usr/local'
New python executable in /root/.virtualenv/test1/bin/python3.6
Also creating executable in /root/.virtualenv/test1/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /root/.virtualenv/test1/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/test1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/test1/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/test1/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/test1/bin/get_env_details
Creating /root/.virtualenv/project_data/test1
Setting project for test1 to /root/.virtualenv/project_data/test1
(test1) [root@redis test1]# ls
(test1) [root@redis test1]# ls /root/.virtualenv/project_data/
test test1

2.9 列出可用的运行环境:
(test1) [root@redis test1]# lsvirtualenv
python3

test1

test

2.10列出当前环境安装的包:
lssitepackages:
(test1) [root@redis test1]# lssitepackages
easy_install.py pip-19.0.3.dist-info pycache setuptools-40.8.0.dist-info wheel-0.33.1.dist-info
pip pkg_resources setuptools wheel

2.11 创建临时运行环境:
mktmpenv
[root@redis test1]# mktmpenv 
Using base prefix '/usr/local'
New python executable in /root/.virtualenv/tmp-4aa130f4957b04a/bin/python3.6
Also creating executable in /root/.virtualenv/tmp-4aa130f4957b04a/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /root/.virtualenv/tmp-4aa130f4957b04a/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/tmp-4aa130f4957b04a/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/tmp-4aa130f4957b04a/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/tmp-4aa130f4957b04a/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenv/tmp-4aa130f4957b04a/bin/get_env_details
This is a temporary environment. It will be deleted when you run 'deactivate'.

2.12 临时环境退出即删除:
(tmp-4aa130f4957b04a) [root@redis tmp-4aa130f4957b04a]# deactivate 
Removing temporary environment: tmp-4aa130f4957b04a
Removing tmp-4aa130f4957b04a...

2.13 删除虚拟环境:
rmvirtualenv
[root@redis .virtualenv]# workon 
python3
test1
test
[root@redis .virtualenv]# rmvirtualenv python3
Removing python3...
[root@redis .virtualenv]# workon 
test1
test

2.14 在虚拟环境安装包
所有的虚拟环境都在 ~/.virtualenv/project_data下,都是独立的,互不影响,在虚拟环境中安装包,使用pip即可,无需root权限
[root@redis .virtualenv]# workon test
(test) [root@redis test]# pip install selenium
Collecting selenium
Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
100% |████████████████████████████████| 911kB 15kB/s 
Collecting urllib3 (from selenium)
Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
100% |████████████████████████████████| 122kB 11kB/s 
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.24.1

(test) [root@redis test]# pip list
Package Version


pip 19.0.3 
selenium 3.141.0
setuptools 40.8.0 
urllib3 1.24.1 
wheel 0.33.1
(test) [root@redis test]# deactivate

切换到env_python3.6查看《没有selenium包》:
[root@redis test]# workon test1
(test1) [root@redis test1]# pip list
Package Version


pip 19.0.3 
setuptools 40.8.0 
wheel 0.33.1



×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:云服务器 » centos7部署python3虚拟环境

网友评论(0)