pip 和 pipenv

    科技2026-08-18  9

    pip 和 pipenv

    I’ve never been a great programmer. I didn’t study at a great university. In fact all I have is a couple of completed modules with the Open University (and I mean to do more as money becomes less of an issue for me). But the one important thing that I have going for me is curiosity. I want to make sure that what I build will make sense in the future and after reading countless articles I think my programming ability is improving. With one small exception. Testing and deployment. I can build nice apps that appear to work well in a debug environment but when it comes to pushing them to production subtle bugs appear. This is because I don’t test my code rigorously enough in different states. Also when it comes to deploying my code I get lost in a stream of Linux commands and installing all of the required dependencies. This is because I need some form of automated deployment which would mean that I don’t need to worry about such things in the future.

    我从来都不是一个伟大的程序员。 我没有在一所伟大的大学学习。 实际上,我所拥有的只是开放大学的几个完成的模块(而且我的意思是要做更多的事情,因为金钱对我来说不再是一个大问题)。 但是我要追求的一件事是好奇心。 我想确保自己构建的内容将来会有意义,并且在阅读了无数文章之后,我认为我的编程能力正在提高。 除了一个小例外。 测试和部署。 我可以构建看起来不错的应用程序,这些应用程序似乎可以在调试环境中很好地工作,但是当涉及到将它们推向生产环境时,就会出现细微的错误。 这是因为我没有在不同状态下足够严格地测试我的代码。 同样,在部署代码时,我迷上了Linux命令流并安装了所有必需的依赖项。 这是因为我需要某种形式的自动化部署,这意味着我以后不必担心此类事情。

    I have fixed the deployment issue somewhat by using the same Linux distribution on my home laptop, my home desktop and all of my servers (OpenSUSE LEAP 15.2 for those who are interested). This has helped a great deal as I always have a decent idea of what is going on because I am so used to using the distribution but ideally I’d get into the Docker and Kubernetes scene to deploy my applications. But that is for another article.

    我已经通过在家用笔记本电脑,家用台式机和所有服务器(对于感兴趣的人使用OpenSUSE LEAP 15.2)上使用相同的Linux发行版,在某种程度上解决了部署问题。 这对我的工作很有帮助,因为我总是对正在发生的事情有个不错的主意,因为我已经习惯了使用发行版,但是理想情况下,我会进入Docker和Kubernetes场景来部署我的应用程序。 但这是另一篇文章。

    安装Pipenv (Installing Pipenv)

    Installing pipenv is easy. All you need is pip which comes automatically with the official downloads from python.org and most Linux distributions. If it is not already installed on your computer search your operating system repositories to make sure Python and pip are installed or install Python from the official website. Once that is done you can install pipenv with this simple command:

    安装pipenv很容易。 您所需要的只是pip,它会自动从python.org和大多数Linux发行版提供官方下载。 如果尚未在计算机上安装Python,请搜索操作系统存储库以确保已安装Python和pip,或者从官方网站安装Python。 完成后,您可以使用以下简单命令安装pipenv:

    pip install --user pipenv

    In an ideal world that would be all you need to do but unfortunately it isn’t an ideal world. You now need to add the pipenv binary to your system path. On Linux open ~/.profile and edit your $PATH variable so it looks something like this:

    在理想的世界中,这是您需要做的所有事情,但不幸的是,这不是理想的世界。 现在,您需要将pipenv二进制文件添加到系统路径。 在Linux上,打开〜/ .profile并编辑$ PATH变量,使其看起来像这样:

    export PATH=$PATH:/home/user/.local/bin

    Once that is done you can logout and back in again or type source ~/.profile on the command line and pipenv will be available to you.

    完成后,您可以注销并重新登录,或者在命令行上键入source〜/ .profile,pipenv将对您可用。

    在您的一个项目中使用Pipenv (Using Pipenv in One of Your Projects)

    Pipenv is what pip and virtualenvs should have been right from the start. When you have a folder that contains a project (or even just an empty folder you haven’t started with yet) you can create a virtual environment and install a dependency with two simple commands:

    Pipenv是pip和virtualenvs从一开始就应该正确的方法。 当您拥有一个包含项目的文件夹(甚至只是一个尚未开始的空文件夹)时,可以创建一个虚拟环境并使用两个简单命令安装依赖项:

    cd /path/to/python/project/folderpipenv install flask

    You have now created a virtual environment and installed Flask along with all of its dependencies in less than a minute. If you look at what Pipenv has added to the directory you’ll see a Pipfile which contains the dependencies and a Pipfile.lock which contains hashes of the dependencies to make sure everything is correct when loading up the project on a different machine.

    现在,您已经创建了一个虚拟环境,并在不到一分钟的时间内安装了Flask及其所有依赖项。 如果查看Pipenv已添加到目录中的内容,则会看到一个包含依赖项的Pipfile和一个包含依赖项哈希值的Pipfile.lock,以确保在其他计算机上加载项目时一切正确。

    在Visual Studio Code中使用Pipenv (Using Pipenv in Visual Studio Code)

    There is one last thing we need to do. Most people these days seem to use Microsoft Visual Studio Code as their IDE and I do as well in fact. We want the virtualenv to be registered with Visual Studio Code so that things like pylint work correctly. Just enter the following commands:

    我们需要做的最后一件事。 如今,大多数人似乎都将Microsoft Visual Studio Code用作他们的IDE,实际上我也是如此。 我们希望将virtualenv注册到Visual Studio Code中,以便诸如pylint之类的东西能够正常工作。 只需输入以下命令:

    cd /path/to/python/project/folderpipenv --venv

    the output of this command is where your pipenv virtual environment is stored. Simply load up your project in Visual Studio Code and type CTRL + Shift + P and select the Python Interpreter option. Select Enter Interpreter Path and Visual Studio Code will now use the correct environment for your project. Enjoy your Python programming!

    该命令的输出是存储pipenv虚拟环境的位置。 只需在Visual Studio代码中加载项目,然后键入CTRL + Shift + P并选择“ Python解释器”选项。 选择Enter Interpreter Path,Visual Studio Code现在将为您的项目使用正确的环境。 享受您的Python编程!

    翻译自: https://medium.com/@simonconnah/say-goodbye-to-pip-and-virtualenvs-and-say-hello-to-pipenv-and-using-pipenv-with-visual-studio-9c5e121833e

    pip 和 pipenv

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.021, SQL: 9