python虚拟开发环境搭建
目录:
python版本问题
开发环境-pyenv
虚拟环境-virtualenv
一. python版本
2.x:发展到了2.7.13
3.x:发展到了3.63.x始自2008年,3.5版本发布于2015年
https://pythonclock.org 2.x的倒计时
不要迷信版本,学会一个版本,好好学一门语言,其他都不是问题。
二. 开发环境 - pyenv
pyenv是python的多版本管理工具:
管理python解释器
管理python版本
管理python的虚拟环境
官网:https://github.com/pyenv/pyenv
1.安装pyenv:
挂载光盘,配置本地yum源:
[root@ames ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@ames ~]# vim /etc/yum.repos.d/cdrom.repo
[cdrom]
name=cdrom
baseurl=file:///media
gpgcheck=0
enable=1
:wq
安装git:
[root@ames ~]# yum -y install git
安装python依赖包:
[root@ames ~]# yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
创建python用户:
[root@ames ~]# useradd python
使用python用户登录,然后执行:
[root@ames ~]# su -l python[python@ames ~]$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
上面命令执行完后结尾处会有如下提示:
WARNING: seems you still have not added 'pyenv' to the load path.# Load pyenv automatically by adding# the following to ~/.bash_profile:export PATH="/home/python/.pyenv/bin:$PATH"eval "$(pyenv init -)"eval "$(pyenv virtualenv-init -)"
执行该操作:
[python@ames ~]$ vim .bash_profile
...
# 结尾处追加如下:
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
使其生效:
[python@ames ~]$ source .bash_profile
此时,pyenv已安装完成:
[python@ames ~]$ pyenv
pyenv 1.1.3
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
[python@ames ~]$
查看帮助信息:
[python@ames ~]$ pyenv help
[python@ames ~]$ pyenv help install
列出所有可用的python版本及python扩展工具:
[python@ames ~]$ pyenv install --list
此处选择python3.5.3进行安装:
[python@ames ~]$ pyenv install 3.5.3
Downloading Python-3.5.3.tar.xz...
-> https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
error: failed to download Python-3.5.3.tar.xz
BUILD FAILED (CentOS Linux 7 using python-build 20160602)
[python@ames ~]$
如上命令执行报错,解决办法如下:
使用缓存方式安装:
在~/.pyenv目录下,新建cache目录,放入下载好的版本文件。
[python@ames ~]$ mkdir .pyenv/cache
[python@ames ~]$ cp /mnt/tools/{Python-3.5.3.tar.gz,Python-3.5.3.tgz,Python-3.5.3.tar.xz} .pyenv/cache
[python@ames ~]$ ls .pyenv/cache/
Python-3.5.3.tar.gz Python-3.5.3.tar.xz Python-3.5.3.tgz
[python@ames ~]$
注: 因为不确定 pyenv install 会选择哪个python文件进行安装,所以把准备的三个安装包都放进去.
再次安装:
[python@ames ~]$ pyenv install 3.5.3 -v
查看:
[python@ames ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.5.3
[python@ames ~]$
设置python版本:
[python@ames ~]$ pyenv help
Usage: pyenv <command> [<args>]
...
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
...
glocal:
设置全局的python版本,通过将版本号写入~/.pyenv/version文件的方式。
[python@ames ~]$ pyenv global 3.5.3
local:
设置面向程序的本地版本,通过将版本号写入当前目录下的.python-version文件的方式。这种方式设置的python版本优先级比global高,pyenv会从当前目录开始向上逐级查找.python-version文件,知道根目录为止,若找不到,就用global版本。使用pyenv local设置从当前工作目录开始向下递归都继承这个设置。
[python@ames ~]$ pyenv local 3.5.3
shell:
设置面向shell的python版本,只作用于当前会话,通过设置当前shell的PYENV_VERSION环境变量的方式。
[python@ames ~]$ pyenv shell 3.5.3
virtualenv:
前面说了pyenv可以帮助你在一台开发机上建立多个版本的python环境, 并提供方便的切换方法。
virtualenv则提供了一种功能, 就是将一个目录建立为一个虚拟的python环境, 这样的话, 用户可以建立多个虚拟环境, 每个环境里面的python版本可以是不同的, 也可以是相同的, 而且环境之间相互独立。
virtualenv插件的位置:
[python@ames ~]$ ll .pyenv/plugins/pyenv-virtualenv/
total 32
drwxrwxr-x. 2 python python 244 Sep 12 03:10 bin
-rw-rw-r--. 1 python python 6370 Sep 12 03:10 CHANGELOG.md
drwxrwxr-x. 3 python python 21 Sep 12 03:10 etc
-rwxrwxr-x. 1 python python 552 Sep 12 03:10 install.sh
-rw-rw-r--. 1 python python 1058 Sep 12 03:10 LICENSE
-rw-rw-r--. 1 python python 9190 Sep 12 03:10 README.md
drwxrwxr-x. 2 python python 40 Sep 12 03:10 shims
drwxrwxr-x. 4 python python 4096 Sep 12 03:10 test
[python@ames ~]$
为便于实验,我又安装了python3.6.1版本,安装方法见上文。
先查看一下:
[python@ames ~]$ pyenv versions
system
* 3.5.3 (set by PYENV_VERSION environment variable)
3.6.1
[python@ames ~]$
创建一个python3.5.3的虚拟环境:
[python@ames ~]$ pyenv virtualenv 3.5.3 env353
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5.3/envs/env353/lib/python3.5/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5.3/envs/env353/lib/python3.5/site-packages
[python@ames ~]$
查看:
[python@ames ~]$ pyenv versions
system
* 3.5.3 (set by PYENV_VERSION environment variable)
3.5.3/envs/env353
3.6.1
env353
[python@ames ~]$
env353的真实目录位于:
[python@ames ~]$ ls .pyenv/versions -al
total 4
drwxrwxr-x. 4 python python 46 Sep 12 04:06 .
drwxrwxr-x. 13 python python 4096 Sep 12 03:48 ..
drwxr-xr-x. 7 python python 68 Sep 12 04:06 3.5.3
drwxr-xr-x. 6 python python 56 Sep 12 04:02 3.6.1
lrwxrwxrwx. 1 python python 46 Sep 12 04:06 env353 -> /home/python/.pyenv/versions/3.5.3/envs/env353
[python@ames ~]$
再创建一个基于3.6.1的虚拟环境:
[python@ames ~]$ pyenv virtualenv 3.6.1 env361
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.1/envs/env361/lib/python3.6/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.1/envs/env361/lib/python3.6/site-packages
[python@ames ~]$ pyenv versions
system
* 3.5.3 (set by PYENV_VERSION environment variable)
3.5.3/envs/env353
3.6.1
3.6.1/envs/env361
env353
env361
[python@ames ~]$
查看真实目录:
[python@ames ~]$ ll .pyenv/versions
total 0
drwxr-xr-x. 7 python python 68 Sep 12 04:06 3.5.3
drwxr-xr-x. 7 python python 68 Sep 12 04:11 3.6.1
lrwxrwxrwx. 1 python python 46 Sep 12 04:06 env353 -> /home/python/.pyenv/versions/3.5.3/envs/env353
lrwxrwxrwx. 1 python python 46 Sep 12 04:11 env361 -> /home/python/.pyenv/versions/3.6.1/envs/env361
[python@ames ~]$
删除虚拟环境:
如果要删除这个虚拟环境,只须直接删除它所在的目录即可:
[python@ames ~]$ pyenv versions
system
* 3.5.3 (set by /home/python/.python-version)
3.5.3/envs/env353
3.6.1
3.6.1/envs/env361
env353
env361
[python@ames ~]$ rm -rf .pyenv/versions/env361
[python@ames ~]$ pyenv versions
system
* 3.5.3 (set by /home/python/.python-version)
3.5.3/envs/env353
3.6.1
3.6.1/envs/env361
env353
[python@ames ~]$
使用pip命令在env353的虚拟环境下安装ipython:
首先修改pip源,改成国内的:
(env353) [python@ames ~]$ mkdir .pip
(env353) [python@ames ~]$ vim .pip/pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
:wq
安装ipython:
(env353) [python@ames ~]$ pip install ipython
(env353) [python@ames ~]$ ipython
Python 3.5.3 (default, Sep 12 2017, 03:35:37)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
安装Jupyter,也会帮安装ipython的:
Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等。【来自百度百科】
安装:
(env353) [python@ames ~]$ pip install jupyter
查看帮助信息:
(env353) [python@ames ~]$ jupyter notebook help
开启服务进程:
(env353) [python@ames ~]$ jupyter notebook --ip=0.0.0.0 --no-browser
[I 04:51:53.222 NotebookApp] Writing notebook server cookie secret to /home/python/.local/share/jupyter/runtime/notebook_cookie_secret
[I 04:51:53.279 NotebookApp] Serving notebooks from local directory: /home/python
[I 04:51:53.279 NotebookApp] 0 active kernels
[I 04:51:53.279 NotebookApp] The Jupyter Notebook is running at: http://0.0.0.0:8888/?token=d5f49225271fb3bdb44091a318af541cc054c377c829374d
[I 04:51:53.279 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 04:51:53.280 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://0.0.0.0:8888/?token=d5f49225271fb3bdb44091a318af541cc054c377c829374d
关闭防火墙后在浏览器访问:
[root@ames ~]# iptables -F
访问: http://172.16.251.184:8888
输入token: d5f49225271fb3bdb44091a318af541cc054c377c829374d
打包env353开发环境:
(env353) [python@ames ~]$ pip freeze > requirement.txt
安装env353开发环境到新的虚拟环境:
(env361) [python@ames ~]$ pip install -r requirement.txt
完!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341