zoukankan      html  css  js  c++  java
  • 使用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%.poetryin

    mac/linux:


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

    验证是否安装成功


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

    注意:

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

     

    1. 将 https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py 代码内容另存到本地,比如存储为 get-poetry.py
    2. 在 https://github.com/python-poetry/poetry/releases 下载对应平台的 realse 版本,比如 1.1.4 的 windows 版本 poetry-1.1.4-win32.tar.gz
    3. 运行 python get-poetry.py(用绝对路径或者进入所在目录使用) --file poetry-1.1.4-win32.tar.gz(用绝对路径或者进入所在目录使用)
    4. 配置环境变量,就可以愉快的使用了
    5. 如果下载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@xxxxxtech.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@xxxxxtech.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:programecondaenvspy37python.exe,那么就用 poetry env use C:programecondaenvspy37python.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的,复制相关命令执行:C:UsersxxxxxAppDataLocalpypoetryCachevirtualenvsmypj1-HtrT1G92-py3.7Scriptspython.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:UsersxxxxAppDataLocalpypoetryCachevirtualenvsmypj1-HtrT1G92-py3.7Scriptspython.exe,添加一个解释器环境即可,这个情况只有在第二步让创建bat文件没点ok才会出现

     

     

     

     

     

     

     

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

     

  • 相关阅读:
    centos下卸载rpm包
    centos下添加环境变量和启动apache
    centos(linux) 下如何查看端口占用情况及杀死进程
    如何使上层的div遮住的链接可以点击
    jquery.blockUI.2.31.js 弹出层项目介绍
    fieldset、legend、display html元素
    <a>标签中href="javascript:;"的意思
    memcache 学习笔记
    sublime text3 JS语法检测插件
    Apache Rewrite的主要功能
  • 原文地址:https://www.cnblogs.com/Eeyhan/p/14281240.html
Copyright © 2011-2022 走看看