加载中...
avatar

Python—Virtualenv---pyenv简明教程(MAC版)

在Mac上,系统默认使用的是python2.7,而且最好不要去手动修改python的版本,因为系统的很多东西是依赖python2.7的,一旦修改将引起不必要的麻烦。

那么,怎样在Mac上进行python3的开发和学习呢?还好,我发现了python版本管理神器virtualenv和pyenv。其中,pyenv侧重于python版本的管理,需要修改当下系统的环境版本;而virtualenv相当于python虚拟容器,能够创建完全独立的python版本环境,不同版本之间不会共享库文件和环境配置,不相互影响。

pyenv管理与系统环境隔离的python解释器版本,比如2.7.8、3.4.2
virtualenv管理与系统环境隔离的包安装,比如可以在x_env里安装a包的1.0版,在y_env里安装a包的1.1版,两者互不影响,也不影响系统环境中的安装包
pyenv包含有virtualenv,可以选择生成的venv是基于哪个解释器环境的
如果只是单独安装virtualenv,不安装pyenv,那么生成的venv就是基于系统环境解释器的了

virtualenv也可以配置python解释器的,新建环境的时候-p path/to/version/python

下面将分别介绍两种版本管理的安装使用和维护方法。

1、安装Virtualenv

使用pip安装Virtualenv, 使用过python的都应该知道pip包管理神器吧, 即使不知道, 网站也有大把的教程, 不过推荐查看官方安装指南

1
2
3
4
$ pip install virtualenv
//或者由于权限问题使用sudo临时提升权限
$ sudo pip install virtualenv
$ pip install virtualenvwrapper

2、virutalenv基本使用

现在开始使用virtualenv管理python环境

1
2
3
4
5
6
7
8
➜  Test git:(master) ✗ virtualenv ENV  #创建一个名为ENV的目录, 并且安装了ENV/bin/python, 创建了lib,include,bin目录,安装了pip
New python executable in
Installing setuptools, pip...done.
➜ Test git:(master) ✗ cd ENV
➜ ENV git:(master) ✗ ll
drwxr-xr-x 14 andrew_liu staff 476 12 8 08:49 bin
drwxr-xr-x 3 andrew_liu staff 102 12 8 08:49 include
drwxr-xr-x 3 andrew_liu staff 102 12 8 08:49 lib
  • lib,所有安装的python库都会放在这个目录中的lib/pythonx.x/site-packages/
  • bin,bin/python是在当前环境是使用的python解释器

如果在命令行中运行virtualenv --system-site-packages ENV, 会继承/usr/lib/python2.7/site-packages下的所有库, 最新版本virtualenv把把访问全局site-packages作为默认行为

如果在命令行中运行virtualenv --system-site-packages ENV, 会继承/usr/lib/python2.7/site-packages下的所有库, 最新版本virtualenv把把访问全局site-packages作为默认行为
default behavior.

2.1、激活virtualenv

1
2
3
4
5
6
7
8
9
10
11
#ENV目录下使用如下命令
➜ ENV git:(master) ✗ source ./bin/activate #激活当前virtualenv
(ENV)➜ ENV git:(master) ✗ #注意终端发生了变化
#使用pip查看当前库
(ENV)➜ ENV git:(master) ✗ pip list
pip (1.5.6)
setuptools (3.6)
wsgiref (0.1.2) #发现在只有这三个
pip freeze #显示所有依赖
pip freeze > requirement.txt #生成requirement.txt文件
pip install -r requirement.txt #根据requirement.txt生成相同的环境

2.2、关闭virtualenv

使用下面命令

1
$ deactivate

2.3、指定python版本

可以使用-p PYTHON_EXE选项在创建虚拟环境的时候指定python版本

1
2
3
4
5
6
7
8
9
10
11
#创建python2.7虚拟环境
➜ Test git:(master) ✗ virtualenv -p /usr/bin/python2.7 ENV2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in ENV2.7/bin/python
#创建python3.4虚拟环境
➜ Test git:(master) ✗ virtualenv -p /usr/local/bin/python3.4 ENV3.4
Running virtualenv with interpreter /usr/local/bin/python3.4
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4'
New python executable in ENV3.4/bin/python3.4
Also creating executable in ENV3.4/bin/python
Installing setuptools, pip...done.

