我的python设置

Python dependency management is known to be bad. Over time, I’ve decided the only way I’m willing to live is to push my Python environment hygiene to the max. As I’ve recommended my setup to a lot of people, I figured I should write it up as a reference.

Python依赖项管理被认为是不好的 。 随着时间的流逝,我决定我唯一愿意生存的方法就是将Python环境卫生最大化。 当我向很多人推荐我的设置时,我认为我应该把它写下来作为参考。

宗旨 (Tenets)

This is what I aim to accomplish in my Python setup. You don’t have to agree with these tenets, and if you don’t, feel free to ignore any of the advice that follows as it flows from them.

这就是我在Python设置中要实现的目标。 您不必同意这些原则,如果不同意,请随时忽略遵循它们的任何建议。

  • Never install anything in system python installs

    永远不要在系统python安装中安装任何东西
  • Always use a virtualenv

    始终使用virtualenv
  • virtualenvs are better when their state is managed (vs. direct pip installs into them), because then you can recreate them at will

    在管理其状态时(相对于直接pip安装),virtualenvs会更好,因为您可以随意重新创建它们
  • CLI tools written in python shouldn’t be treated like python packages

    用python编写的CLI工具不应像python包那样对待
  • Don’t pip-install tools that work across python versions into a specific python version (give them their own isolated install)

    不要将跨python版本工作的工具pip安装到特定的python版本中(给他们自己的隔离安装)

实作 (Implementation)

  • Use pyenv to setup and manage multiple python installations (including anaconda)

    使用pyenv设置和管理多个python安装(包括anaconda)

  • Use pipenv and/or poetry to manage virtualenvs

    使用Pipenv和/或诗歌来管理virtualenvs

  • Use pipx to install CLI tools written in python

    使用pipx安装以python编写的CLI工具

  • Instead of pip installing (or pipx-installing) these tools, get isolated installs for each. pipenv and pipx can be installed with pip, but which pyenv python version would you install them in?

    这些工具不是pip安装(或pipx-installing),而是每个工具都可以独立安装。 pipenv和pipx可以通过pip安装,但是您将在哪个pyenv python版本中安装它们?
  • With this setup, nothing touches system python, and nothing ever actually gets pip installed into the pyenv python installs directly

    有了这个设置,什么都没有碰到系统python,也没有任何东西可以将pip安装到pyenv python中直接安装

吹牛和诗歌 (pipenv and poetry)

Why use a virtualenv management tool like pipenv or poetry? Each virtualenv gets a file defining its state (Pipfile or pyproject.toml), making them easy to manage and recreate, and it’s then that much easier for each project to have its own managed virtualenv.

为什么要使用虚拟环境管理工具(例如pipenv或诗歌)? 每个virtualenv都有一个定义其状态的文件( Pipfilepyproject.toml ),使它们易于管理和重新创建,因此每个项目拥有自己的托管virtualenv变得容易得多。

Which to choose? It’s mostly your choice for which to use; I actually use both. I think they are good at different things, and good enough at those separate things to make it worth knowing and using both.

选择哪一个? 多数由您选择使用; 我实际上都使用。 我认为他们擅长于不同的事物,并且足够擅长于这些独立的事物,因此值得了解和使用两者。

pipenv is better for specifying the environment for an application (e.g., a thing you deploy), which should have a very specific known python setup. It also lets you have .env files for project environment variables, which tend to be useful for applications (e.g., its deployment environment) and less so for libraries.

pipenv更适合为应用程序(例如,您部署的事物)指定环境,该应用程序应具有非常特定的已知python设置。 它还使您拥有用于项目环境变量的.env文件,该文件对应用程序(例如,其部署环境)很有用,而对库则不太有用。

poetry is better for building libraries and CLI tools, which need to get put in PyPI and be flexible about the Python environment they end up in. Poetry’s PEP 518-compliant pyproject.toml is way easier than writing a setup.py file (which is still required if you’re using pipenv). It allows multiple virtualenvs/python versions (important for libraries). And, it can build wheels and publish to PyPI.

poetry对于构建库和CLI工具是更好的选择,这些库和CLI工具需要放入PyPI中并对其最终使用的Python环境保持灵活性pyproject.toml PEP 518兼容的 pyproject.toml比编写setup.py文件(如果您使用pipenv,则仍然需要)。 它允许使用多个virtualenvs / python版本(对于库很重要)。 而且,它可以构建轮子并发布到PyPI。

Both tools allow you to specify that the virtualenv directory for a project should go in the project directory itself (as .venv/), rather than in the central store of virtualenvs. I like this a lot, as it makes that virtualenv easier to find+destroy if it gets corrupted, and also gets inherently cleaned up if you remove the project directory. This feature is enabled with environment variables; instructions are included below.

两种工具都允许您指定项目的virtualenv目录应该位于项目目录本身(如.venv/ )中,而不是位于virtualenvs的中央存储中。 我非常喜欢它,因为它可以使virtualenv在损坏时更易于查找和销毁,并且在删除项目目录时也可以进行内在清理。 此功能已通过环境变量启用。 说明包括在下面。

