In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to use Pipenv, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Pipenv is the "Python development workflow for developers" created by Kenneth Reitz more than a year ago, and it has become the official recommended resource for Python to manage package dependencies. But there is still confusion about what it solves and how it is more useful than standard workflows that use pip and requirements.txt files. In this month's Python column, we'll fill in these gaps.
A brief history of Python package installation
To understand the problems solved by Pipenv, it is useful to take a look at how Python package management has evolved.
Let's go back to * Python versions, when we have Python, but there is no clean way to install the package.
Then there's Easy Install, a software package that makes it relatively easy to install other Python packages, but it also poses a problem: it's not easy to uninstall unwanted packages.
Pip makes its debut, and most Python users are familiar with it. Pip allows us to install and uninstall packages. We can specify the version, run pip freeze > requirements.txt to output a list of installed packages to a text file, and use the same text file with pip install-r requirements.txt to install all the packages needed by an application.
But pip does not include a way to isolate packages from each other. We may develop different versions of the application that use the same library, so we need a way to do this. Then comes the virtual environment, which enables us to create a small, isolated environment for each application we develop. We have seen many tools for managing virtual environments: virtualenv, venv, virtualenvwrapper, pyenv, pyenv-virtualenv, pyenv-virtualenvwrapper, and so on. They all work well with pip and requirements.txt files.
New method: Pipenv
Pipenv aims to solve several problems:
First, you need the pip library to install the package, plus a library for creating the virtual environment, and a library for managing the virtual environment, and then all the commands related to these libraries. All these need to be managed. Pipenv comes with package management and virtual environment support, so you can use a tool to install, uninstall, track and record dependencies, and to create, use, and organize your virtual environment. When you use it to start a project, Pipenv will automatically create a virtual environment for the project if you haven't already used it.
Pipenv does this dependency management by abandoning the requirements.txt specification and moving it to a new document called Pipfile. When you install a library using Pipenv, the project's Pipfile automatically updates the installation details, including version information, and possible Git repository locations, file paths, and other information.
Second, Pipenv wants to make it easier to manage complex interdependencies. Your application may depend on a particular version of the library, and that library may depend on another specific version of the library, and these dependencies are stacked like turtles. Your situation becomes difficult when your application uses two libraries that have conflicting dependencies. Pipenv hopes to ease this pain by tracking the application interdependency tree in a file called Pipfile.lock. Pipfile.lock also verifies that the correct version of the dependency is used in production.
In addition, Pipenv is convenient when multiple developers are working on a project. Through pip workflows, Casey may install a library and spend two days using it to implement a new feature. When Casey commits the changes, he may forget to run pip freeze to update the requirements.txt file. The next day, Jamie took Kathy's change, and the test suddenly failed. It will take a while to realize that the problem is the lack of related libraries in the requirements.txt files that Jamie has not yet installed in the virtual environment.
Because Pipenv automatically records dependencies during installation, if Jamie and Casey use Pipenv,Pipfile, it will be automatically updated and included in Casey's submission. In this way, Jamie and Casey can save time and ship their products faster.
* recommend Pipenv to others working on your project because it uses a standardized way to install project dependencies and development and testing requirements. Using pip workflows and requirements.txt files means that you may have only one requirements.txt file, or multiple requirements.txt files for different environments. For example, your colleagues may not know whether they should run dev.txt or local.txt when running projects on their laptops. It can also cause confusion when two similar requirements.txt files are out of sync with each other: is local.txt out of date, or should it really be different from dev.txt? Multiple requirements.txt files require more context and documentation to enable others to install dependencies correctly as expected. This workflow may confuse and increase your maintenance burden.
Using Pipenv, it generates Pipfile, which avoids these problems by managing dependencies on different environments for you. This command installs the primary project dependency:
Pipenv install
Adding the-- dev flag installs the developed / tested requirements.txt:
Pipenv install-dev
Using Pipenv has other benefits: it has better security features, draws your dependencies in an easy-to-understand format, handles .env files seamlessly, and automatically handles different dependencies between development and production environments in one file. You can read more in the document.
Use Pipenv
The basics of using Pipenv are detailed in the official Python package Management tutorial Managing Application Dependencies. To install Pipenv, use pip:
Pip install pipenv
To install the package used in the project, change to the directory of the project. Then install a package (we'll use Django as an example) and run:
Pipenv install django
You will see some output indicating that Pipenv is creating a Pipfile for your project.
If you haven't used a virtual environment yet, you'll also see some output from Pipenv, indicating that it is creating a virtual environment for you.
Then you will see the output that you often see when installing the package.
To generate the Pipfile.lock file, run:
Pipenv lock
You can also use Pipenv to run Python scripts. Run the upper-level Python script named hello.py:
Pipenv run python hello.py
You will see the expected results in the console.
Start a shell and run:
Pipenv shell
If you want to convert a project that currently uses requirements.txt files to use Pipenv, install Pipenv and run:
Pipenv install requirements.txt
This creates a Pipfile and installs the specified requirements.txt. Consider upgrading your project!
Learn more
Check the Pipenv documentation, especially the basic usage of Pipenv, to help you learn further. Kenneth Reitz, the founder of Pipenv, gave a speech for Pipenv at the recent PyTennessee: "the Future of Python relying on Management." The speech was not recorded, but his slides helped to understand what Pipenv did and the problems he solved.
On how to use Pipenv to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.