python虚拟开发环境搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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源:
[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) [[email protected] ~]# vim /etc/yum.repos.d/cdrom.repo [cdrom] name=cdrom baseurl=file:///media gpgcheck=0 enable=1 :wq
安装git:
[[email protected] ~]# yum -y install git
安装python依赖包:
[[email protected] ~]# yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
创建python用户:
[[email protected] ~]# useradd python
使用python用户登录,然后执行:
[[email protected] ~]# su -l python[[email protected] ~]$ 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 -)"
执行该操作:
[[email protected] ~]$ vim .bash_profile ... # 结尾处追加如下: export PATH="/home/python/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
使其生效:
[[email protected] ~]$ source .bash_profile
此时,pyenv已安装完成:
[[email protected] ~]$ 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 [[email protected] ~]$
查看帮助信息:
[[email protected] ~]$ pyenv help [[email protected] ~]$ pyenv help install
列出所有可用的python版本及python扩展工具:
[[email protected] ~]$ pyenv install --list
此处选择python3.5.3进行安装:
[[email protected] ~]$ 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) [[email protected] ~]$
如上命令执行报错,解决办法如下:
使用缓存方式安装:
在~/.pyenv目录下,新建cache目录,放入下载好的版本文件。
[[email protected] ~]$ mkdir .pyenv/cache [[email protected] ~]$ cp /mnt/tools/{Python-3.5.3.tar.gz,Python-3.5.3.tgz,Python-3.5.3.tar.xz} .pyenv/cache [[email protected] ~]$ ls .pyenv/cache/ Python-3.5.3.tar.gz Python-3.5.3.tar.xz Python-3.5.3.tgz [[email protected] ~]$
注: 因为不确定 pyenv install 会选择哪个python文件进行安装,所以把准备的三个安装包都放进去.
再次安装:
[[email protected] ~]$ pyenv install 3.5.3 -v
查看:
[[email protected] ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.5.3 [[email protected] ~]$
设置python版本:
[[email protected] ~]$ 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文件的方式。
[[email protected] ~]$ pyenv global 3.5.3
local:
设置面向程序的本地版本,通过将版本号写入当前目录下的.python-version文件的方式。这种方式设置的python版本优先级比global高,pyenv会从当前目录开始向上逐级查找.python-version文件,知道根目录为止,若找不到,就用global版本。使用pyenv local设置从当前工作目录开始向下递归都继承这个设置。
[[email protected] ~]$ pyenv local 3.5.3
shell:
设置面向shell的python版本,只作用于当前会话,通过设置当前shell的PYENV_VERSION环境变量的方式。
[[email protected] ~]$ pyenv shell 3.5.3
virtualenv:
前面说了pyenv可以帮助你在一台开发机上建立多个版本的python环境, 并提供方便的切换方法。
virtualenv则提供了一种功能, 就是将一个目录建立为一个虚拟的python环境, 这样的话, 用户可以建立多个虚拟环境, 每个环境里面的python版本可以是不同的, 也可以是相同的, 而且环境之间相互独立。
virtualenv插件的位置:
[[email protected] ~]$ 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 [[email protected] ~]$
为便于实验,我又安装了python3.6.1版本,安装方法见上文。
先查看一下:
[[email protected] ~]$ pyenv versions system * 3.5.3 (set by PYENV_VERSION environment variable) 3.6.1 [[email protected] ~]$
创建一个python3.5.3的虚拟环境:
[[email protected] ~]$ 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 [[email protected] ~]$
查看:
[[email protected] ~]$ pyenv versions system * 3.5.3 (set by PYENV_VERSION environment variable) 3.5.3/envs/env353 3.6.1 env353 [[email protected] ~]$
env353的真实目录位于:
[[email protected] ~]$ 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 [[email protected] ~]$
再创建一个基于3.6.1的虚拟环境:
[[email protected] ~]$ 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 [[email protected] ~]$ 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 [[email protected] ~]$
查看真实目录:
[[email protected] ~]$ 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 [[email protected] ~]$
删除虚拟环境:
如果要删除这个虚拟环境,只须直接删除它所在的目录即可:
[[email protected] ~]$ 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 [[email protected] ~]$ rm -rf .pyenv/versions/env361 [[email protected] ~]$ 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 [[email protected] ~]$
使用pip命令在env353的虚拟环境下安装ipython:
首先修改pip源,改成国内的:
(env353) [[email protected] ~]$ mkdir .pip (env353) [[email protected] ~]$ vim .pip/pip.conf [global] index-url=https://mirrors.aliyun.com/pypi/simple/ trusted-host=mirrors.aliyun.com :wq
安装ipython:
(env353) [[email protected] ~]$ pip install ipython (env353) [[email protected] ~]$ 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) [[email protected] ~]$ pip install jupyter
查看帮助信息:
(env353) [[email protected] ~]$ jupyter notebook help
开启服务进程:
(env353) [[email protected] ~]$ 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
关闭防火墙后在浏览器访问:
[[email protected] ~]# iptables -F
访问: http://172.16.251.184:8888
输入token: d5f49225271fb3bdb44091a318af541cc054c377c829374d
打包env353开发环境:
(env353) [[email protected] ~]$ pip freeze > requirement.txt
安装env353开发环境到新的虚拟环境:
(env361) [[email protected] ~]$ pip install -r requirement.txt
完!
本文出自 “yangbin” 博客,请务必保留此出处http://13683137989.blog.51cto.com/9636221/1964697
以上是关于python虚拟开发环境搭建的主要内容,如果未能解决你的问题,请参考以下文章