简要摘抄:
1、安装虚拟系统:
[sudo] pip install virtualenv还挺顺利!用了sudo。virtualenv是python的虚拟系统,用于解决python的依赖以及库的版本不一致的问题。
2、创建virtualenv。
virtualenv -p python3 env这下问题来了,没有python3。那么如何安装python3呢?一开始通过网上搜索,就直接下载的.tgz包。参考 使用virtualenv搭建python3开发环境,可能是系统差异的原因吧,我在安装过程并没有那么顺利,各种错误。
后来就放弃了。又找到Mac OSX 正确地同时安装Python 2.7 和Python3这篇文章,觉得有点苗头了。
我先执行了brew install python3.可是安装完成之后,which python3的时候并没有找到python3的路径,感觉自己的python3装的并不是很正常,于是brew uninstall python3卸载掉了。
试着执行了下brew doctor,发现brew已经过期了(Warning: Your Homebrew is outdated.)。于是参考Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)这篇文章,把brew更新了下。
执行brew update。出错:Error: The /usr/local directory is not writable.
执行:sudo chown -R $(whoami):admin /usr/local
执行brew update。这次成功下载并更新了。Already up-to-date.
执行brew upgrade。下载了好多。。。
接着执行brew install python3.依然错误。Error: You must `brew link xz` before python3 can be installed
执行brew link xz。
再执行brew install python3。成功了。
$ python3 --version
Python 3.5.2
$ which python3
/usr/local/bin/python3
创建virtualenv:virtualenv -p /usr/local/bin/python3 env
参数说明:
- -p python3: 使用 python3 作为 virtualenv中的python解释器
- env: virtualenv 的存储目录, 存储python程序和依赖库
source env/bin/activate
4、关闭virtualenv
deactivate
5、安装mysqlclient时出现的问题
安装mysqlclient的时候一直出现个错误:OSError: mysql_config not found
Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at '/Users/jennifer/env/bin/python'.
Collecting mysqlclient
Using cached mysqlclient-1.3.7.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/d1/6vdmk20n6hn182xp8krmtgc80000gn/T/pycharm-packaging499/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/d1/6vdmk20n6hn182xp8krmtgc80000gn/T/pycharm-packaging499/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/private/var/folders/d1/6vdmk20n6hn182xp8krmtgc80000gn/T/pycharm-packaging499/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/d1/6vdmk20n6hn182xp8krmtgc80000gn/T/pycharm-packaging499/mysqlclient/
解决方法:激活虚拟环境,在命令里直接安装:pip install mysqlclient
6、配置 python interpreter
File -->Default Setting --> project interpreter -->Add Local --> /Users/xxx/p27env/bin/python2.7(创建的虚拟环境的位置) -->OK