到此已经可以解决python版本冲突问题和python库不同版本的问题。

2.4、在pycharm中使用虚拟环境

在设置里面Project Interpreter,点击齿轮那个按钮,选择”Add Local”,然后选择刚才创建的虚拟环境的路径。

3、pyenv查看系统版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(ENV3.4)╭─lumia@localhost ~/ENV3.4  ‹master*›
╰─➤ more /System/Library/CoreServices/SystemVersion.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ProductBuildVersion</key>
<string>14F27</string>
<key>ProductCopyright</key>
<string>1983-2015 Apple Inc.</string>
<key>ProductName</key>
<string>Mac OS X</string>
<key>ProductUserVisibleVersion</key>
<string>10.10.5</string>
<key>ProductVersion</key>
<string>10.10.5</string>
</dict>
</plist>

3.1、安装homebrew

打开终端输入如下命令安装homebrew。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(ENV3.4)╭─lumia@localhost ~/ENV3.4  ‹master*›
╰─➤ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Homebrew
/usr/local/sbin

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir -p /usr/local/Homebrew /usr/local/sbin
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/Homebrew /usr/local/sbin
==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown lumia /usr/local/Homebrew /usr/local/sbin
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Homebrew /usr/local/sbin
==> /usr/bin/sudo /bin/mkdir -p /Users/lumia/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/lumia/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown lumia /Users/lumia/Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 6081, done.
remote: Compressing objects: 100% (3725/3725), done.
remote: Total 6081 (delta 3449), reused 4041 (delta 2165), pack-reused 0
Receiving objects: 100% (6081/6081), 3.48 MiB | 1.48 MiB/s, done.
Resolving deltas: 100% (3449/3449), done.
From https://github.com/Homebrew/brew
* [new branch] master -> origin/master
* [new tag] 0.1 -> 0.1
* [new tag] 0.2 -> 0.2
* [new tag] 0.3 -> 0.3
* [new tag] 0.4 -> 0.4
* [new tag] 0.5 -> 0.5
* [new tag] 0.6 -> 0.6
* [new tag] 0.7 -> 0.7
* [new tag] 0.7.1 -> 0.7.1
* [new tag] 0.8 -> 0.8
* [new tag] 0.8.1 -> 0.8.1
* [new tag] 0.9 -> 0.9
* [new tag] 0.9.1 -> 0.9.1
* [new tag] 0.9.2 -> 0.9.2
* [new tag] 0.9.3 -> 0.9.3
* [new tag] 0.9.4 -> 0.9.4
* [new tag] 0.9.5 -> 0.9.5
* [new tag] 0.9.8 -> 0.9.8
* [new tag] 0.9.9 -> 0.9.9
* [new tag] 1.0.0 -> 1.0.0
* [new tag] 1.0.1 -> 1.0.1
* [new tag] 1.0.2 -> 1.0.2
* [new tag] 1.0.3 -> 1.0.3
* [new tag] 1.0.4 -> 1.0.4
* [new tag] 1.0.5 -> 1.0.5
* [new tag] 1.0.6 -> 1.0.6
* [new tag] 1.0.7 -> 1.0.7
* [new tag] 1.0.8 -> 1.0.8
* [new tag] 1.0.9 -> 1.0.9
* [new tag] 1.1.0 -> 1.1.0
* [new tag] 1.1.1 -> 1.1.1
* [new tag] 1.1.10 -> 1.1.10
* [new tag] 1.1.11 -> 1.1.11
* [new tag] 1.1.12 -> 1.1.12
* [new tag] 1.1.13 -> 1.1.13
* [new tag] 1.1.2 -> 1.1.2
* [new tag] 1.1.3 -> 1.1.3
* [new tag] 1.1.4 -> 1.1.4
* [new tag] 1.1.5 -> 1.1.5
* [new tag] 1.1.6 -> 1.1.6
* [new tag] 1.1.7 -> 1.1.7
* [new tag] 1.1.8 -> 1.1.8
* [new tag] 1.1.9 -> 1.1.9
* [new tag] 1.2.0 -> 1.2.0
* [new tag] 1.2.1 -> 1.2.1
HEAD is now at dd5d488 Merge pull request #2658 from JCount/audit-fix-versioned-aliases-homebrew-core
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4421, done.
remote: Compressing objects: 100% (4226/4226), done.
remote: Total 4421 (delta 37), reused 473 (delta 14), pack-reused 0
Receiving objects: 100% (4421/4421), 3.52 MiB | 2.40 MiB/s, done.
Resolving deltas: 100% (37/37), done.
Checking connectivity... done.
Tapped 4225 formulae (4,463 files, 11.0MB)
==> Cleaning up /Library/Caches/Homebrew...
Removing: /Library/Caches/Homebrew/gdbm-1.11.yosemite.bottle.2.tar.gz... (163.3KB)
Removing: /Library/Caches/Homebrew/python3-3.4.3.yosemite.bottle.tar.gz... (18.5MB)
Removing: /Library/Caches/Homebrew/readline-6.3.8.yosemite.bottle.tar.gz... (741.5KB)
Removing: /Library/Caches/Homebrew/sqlite-3.8.8.3.yosemite.bottle.tar.gz... (940.7KB)
Removing: /Library/Caches/Homebrew/xz-5.2.0.yosemite.bottle.tar.gz... (449.5KB)
==> Migrating /Library/Caches/Homebrew to /Users/lumia/Library/Caches/Homebrew..
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
Error: Could not link:
/usr/local/share/man/man1/brew.1

Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/doc/homebrew

