pycharm 使用 poetry 创建的虚拟环境

2021最佳实践:pyenv 管理python版本, poetry 负责依赖包管理。

1. pyenv python 版本控制

  • 1. 安装: curl https://pyenv.run | bash , 文件在用户家目
    录下.pyenv (Linux)
  • 2. 将 export PATH="PATH" 添加到 .bashrc,
    source完成安装。
  • 3. 安装python 相关的依赖,https://github.com/pyenv/pyenv/wiki#suggested-build-environment (Ubuntu)
  • 4. 创建python环境
pyenv install 3.9.2 
pyenv local 3.9.2 # 设置当前目录的
pyenv versions # 展示所有python版本

2. poetry虚拟环境

  • 1. 安装: curl -sSL https://raw.githubusercontent.com/python- poetry/poetry/master/get-poetry.py | python

  • 2.将 export PATH="PATH" 添加到 .bashrc,
    source完成安装。

  • 3. 添加依赖包源,在pyproject.toml添加

[[tool.poetry.source]] 
name = "aliyun" 
url = "https://mirrors.aliyun.com/pypi/simple" 
default = true

-4. 创建虚拟环境

poetry env use python3.8  # 指定虚拟环境使用的python版本
poetry shell  # 进入Poetry交互
poetry add #
poetry install 

poetry创建的虚拟环境在项目目录下的.venv文件

3. pycharm 使用poetry 环境

2021.3版本以后内置Poetry环境,之前的版本需要安装插件。
点击添加虚拟环境,选择wsl(我使用wsl),选择.venv/bin/python即可

4. 注意点

  1. 之前犯的一个错误:本地是win的pycharm,但是虚拟环境在wsl,故在poetry environment 中没有python.exe。
  2. 在wsl中一定不要忘记选择wsl虚拟环境!

5. 终端代理

clash 选择general allow lan, 使用wsl中的ip port。
在终端执行 export all_proxy:http://ip:port
curl google.com # 出现301即可

你可能感兴趣的:(pycharm 使用 poetry 创建的虚拟环境)