Quickly learn how pipenv creates a virtual environment!

WBOY
Release: 2024-01-04 12:05:56
Original
879 people have browsed it
<p><img src="https://img.php.cn/upload/article/000/887/227/170434116288979.jpg" alt="Quickly learn how pipenv creates a virtual environment!"></p> <p>Let’s learn how pipenv creates a virtual environment efficiently! </p> <p>During the development process, we often need to use different Python packages and dependencies in different projects. To avoid conflicts with these packages and dependencies, we can use virtual environments. The virtual environment can manage our packages and dependencies independently of the global environment of the system, ensuring the independence of each project. </p> <p>pipenv is a Python virtual environment and package management tool. Through pipenv, you can easily create and manage virtual environments, and automatically handle the installation and version control of dependencies. Next, I will introduce how to use pipenv to efficiently create a virtual environment. </p> <p>First, we need to install pipenv. Open a command line terminal and run the following command to install pipenv: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pip install pipenv</pre><div class="contentsignin">Copy after login</div></div><p>After the installation is complete, we can create a new virtual environment. Go to the directory where you want to create the virtual environment and run the following command: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv shell</pre><div class="contentsignin">Copy after login</div></div><p> This will create a new virtual environment and activate it. You can see the name of the virtual environment in front of the command line prompt, indicating that you are using the virtual environment. </p><p>Next, install the dependency packages in the virtual environment. Suppose you want to install a package called <code>requests</code>, just use the following command: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv install requests</pre><div class="contentsignin">Copy after login</div></div><p> This will automatically install the latest version of the <code>requests</code> package and add it Added to the virtual environment's dependency list. </p><p>If you need to install a specific version of the package, you can use the <code>==</code> operator to specify the version number. For example, to install version 2.27.1 of <code>requests</code>, you can run the following command: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv install requests==2.27.1</pre><div class="contentsignin">Copy after login</div></div><p> You can also use <code><</code>, <code>></code> , <code><=</code>, <code>>=</code> and other operators to specify the version range to meet different needs. </p><p> If you are installing dependencies from an existing <code>requirements.txt</code> file, you can use the following command: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv install -r requirements.txt</pre><div class="contentsignin">Copy after login</div></div><p>This will automatically install dependencies from <code>requirements.txt Read the dependency list in </code> and install the corresponding package. </p><p>During the development process, we may continue to add and delete dependent packages. In order to ensure the consistency between the current environment and the dependency list, we can use the following command to generate the <code>Pipfile.lock</code> file: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv lock</pre><div class="contentsignin">Copy after login</div></div><p><code>Pipfile.lock</code>The file will contain the current Detailed information about the environment, including the version number and dependencies of each dependent package. When restoring the environment later or sharing the project with other developers, simply restore the environment via the <code>Pipfile.lock</code> file. </p><p>When we no longer need a dependent package, we can use the following command to uninstall it: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv uninstall package_name</pre><div class="contentsignin">Copy after login</div></div><p>This will remove the package from the dependency list and remove it from the virtual environment uninstall. </p><p>In addition to the commands introduced above, pipenv also provides many other useful functions. For example, you can use the following command to view the installed packages and versions in the current virtual environment: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv graph</pre><div class="contentsignin">Copy after login</div></div><p> You can also use the following command to run a command, such as executing a Python script: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:python;toolbar:false;'>pipenv run python script.py</pre><div class="contentsignin">Copy after login</div></div><p> This will run the specified command in the virtual environment. </p> <p>To sum up, pipenv is a powerful Python virtual environment and package management tool. Through pipenv, we can easily create and manage virtual environments and automatically handle dependency installation and version control. I hope this article can help you use pipenv to create virtual environments more efficiently and improve your development efficiency! </p>

The above is the detailed content of Quickly learn how pipenv creates a virtual environment!. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template