Please delete these paths and run `brew update`.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
http://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation:
http://docs.brew.sh

测试是否安装成功

1
2
3
4
(ENV3.4)╭─lumia@localhost ~/ENV3.4  ‹master*›
╰─➤ brew -v
Homebrew 1.2.1
Homebrew/homebrew-core (git revision 9bf0; last commit 2017-05-22)

显示版本信息证明安装成功了。

3.2、安装pyenv

当然,既可以通过brew命令安装,也可以通过pip命令安装。

1
2
3
4
5
6
7
8
$ brew install pyenv 
$ brew install pyenv-virtualenv
#或者pip
$ pip install pyenv
$ pip install virtualenv
#验证安装成功
$ pyenv -v
$ pyenv-virtualenv --version

3.3、查看可以安装python版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(ENV3.4)╭─lumia@localhost ~/ENV3.4  ‹master*›
╰─➤ pyenv install --list 130 ↵
Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
2.6.7
2.6.8
2.6.9
2.7-dev
2.7
2.7.1
2.7.2
2.7.3
2.7.4
2.7.5
2.7.6
2.7.7
2.7.8
2.7.9
2.7.10
3.0.1
3.1
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.2-dev
3.2
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.2.6
3.3.0
3.3-dev
3.3.1
3.3.2
3.3.3
3.3.4
3.3.5
3.3.6
3.4.0
3.4-dev
3.4.1
3.4.2
3.4.3
3.5.0
3.5-dev
3.6-dev
anaconda-1.4.0
anaconda-1.5.0
anaconda-1.5.1
anaconda-1.6.0
anaconda-1.6.1
anaconda-1.7.0
anaconda-1.8.0
anaconda-1.9.0
anaconda-1.9.1
anaconda-1.9.2
anaconda-2.0.0
anaconda-2.0.1
anaconda-2.1.0
anaconda-2.2.0
anaconda-2.3.0
anaconda3-2.0.0
anaconda3-2.0.1
anaconda3-2.1.0
anaconda3-2.2.0
anaconda3-2.3.0
ironpython-dev
ironpython-2.7.4
ironpython-2.7.5
jython-dev
jython-2.5.0
jython-2.5-dev
jython-2.5.1
jython-2.5.2
jython-2.5.3
jython-2.5.4-rc1
jython-2.7.0
jython-2.7.1b1
miniconda-2.2.2
miniconda-3.0.0
miniconda-3.0.4
miniconda-3.0.5
miniconda-3.3.0
miniconda-3.4.2
miniconda-3.7.0
miniconda-3.8.3
miniconda-3.9.1
miniconda-3.10.1
miniconda-3.16.0
miniconda3-2.2.2
miniconda3-3.0.0
miniconda3-3.0.4
miniconda3-3.0.5
miniconda3-3.3.0
miniconda3-3.4.2
miniconda3-3.7.0
miniconda3-3.8.3
miniconda3-3.9.1
miniconda3-3.10.1
miniconda3-3.16.0
pypy-c-jit-latest
pypy-c-nojit-latest
pypy-dev
pypy-stm-2.3
pypy-portable-2.3.1
pypy-portable-2.4
pypy-portable-2.5
pypy-portable-2.5.1
pypy-stm-2.5.1
pypy-portable-2.6.0
pypy-portable-2.6.1
pypy-1.5-src
pypy-1.5
pypy-1.6
pypy-1.7-dev
pypy-1.7
pypy-1.8-dev
pypy-1.8
pypy-1.9-dev
pypy-1.9
pypy-2.0-dev
pypy-2.0-src
pypy-2.0
pypy-2.0.1-src
pypy-2.0.1
pypy-2.0.2-src
pypy-2.0.2
pypy-2.1-src
pypy-2.1
pypy-2.2-src
pypy-2.2
pypy-2.2.1-src
pypy-2.2.1
pypy-2.3-src
pypy-2.3
pypy-2.3.1-src
pypy-2.3.1
pypy-2.4.0-src
pypy-2.4.0
pypy-2.4-beta1-src
pypy-2.4-beta1
pypy-2.5.0-src
pypy-2.5.0
pypy-2.5.1-src
pypy-2.5.1
pypy-2.6.0-src
pypy-2.6.0
pypy-2.6.1-src
pypy-2.6.1
pypy3-dev
pypy3-portable-2.3.1
pypy3-portable-2.4
pypy3-2.3.1-src
pypy3-2.3.1
pypy3-2.4.0-src
pypy3-2.4.0
stackless-dev
stackless-2.7-dev
stackless-2.7.2
stackless-2.7.3
stackless-2.7.4
stackless-2.7.5
stackless-2.7.6
stackless-2.7.7
stackless-2.7.8
stackless-3.2-dev
stackless-3.2.2
stackless-3.2.5
stackless-3.3-dev
stackless-3.3.5
stackless-3.4.1

