Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

折腾 Python 版本管理 #41

Closed
riskers opened this issue Oct 22, 2018 · 1 comment
Closed

折腾 Python 版本管理 #41

riskers opened this issue Oct 22, 2018 · 1 comment
Labels

Comments

@riskers
Copy link
Owner

riskers commented Oct 22, 2018

最近开始写 Python,对于一直写 JS 的我来说,十分不习惯 Python 的版本管理,一个项目的依赖装在了全局。强迫症的我就开始找 Python 的版本管理方案。

pyenv

install

  1. install

    curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
    
  2. .zshrc 添加:

    export PATH="$HOME/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    
  3. 安装不同版本 Python

    CFLAGS="-I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" \
    LDFLAGS="-L$(brew --prefix openssl)/lib" \
    pyenv install -v 3.5.0
    

usage

  1. 查看版本

    pyenv versions # 查看系统当前安装的python列表
    pyenv version # 查看系统当前使用的python版本
    
  2. 设置版本

    pyenv global # 设置全局 Python 版本,将版本号写入~/.pyenv/version文件
    pyenv local # 设置当前项目 Python 版本,将版本号写入当前目录下的.python-version文件

pipenv

install

  1. 安装:

    python3 -m pip install --upgrade --force-reinstall pip
    pip3 install pipenv
    
  2. .zshrc 添加:

    export PIPENV_VENV_IN_PROJECT=1 # 在每个项目的根目录下保存虚拟环境目录.venv
    

usage

pipenv --two/three # 创建一个虚拟环境

会生成 PipfilePipfile.lock,类似 package.jsonyarn.lock。还支持 pipenv --python 3.7.0 这种指定版本的用法,如果本地没有这个版本,就会安装这个版本。

其他用法:

  • pipenv install [package]: 安装模块
  • pipenv graph: 列出项目所有依赖
  • pipenv shell: 进入 shell

最佳实践

pyenv 安装多版本 Python 后,用他切 local 版本,其他包版本的事交给 pipenv。

  1. pyenv install -v 3.5.0
  2. cd envtest
  3. pyenv local 3.5.0: python -V: 3.5.0 (设置成功)
  4. pipenv --python 3.5.0
  5. pipenv shell -> python -V: 3.5.0 (设置成功)


向我捐助 | 关于我 | 工作机会


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants