macos下python环境搭建

pyenv安装

用来管理电脑不同版本的python的一个管理工具,切换不同版本不会影响到系统自带的python。

##安装blew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

##安装zlib依赖

brew install zlib
brew link zlib --force
##安装pyenv

brew install pyenv

pyenv常用命令

$ pyenv
pyenv 1.2.4
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

需要在.bash_profile的最下面加入

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

安装python版本

#查看可以安装的版本

pyenv install -l

#安装3.5.3

CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 3.5.3      

#查看已经安装的python版本,其中*表示当前版本。

$ pyenv versions
* system (set by /Users/hong/.pyenv/version)
  3.5.3

修改python版本

qiaohongdeMacBook-Pro:~ hong$ pyenv versions
* system (set by /Users/hong/.pyenv/version)
  3.5.3
qiaohongdeMacBook-Pro:~ hong$ pyenv global 3.5.3
qiaohongdeMacBook-Pro:~ hong$ pyenv versions
  system
* 3.5.3 (set by /Users/hong/.pyenv/version)

通过global参数修改全局的python版本,会导致系统异常,因为中部分软件依赖低版本python,应该通过local设置该目录下的python版本,子目录会同步变更。

 
 

$ pwd

/Users/hong/python/project

$ pyenv version

system (set by /Users/hong/.pyenv/version)

$ cd test/

$ pyenv local 3.5.3

$ pyenv version

3.5.3 (set by /Users/hong/python/project/test/.python-version)

$ cd ..

$ pyenv version

system (set by /Users/hong/.pyenv/version)

虽然通过local设置目录的版本,但是在团队开发时,如我装了10个包,A又安装了20个,都装到了3.5.3这个环境中,程序打包时没法将不要的包剔除出去,而且相同的包版本不同也会引起冲突。怎么解决呢,pyenv提供了virtualenv(虚拟环境)用来实现包隔离。

virtualenv安装及使用

##virtualenv是依赖python的,所以在安装前至少要有一个python的版本。通过pip、brew或源码安装

brew install pyenv-virtualenv

##基于3.5.3版本创建虚拟环境nenv353

$ pyenv virtualenv 3.5.3 nenv353
$ pyenv versions
  system
* 3.5.3 (set by /Users/hong/python/project/test/.python-version)
  3.5.3/envs/nenv353
  nenv353

##在nenv353下安装redis

pip install redis

##验证redis安装位置

$ pwd
/Users/hong/.pyenv/versions/nenv353/lib/python3.5/site-packages
$ ls redi*
redis:
$ pwd
/Users/hong/.pyenv/versions/3.5.3/lib/python3.5/site-packages
$ ls
README				setuptools
__pycache__			setuptools-28.8.0.dist-info
easy_install.py			virtualenv-16.0.0.dist-info
pip				virtualenv.py
pip-9.0.1.dist-info		virtualenv_support
pkg_resources
qiaohongdeMacBook-Pro:site-packages hong$ 
##安装 jupyter,默认会安装Ipython
pip install jupyter

##修改jupyter notebook密码

jupyter notebook password

##启动jupyter,默认端口8888,启动后通过浏览器打开。--ip:0.0.0.0设置后,远程机器可以通过ip访问。

jupyter notebook --ip=0.0.0.0

输入密码











猜你喜欢

转载自blog.csdn.net/zqhjm/article/details/80377189