3.4、安装对应的python版本

1
2
3
$ pyenv install 3.4.3
pyenv: /Users/angel/.pyenv/versions/3.4.3 already exists
continue with installation? (y/N) N

3.5、设定环境

在.bashrc(或者.bash_profile)加入如下内容

1
2
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

如果没有如上两个文件,可以生成。

3.6、切换python版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(ENV3.4)╭─lumia@localhost ~/ENV3.4  ‹master*›
╰─➤python
Python 3.4.3 (default, May 2 2015, 21:56:04)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(ENV3.4)╭─lumia@localhost ~/ENV3.4 ‹master*›
╰─➤ pyenv versions
system
* 3.4.3 (set by /Users/angel/.pyenv/version)
(ENV3.4)╭─lumia@localhost ~/ENV3.4 ‹master*›
╰─➤pyenv global system
(ENV3.4)╭─lumia@localhost ~/ENV3.4 ‹master*›
╰─➤ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(ENV3.4)╭─lumia@localhost ~/ENV3.4 ‹master*›
╰─➤pyenv versions
* system (set by /Users/angel/.pyenv/version)
3.4.3
(ENV3.4)╭─lumia@localhost ~/ENV3.4 ‹master*›
╰─➤ pyenv versions

“system”是系统默认版本。

文章作者: 蕾米亚
文章链接: http://omimo.ga/2017/5c1674cb.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭彭和丁满
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论