使用poetry+conda配置虚拟环境

有关虚拟环境的相关的我之前也介绍过,python高级(4)—— 虚拟环境安装使用

比如pipenv,还有virtualenv之类的,还有本篇文章的主角 – poetry

到底哪个更好用呢,看你个人吧,我觉得poetry更好一点咯,不过还是看个人喜好了

一:conda创建虚拟环境
1.安装conda
这个就省略了,网上太多教程了

2.创建虚拟环境:
conda create -n py37 python=3.7

二:poetry使用教程
简介:
poetry是一个依赖项管理和打包工具,它运行声明项目所依赖的库,很方便的管理项目环境,仅支持python3.6+

安装:
官方不推荐用pip安装,虽然也能安装,但是使用起来不方便

1.mac/linux:  
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

2.windows:

powershell下安装(cmd下回提示“.Content”相关错误)

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python

配置poetry环境变量
windows系统:
path变量里添加如下:
%USERPROFILE%.poetry\bin

mac/linux:

安装完即可使用,可能需要配置软链接,找到安装目录,ln命令配置进软链接即可

验证是否安装成功

终端命令下使用poetry --version,如果输出版本即可

注意:
如果上面安装时,速度较慢解决办法:

将 https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py 代码内容另存到本地,比如存储为 get-poetry.py
在 https://github.com/python-poetry/poetry/releases 下载对应平台的 realse 版本,比如 1.1.4 的 windows 版本 poetry-1.1.4-win32.tar.gz
运行 python get-poetry.py(用绝对路径或者进入所在目录使用) --file poetry-1.1.4-win32.tar.gz(用绝对路径或者进入所在目录使用)
配置环境变量,就可以愉快的使用了
如果下载poetry包时还是下载很慢或者代理速度不给力,使用github镜像站下载:
把域名https://github.com替换成https://github.wuyanzheshui.workers.dev即可

比如: 原链接:https://github.com/python-poetry/poetry/releases/download/1.1.4/poetry-1.1.4-win32.tar.gz

替换之后:https://github.wuyanzheshui.workers.dev/python-poetry/poetry/releases/download/1.1.4/poetry-1.1.4-win32.tar.gz 下载速度就飞起了

poetry常用命令

三: pip 切换安装源

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple ,记得用https

四:poetry+conda配置环境
以下步骤请在终端下执行,如果是windows的话,建议使用commder或者ConEmu,不要用cmd,cmd有中文编码问题

0.创建目录:
poetry new xx文件夹名

1.初始化:

这里如果没有用第0个步骤的命令,而是进入已存在或者事先创建的目录下,就需要使用这个命令
poetry init

结果:

This command will guide you through creating your pyproject.toml config.

Package name [baseprojectone]:
Version [0.1.0]:
Description []:
Author [xxxxxx yh@nosugartech.com, n to skip]:
License []:
Compatible Python versions [^3.7]:

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file

[tool.poetry]
name = “baseprojectone”
version = “0.1.0”
description = “”
authors = [“xxxxxx yh@nosugartech.com”]

[tool.poetry.dependencies]
python = “^3.7”

[tool.poetry.dev-dependencies]

[build-system]
requires = [“poetry-core>=1.0.0”]
build-backend = “poetry.core.masonry.api”

Do you confirm generation? (yes/no) [yes]

2.指定解释器:

poetry env use /Users/dm/.miniconda3/envs/py37/bin/python(conda创建的虚拟环境,如果是windows系统,找到python.exe文件)
比如我的路径是C:\programe\conda\envs\py37\python.exe,那么就用 poetry env use C:\programe\conda\envs\py37\python.exe

3.安装相关初始环境

这里前提是必须已经配置好pip安装源,不然会很慢

poetry install

结果:

Updating dependencies
Resolving dependencies… (12.4s)

Writing lock file

Package operations: 13 installs, 0 updates, 0 removals

• Installing typing-extensions (3.7.4.3)
• Installing zipp (3.4.0)
• Installing importlib-metadata (3.4.0)
• Installing pyparsing (2.4.7)
• Installing atomicwrites (1.4.0)
• Installing attrs (20.3.0)
• Installing colorama (0.4.4)
• Installing more-itertools (8.6.0)
• Installing packaging (20.8)
• Installing pluggy (0.13.1)
• Installing py (1.10.0)
• Installing wcwidth (0.2.5)
• Installing pytest (5.4.3)

四.配置项目环境:

1.命令:
poetry add git+ssh://git@xxxxxx.git

poetry add requests

注意:

如果有提示更新pip的,复制相关命令python基础教程执行:C:\Users\xxxxx\AppData\Local\pypoetry\Cache\virtualenvs\mypj1-HtrT1G92-py3.7\Scripts\python.exe -m pip install --upgrade pip

最后安装成功:

Updating dependencies
Resolving dependencies… (3.8s)

Package operations: 1 install, 0 updates, 0 removals

• Installing xxx (0.0.2 1bd38afe2c)

五:pycharm使用poetry创建的虚拟环境创建项目

1.pycharm先安装poetry插件
低版本的pycharm不支持,最好用最新版pycharm

2.打开项目所在目录:
在打开时,会弹出一个创建bat文件的窗口,点【ok】

在这里插入图片描述

3.如果没有解释器环境的话,找到创建那个路径的解释器环境,比如:C:\Users\xxxx\AppData\Local\pypoetry\Cache\virtualenvs\mypj1-HtrT1G92-py3.7\Scripts\python.exe,添加一个解释器环境即可,这个情况只有在第二步让创建bat文件没点ok才会出现

在这里插入图片描述

接下来就可以进行你的项目开发了

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值