pipx (pipx)

pipx installs each package into its own virtualenv and then links the executables so they are on your $PATH

pipx将每个软件包安装到其自己的virtualenv中,然后链接可执行文件,使它们位于您的$PATH

隔离安装 (Isolated installs)

To get isolated installations of these tools, I turn to homebrew, even on Linux (it’s good now!). As mentioned above, pip-installing requires selecting a pyenv-installed Python version to install into. You could pipx-install, but how do you install pipx? Additionally, brew formulas are kept much more up-to-date than most OS package managers (e.g., apt).

为了获得这些工具的隔离安装,我转向homebrew ,即使在Linux上也是如此(现在很好!)。 如上所述,pip安装需要选择要安装pyenv的Python版本。 您可以pipx-install,但是如何安装pipx? 此外,与大多数OS软件包管理器(例如,apt)相比,Brew公式保持最新。

pyenv, pipenv, and pipx should be brew installed, but I recommend using poetry’s only standalone installation (for now), because the brew formula is currently broken.

pyenv,pipenv和pipx应该进行brew安装,但是我建议使用poetry唯一的独立安装(目前),因为brew公式当前已损坏

自制 (homebrew)

Website

网站

苹果电脑 (mac)

/bin/bash

linux (linux)

sudo
test
test
echo

pyenv (pyenv)

Website

网站

Note: currently on linux, you should patch brew-installed pyenv for readline support (see this GitHub issue) so the REPL gets tab completion.

注意:当前在linux上,您应该修补brew安装的pyenv以获得readline支持( 请参阅GitHub问题 ),以便REPL可以完成制表符。

brew install# get latest versionsPY27=$(pyenv install
PY35=$(pyenv install
PY36=$(pyenv install
PY37=$(pyenv install
PY38=$(pyenv install# set global python version
pyenv install
pyenv global $PY38

You can also configure pip to refuse to install outside of a virtualenv, see instructions here (thanks to Theron Luhn for this tip).

您还可以将pip配置为拒绝安装在virtualenv之外, 请参阅此处的说明 (感谢Theron Luhn的帮助)。

吹牛 (pipenv)

Website

网站

brew install# Optional, if you want virtualenvs for a projects to go inside the project rather than centrally. Then when you delete a project, the virtualenv doesn't stick around. I like this a lot!
# YOUR_DOTFILE should be .profile on mac, and on linux, your .bashrc or whatever for your shellecho

诗歌 (poetry)

Website

网站

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
poetry completions bash# Optional, if you want virtualenvs for a projects to go inside the project rather than centrally. Then when you delete a project, the virtualenv doesn't stick around.
# YOUR_DOTFILE should be .profile on mac, and on linux, your .bashrc or whatever for your shellecho

笔记 (Notes)

This is based originally on Jacob Kaplan-Moss’s recommendations in this blog post.

这最初基于Jacob Kaplan-Moss此博客文章中的建议。

If you think this is excessive, that’s totally acceptable, but you can keep it to yourself. I have found that this works for me, and I’m sharing it in case other people find it useful.

如果您认为这太过分了,那完全可以接受,但是您可以自己保留。 我发现这对我有用,并且我正在分享它,以防其他人发现它有用。

If you think that Linux system package managers should be used in place of homebrew, you can continue to use them and I will not try to stop you.

如果您认为应该使用Linux系统软件包管理器代替自制软件,则可以继续使用它们,并且我不会试图阻止您。

If you think piping curl’d content from GitHub into your shell is a bad idea, I agree in principle but I think it’s an acceptable risk for these programs.

如果您认为将来自GitHub的curl内容传递到您的Shell中不是一个好主意,我原则上同意,但是对于这些程序来说,这是可以接受的风险。

If you’re on Windows and don’t want to use WSL (fair, but give it a try!), I’m sorry that I don’t have something for you.

如果您使用的是Windows,并且不想使用WSL(很公平,请尝试一下!),对不起,我没有适合您的东西。

If you think Docker is a better answer, you might be right! I have not gone far enough down that path to have a strong opinion about it, but it seems plausible, though it seems to get fiddly around IDE/GUI integration and integration with common resources from your host (e.g., dotfiles). I’m also unsure about whether I’d want to go with the traditional “container-as-process” model or the “container-as-vm” model of LXD.

如果您认为Docker是一个更好的答案,那可能是对的! 我还没有走很远的路来对它有一个强烈的看法,但是这似乎是合理的,尽管它似乎在与IDE / GUI集成以及与主机中的公共资源(例如dotfiles)的集成有关。 我也不确定是否要使用LXD的传统“容器作为过程”模型或“容器作为虚拟机”模型。

If you think I have not gone far enough and there are ways to make this even more hygienic, please let me know here or on Twitter.

如果您认为我做得还不够,并且有办法使它更加卫生,请在此处或在Twitter上告诉我。

翻译自: https://read.acloud.guru/my-python-setup-77c57a2fc4b6

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值